tomazy
tomazy

Reputation: 915

How to get current method's name in Delphi 7?

Is there any way to know the name of a method I'm currently in?

So that:

procedure TMyObject.SomeMethod();
begin
  Writeln('my name is: ' + <hocus pocus>); 
end;

would produce this output:

my name is: SomeMethod

Upvotes: 30

Views: 12276

Answers (3)

Alex
Alex

Reputation: 5668

If you have EurekaLog:

uses
  EDebugInfo;

procedure TMyObject.SomeMethod();
begin
  Writeln('my name is: ' + __FUNCTION__); 
end;

There is also __FILE__, __MODULE__, __UNIT__, __LINE__, as well as a generic GetLocationInfoStr function.

However:

  1. It will only work if you are compiling with some debug information (and the corresponding debug info provider is enabled):
  • EurekaLog has its own debug info format, which can be optionally compressed (which is actually not recomended, as you will spend more memory and CPU).
  • It also supports JCL/JEDI, Synopse/MAB, as well as .map, .tds/TD32, .dbg, .pdb.
  1. It is NOT a constant. The name will be looked up dynamically, so it have some run-time costs.

Upvotes: 1

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43023

See also our TSynMapFile class.

It is able to load a .map file, and compress it into an optimized binary format. It will be much smaller than the .map itself (e.g. 900 KB .map -> 70 KB .mab). This .mab can be easily embedded within the exe. It is therefore smaller than the format used by JCL or MadExcept, and also smaller than the information embedded at compile time by Delphi.

You'll use it as such:

Map := TSynMapFile.Create; // or specify an exe name
try
  i := Map.FindSymbol(SymbolAddr);
  if i>=0 then 
    writeln(Map.Symbols[i].Name);
  // or for your point:
  writeln(Map.FindLocation(Addr)); // e.g. 'SynSelfTests.TestPeopleProc (784)'
finally
  Map.Free;
end;

For instance, here is how it is used from our logging classes.

procedure TSynLog.Log(Level: TSynLogInfo);
var aCaller: PtrUInt;
begin
  if (self<>nil) and (Level in fFamily.fLevel) then begin
    LogHeaderLock(Level);
    asm
      mov eax,[ebp+4]  // retrieve caller EIP from push ebp; mov ebp,esp
      sub eax,5        // ignore call TSynLog.Enter op codes
      mov aCaller,eax
    end;
    TSynMapFile.Log(fWriter,aCaller); // here it will call TSynMapFile for the current exe
    LogTrailerUnLock(Level);
  end;
end;

This method is able to retrieve the caller's address, and log its unit name, method name and line number.

Note/edit: the source code of the mORMot log unit is SynLog.pas. The updated documentation is reacheable at this URI.

Upvotes: 12

Lars Truijens
Lars Truijens

Reputation: 43587

JCL is free and has functions for that. It does depend on how well a stack trace can be made and how much debug information is present.

JclDebug.pas

function FileByLevel(const Level: Integer = 0): string;
function ModuleByLevel(const Level: Integer = 0): string;
function ProcByLevel(const Level: Integer = 0): string;
function LineByLevel(const Level: Integer = 0): Integer;

Upvotes: 33

Related Questions