Solved

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

Posted on 2012-03-28
11
433 Views
Last Modified: 2012-03-28
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
0
Comment
Question by:jatkin
  • 5
  • 4
  • 2
11 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 37776082
Perhaps this helps you...
How to get line number at runtime
0
 
LVL 4

Author Comment

by:jatkin
ID: 37776089
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?
0
 
LVL 19

Accepted Solution

by:
Thommy earned 250 total points
ID: 37776193
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
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 37776200
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
0
 
LVL 36

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 250 total points
ID: 37776207
if it's not for exception handling, then this $LINENO variables doesn't exist
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Author Closing Comment

by:jatkin
ID: 37776221
I had come to the same conclusion :-(
Thanks anyway...
James
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 37776225
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
0
 
LVL 4

Author Comment

by:jatkin
ID: 37776234
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.
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 37776242
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

0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 37776285
why not simply use a timestamp in the comment ?
hh:nn:ss.zzz
0
 
LVL 4

Author Comment

by:jatkin
ID: 37776495
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 58
Delphi XE10, MySQL Query 4 108
How to debug For loops? 3 33
tidtcpserver connection lost handle 2 42
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now