How can I embed the line number of code into a compiled exe in Delphi?

Hi all,
I wish to embed the Source Code line number into some Delphi Source.

S1 := 'Something';
S2 := 'Something at line {$LINENO}';

Now, no matter where this code is, I want S2 to reflect the actual line it appears at the code.
Putting a literal line in is not really practical as this may be in many places, and as soon as any text is inserted above the line in question, they are immediately wrong :-(
Also, this text is not necessarily in an exception handler; it may just be in the middle of a code block.

Any ideas if this is possible?

Is there some wonderful {$LINENO} that is available :-)

Any help would be appreciated.

Best regards,

James AtkinSenior Principle Software EngineerAsked:
Who is Participating?
ThommyConnect With a Mentor Commented:
It's not basically about excepting handling!
I think the main statement of my suggested link is this:

You can't do this without a map file or something equivalent. The process of compilation leaves the source code behind. ...

Please read this part at How to get line number at runtime
Perhaps this helps you...
How to get line number at runtime
James AtkinSenior Principle Software EngineerAuthor Commented:
Thanks for the reply. I did look at this article but it appears to deal with overriding the exception handling, which is not really what I am after - unless I am misinterpreting something?
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Geert GOracle dbaCommented:
embed into to the source code ????
source code doesn't contain line numbers

find the source code line when trying to resolve an error may be handy
> in this case use a error logging tool like MadExcept or Eurekalog

why do you want to add line numbers to code ???
>> delphi wouldn't compile
Geert GConnect With a Mentor Oracle dbaCommented:
if it's not for exception handling, then this $LINENO variables doesn't exist
James AtkinSenior Principle Software EngineerAuthor Commented:
I had come to the same conclusion :-(
Thanks anyway...
Geert GOracle dbaCommented:
what are you trying to do anyway ?

there may be other solutions to your problem ...
or the problem may not be what you think it is
James AtkinSenior Principle Software EngineerAuthor Commented:
I have various comments logged to an event log during execution of a program, some of which are the same within a module. I wanted a way to distinguish between these comments without manually needing to ensure that these are kept identifiable by adding a suffix of 1, 2, 3..... etc.
I thought simply using Line numbers in place of the number would be the simplest way to do this in a generic manner.
Geert GOracle dbaCommented:
if you were to save text in a separate file (say for config)
and you want a line number:


Open in new window

  s: TStrings;
  I: Integer;
  s := TStringList.Create;
    for I := 0 to s.Count-1 do
      if Pos('[LINENO'], s[I]) > 0 then 
        s[I] := StringReplace(s[I], ['LINENO'], IntToStr(I), [rfReplaceAll]);
    ShowMessage(S.Values['param4]); // >> showMessage('2'); // 0 based = line 3

Open in new window

Geert GOracle dbaCommented:
why not simply use a timestamp in the comment ?
James AtkinSenior Principle Software EngineerAuthor Commented:
I need to identify the section of code that was executing rather than making it unique within the log.

What I have at the moment is something like...

Log('Command Executed Successfully : 1');
Log('Command Executed Successfully : 2');
Log('Command Executed Successfully : 3');

I cannot just do :

Log('Command Executed Successfully : ' + IntToStr(X)); inc(X);

as there is no guarantee that these are all executed throughout the execution, so if the 1st and 3rd were executed, I would see:

      Command Executed Successfully : 1
      Command Executed Successfully : 2

Which would not really help me.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.