有什么方法可以知道我目前使用的方法的名称吗?
以便:
procedure TMyObject.SomeMethod();
begin
Writeln('my name is: ' + <hocus pocus>);
end;
会产生这个输出:
my name is: SomeMethod
JCL是免费的,并且具有相应的功能。它确实取决于堆栈跟踪的效果以及存在多少调试信息。
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;
它能够加载.map
文件,并将其压缩为优化的二进制格式。它会比它.map
本身小很多(例如 900 KB .map
-> 70 KB .mab
)。这.mab
可以很容易地嵌入到 exe 中。因此它小于 JCL 或 MadExcept 使用的格式,也小于 Delphi 在编译时嵌入的信息。
您将这样使用它:
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;
例如,这是我们的日志记录类中使用它的方式。
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;
该方法能够检索调用者的地址,并记录其单元名称、方法名称和行号。
注意/编辑:mORMot 日志单元的源代码是SynLog.pas。可通过此 URI 访问更新的文档。