【发布时间】:2010-11-21 00:53:23
【问题描述】:
有什么方法可以知道我目前正在使用的方法的名称吗?
这样:
procedure TMyObject.SomeMethod();
begin
Writeln('my name is: ' + <hocus pocus>);
end;
会产生这个输出:
my name is: SomeMethod
【问题讨论】:
有什么方法可以知道我目前正在使用的方法的名称吗?
这样:
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。更新后的文档是reacheable at this URI。
【讨论】: