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
  ELowLevel,  // for GetEIP
  EClasses,   // for TELLocationInfo
  EDebugInfo; // for GetLocationInfo
 
procedure Test;
begin
 
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  Info: TELLocationInfo;
begin
  // Get information about arbitrary code location
  Info := GetLocationInfo(@Test);

 

  // Once information is obtained - you can use it
  Label1.Caption := Info.UnitName + '.' + Info.ProcedureName;
  // Will output: 'Unit1.Test'

 

 

  // Get information about current location
  Info := GetLocationInfo(GetEIP);

 

  Label2.Caption := Info.UnitName + '.' + Info.ProcedureName;
  // Will output: 'Unit1.Button1Click'
end;

 

If you need information about current location only - 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: 2024-09-30
Last edited: 2024-06-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