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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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 GOracle 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.