PeterPanettone 167 Posted Monday at 04:36 PM It seems that Delphi does not have a built-in compiler directive or "magic word" equivalent to __LINE__ or $LINE for directly accessing the current source code line number, as seen in languages like C/C++. Free Pascal (which shares compatibility with Delphi) does support special tokens like %LINE% and %FILE% within the {$INCLUDE} directive to insert the current line number or file name as text. Is there a way to make Delphi support this (e.g., with custom pre-processors)? Share this post Link to post
DelphiUdIT 260 Posted Monday at 05:58 PM (edited) May be this can help: https://en.delphipraxis.net/topic/11249-embed-line-number-at-design-time/?do=findComment&comment=89256 P.S:: those work at runtime not like preprocessors directive. Edited Monday at 06:01 PM by DelphiUdIT 1 Share this post Link to post
PeterPanettone 167 Posted Monday at 07:44 PM (edited) GREAT! This function solves my inspecting problem: function LN: string; begin Result := IntToStr(JclDebug.GetLocationInfo(ReturnAddress).LineNumber); end; I now use this CnWizards Source Template: 1. I press CTRL+SHIFT+# which inserts this code: CodeSite.Send('TForm1.Button1Click, Line Number: ' + LN + ': ', E); 2. I insert the desired expression I want to inspect, for example, Sender.ClassName CodeSite.Send('TForm1.Button1Click, Line Number: ' + LN + ': Sender.ClassName', Sender.ClassName); 3. And then, in the CodeSite Live Viewer at runtime, I get the desired inspection result: TForm1.Button1Click, Line Number: 40: Sender.ClassName = TButton Happy inspecting! Edited Monday at 07:58 PM by PeterPanettone Share this post Link to post
DelphiUdIT 260 Posted Monday at 10:08 PM 2 hours ago, PeterPanettone said: CodeSite.Send('TForm1.Button1Click, Line Number: ' + LN + ': Sender.ClassName', Sender.ClassName); With the same function you can get also the procedure name ' TForm1.Button1Click', instead to write it like a constant: GetLocationInfo(ReturnAddress).ProcedureName Think to use a temporary record and call GetLocationInfo only one time. 1 Share this post Link to post
PeterPanettone 167 Posted Tuesday at 11:26 AM (edited) 13 hours ago, DelphiUdIT said: With the same function you can get also the procedure name ' TForm1.Button1Click', instead to write it like a constant: GetLocationInfo(ReturnAddress).ProcedureName Think to use a temporary record and call GetLocationInfo only one time. Thanks for the hint. Currently, the procedure name is automatically inserted at design time by the CnWizards Source Template macro %CurrProcName%. However, I plan to create a separate IDE plugin (along with other features) - if Thomas has not already picked up the idea - where a CodeSite editor replaces the CnWizards Source Template macro. That would make it very flexible and independent of the CodeSite statement location. Edited Tuesday at 11:39 AM by PeterPanettone Share this post Link to post
PeterPanettone 167 Posted 10 hours ago (edited) No need to use a record: uses CodeSiteLogging, JCLDebug; function _Loc: JclDebug.TJclLocationInfo; begin Result := JclDebug.GetLocationInfo(ReturnAddress); end; procedure TForm1.Button1Click(Sender: TObject); begin // 1. At design time, insert the CodeSite statement with CTRL+SHIFT+# by using this CnWizards Source Template: // CodeSite.Send(_Loc.ProcedureName + ' (Line#: ' + IntToStr(_Loc.LineNumber) + '): X', X); // 2. Then, replace X with the desired expression to get the Procedure Name and the current line Number at runtime: CodeSite.Send(_Loc.ProcedureName + ' (Line#: ' + IntToStr(_Loc.LineNumber) + '): Sender.ClassName', Sender.ClassName); // 3. The CodeSite result in the CodeSite Live Viewer rsults as: TForm1.Button1Click (Line#: 39): Sender.ClassName = TButton end; Edited 10 hours ago by PeterPanettone Share this post Link to post
dummzeuch 1675 Posted 9 hours ago Wich will result in multiple expensive calls to GetLocationInfo... Share this post Link to post
PeterPanettone 167 Posted 8 hours ago (edited) 8 hours ago, dummzeuch said: Wich will result in multiple expensive calls to GetLocationInfo... My CPU can afford this 😉 However, I've simplified the CodeSite statement and avoided calling GetLocationInfo twice by using a with statement (without using a record): function GetLocPrefix: string; begin CodeSite.Send('GetLocPrefix: '); // this is called only once! // Directly get the location of the caller (Button1Click), skipping the need for _Loc here with JclDebug.GetLocationInfo(ReturnAddress) do Result := ProcedureName + ' (Line#: ' + IntToStr(LineNumber) + '): '; end; procedure TForm1.Button1Click(Sender: TObject); begin // 1. At design time, insert the CodeSite statement with CTRL+SHIFT+# by using this CnWizards Source Template: // CodeSite.Send(GetLocPrefix + 'X', X); // 2. Then, replace X with the desired expression, then get the Procedure Name and the current line Number at runtime: CodeSite.Send(GetLocPrefix + 'Sender.ClassName', Sender.ClassName); // 3. The CodeSite result in the CodeSite Live Viewer rsults as: TForm1.Button1Click (Line#: 51): Sender.ClassName = TButton end; Edited 53 minutes ago by PeterPanettone Share this post Link to post