Link to home
Start Free TrialLog in
Avatar of Member_2_99151
Member_2_99151

asked on

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.
i.e.

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

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
Avatar of Thommy
Thommy
Flag of Germany image

Perhaps this helps you...
How to get line number at runtime
Avatar of Member_2_99151
Member_2_99151

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Thommy
Thommy
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Geert G
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I had come to the same conclusion :-(
Thanks anyway...
James
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
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.
example:
if you were to save text in a separate file (say for config)
and you want a line number:

configfile:
param1=20
param2=30
param4=[LINENO]

Open in new window


code:
var 
  s: TStrings;
  I: Integer;
begin
  s := TStringList.Create;
  try
    s.LoadFromFile(configfile);
    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
  finally
    s.Free;
  end;
  

Open in new window

why not simply use a timestamp in the comment ?
hh:nn:ss.zzz
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.