Jump to content
Ian Branch

Embed line number at design time??

Recommended Posts

Hi Team,

Delphi 12.

Is there a way to embed the current design time code line number into a string so that it can be shown in a message.  It needs to reflectthe actual line number, even if the previous code is edited and it's actual number changes.

Regards & TIA,

Ian

Share this post


Link to post

It sounds like you are trying to add an exception logging mechanism to your application.

Would you like some help?

image.jpeg.e53d1fa2df68dca93f13d6a2f22331bd.jpeg

 

All jokes aside, if that's the case, then you should really investigate proper stack tracing and exception logging mechanisms. 

 

In case (for whatever reason), you really just like to add the current line number into a string, then have a look at:

https://stackoverflow.com/q/7214213

 

  • Haha 1

Share this post


Link to post
1 hour ago, Der schöne Günther said:

In case (for whatever reason), you really just like to add the current line number into a string, then have a look at:

https://stackoverflow.com/q/7214213

I had a look at that previously.  I may be missreading its use but it seems that JCLDebug relies on an Exception for something like this to work:

function GetCurrentLineNumber: Integer;
var
  ModInfo: TJclLocationInfo;
begin
  ModInfo := GetLocationInfo(ExceptAddr);

  Result := ModInfo.LineNumber;
end;

 

Share this post


Link to post
Posted (edited)
1 hour ago, Ian Branch said:

I had a look at that previously.  I may be missreading its use but it seems that JCLDebug relies on an Exception for something like this to work:


function GetCurrentLineNumber: Integer;
var
  ModInfo: TJclLocationInfo;
begin
  ModInfo := GetLocationInfo(ExceptAddr);

  Result := ModInfo.LineNumber;
end;

 

It does indeed. It should also be possible to write a function that reads its own return address from the stack and then returns this value as the result. This then could be passed to GetLocationInfo. I'm not sure how to implement this, but it shouldn't be rocket science.

 

Edit: There is System.ReturnAddress, introduced with Delphi XE2, which gives you just that, so

function GetCurrentLineNumber: Integer;
var
  ModInfo: TJclLocationInfo;
begin
  ModInfo := GetLocationInfo(ReturnAddress);

  Result := ModInfo.LineNumber;
end;

Should do the trick (untested)

Edit: Just tried it. It works, at least in Delphi 10.2.

 

Edit: JclDebug already contains such a function called LineByLevel.

Edited by dummzeuch
  • Like 1

Share this post


Link to post
11 hours ago, Ian Branch said:

Is there a way to embed the current design time code line number into a string so that it can be shown in a message.

FreePascal has {$I %LINE%} (doc) , but Delphi doesn't.

Share this post


Link to post
12 hours ago, dummzeuch said:

Edit: There is System.ReturnAddress, introduced with Delphi XE2, which gives you just that, so

Tks.  Exactly what I needed/wanted.

I made a minor change as I am using D12:

/// <summary>
/// Retrieves the current line number.
/// </summary>
/// <remarks>
/// Requires the JCLDebug unit.
/// </remarks>
/// <returns>The line number of the current position.</returns>
function GetCurrentLineNumber: Integer;
begin
  //
  var ModInfo := GetLocationInfo(ReturnAddress);
  //
  Result := ModInfo.LineNumber;
  //
end;

LineByLevel - Whilst I could use it in my code by adding JCLDebug, I wanted to have it as a function in my library.  If I used it from my library I got the line number in the library, not the Apps code.

 

Regards & Tks again,

Ian

Share this post


Link to post

Just keep in mind that calling GetLocationInfo is rather costly. It's not meant to be called all over the place.

If I remember correctly somebody claimed to have improved the jcldebug file format to make it much smaller and lookups faster. Unfortunately I don't remember who it was and where I read about it. It's quite possible that he donated the code to the jcl in the mean time

Share this post


Link to post
3 hours ago, dummzeuch said:

Just keep in mind that calling GetLocationInfo is rather costly. It's not meant to be called all over the place.

Noted.  Tks.

Share this post


Link to post
On 3/15/2024 at 9:52 PM, Ian Branch said:

LineByLevel - Whilst I could use it in my code by adding JCLDebug, I wanted to have it as a function in my library.  If I used it from my library I got the line number in the library, not the Apps code.

You should get the line number of the original function if you pass 1 as the parameter to LineByLevel instead of the default 0.

Share this post


Link to post
6 hours ago, dummzeuch said:

You should get the line number of the original function if you pass 1 as the parameter to LineByLevel instead of the default 0.

Yup.  Works just as well.  Tks.

Share this post


Link to post
On 3/15/2024 at 7:14 AM, Der schöne Günther said:

In case (for whatever reason), you really just like to add the current line number into a string, then have a look at:

https://stackoverflow.com/q/7214213

 

 

On 3/15/2024 at 8:31 AM, Ian Branch said:

I had a look at that previously.  I may be missreading its use but it seems that JCLDebug relies on an Exception for something like this to work:

The accepted answer to that question doesn't involve exceptions; It redirects the assertion handler to another function which then has access to the unit name and line number.

Share this post


Link to post
On 3/16/2024 at 5:51 PM, dummzeuch said:

If I remember correctly somebody claimed to have improved the jcldebug file format to make it much smaller and lookups faster. Unfortunately I don't remember who it was and where I read about it.

This one maybe: https://blog.synopse.info/?post/2011/04/14/Enhanced-logging-in-SynCommons

via: https://stackoverflow.com/a/7216537/2249664

  • Thanks 1

Share this post


Link to post
10 hours ago, Anders Melander said:

No, at least if I remember correctly that the generated format was compatible to jcldebug.

But thanks for the link anyway. I have wanted to look into synopse logging since I read about it in that blog post ... OMG: 2011? > 10 years ago!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×