Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

delphi - How to get line number at runtime

is it possible to get source line number at runtime in Delphi? I know JCL debug, but I want to avoid to use it. Also Assert is not exactly what I want. I would like to get something like this, where GetLineNumber would get the source line number. Is it possible to do it without MAP files (anyway will MAP file be generated when I use Assert) ? Is there any example ?

function GetLineNumber: integer;
begin
  ???
end;

procedure ThisWouldBeGreat;
begin
  if not SomeCondition then
    LogMemo.Lines.Add('Error on line: ' + IntToStr(GetLineNumber));
end;

procedure ThatsWhatIWont;
begin
  Assert(not SomeCondition, 'Error');
end;

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can indeed use Assert for this. Write a procedure that matches the signature dictated by the TAssertErrorProc type, and then do whatever you want there. To preserve the expected behavior, you should probably call the original handler after you're finished.

procedure MichaelAssertProc(const Message, Filename: string;
  LineNumber: Integer; ErrorAddr: Pointer);
begin
  LogMessage(...);
  SysUtils.AssertErrorHandler(Message, Filename, LineNumber, ErrorAddr);
end;

Assign that procedure to System.AssertErrorProc sometime while your program starts up.

AssertErrorProc := MichaelAssertProc;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...