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

前端 未结 2 1027
太阳男子
太阳男子 2020-12-08 14:19

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: \' + 

        
相关标签:
2条回答
  • 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;
    
    0 讨论(0)
  • 2020-12-08 15:06

    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.

    0 讨论(0)
提交回复
热议问题