Root > How to... > ...get source code location information?

...get source code location information?

Previous pageReturn to chapter overviewNext page   

Use GetLocationInfo function from EDebugInfo unit:

 

uses
  ECallStack, // for CurrentAddress and ReturnAddress
  EClasses,   // for TELLocationInfo
  EDebugInfo; // for GetLocationInfo and LocationToStr
 
procedure Test; forward;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  Info: TELLocationInfo;
begin

  // 1. Get information about arbitrary code location
  Info := GetLocationInfo(@Test); // <- pass a pointer to a location

 

  // Once information about location is obtained - you can use it:
  ShowMessage(Info.UnitName + '.' + Info.ProcedureName);
  // Will show: 'Unit1.Test'

 

 

 

  // 2. Get information about current location
  Info := GetLocationInfo(CurrentAddress); // <- this is line #42 in the Unit1.pas file

 

  ShowMessage(LocationToStr(Info, False, False, False, False, False));
  // Will show: 'Unit1.TForm1.Button1Click (Line 42, "Unit1.pas")'

 

  Test; // call the test function below

end;

 

procedure Test;
var
  Info: TELLocationInfo;
begin
  // 3. Get information about our caller (see above)
  Info := GetLocationInfo(ReturnAddress);

 

  ShowMessage('The function was called from ' + Info.UnitName + '.' + Info.ClassName + '.' + Info.ProcedureName);

  // Will show: 'Unit1.TForm1.Button1Click'
end;
 

 

If you need information about current location only (the CurrentAddress in the example above) - you can also use simple wrappers: __MODULE__, __UNIT__, __FILE__, __FUNCTION__, and __LINE__. For example:

 

uses
  EDebugInfo; // for __FILE__, __FUNCTION__, __LINE__
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Log('Entering ' + __FILE__ + ' ' + __FUNCTION__ + ' ' + __LINE__);

  // Will log 'Entering Unit1.pas TForm1.Button1Click 42'

 

  // Real code goes below

  // ...

end;

 

Please note that it is better to use a proper logging.

 

 

See also:




Send feedback... Build date: 2025-11-08
Last edited: 2025-11-03
PRIVACY STATEMENT
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.


Permanent link to this article: https://www.eurekalog.com/help/eurekalog/how_to_get_source_code_location_info.php