EurekaLog
Menu

EurekaLog 7 Manual

...get source code location information?

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: