I'm using Delphi 7 and EurekaLog 7 (in compatibility mode) and simply want to get call stack of a handled exception, like
procedure CrossThreadFunc;
begin
try
SomeCode;
except
on E: Exception do
Log(CallStackOf(E));
end;
end;
It's a multi-threaded application so I'd like to see the call stack of the calling thread if possible. Also, since this is a handled exception, do I still need to use EurekaLog's OnExceptionRaise event? (which I don't want to).
Edit: CrossThreadFunc() is being called many times with some arguments and what I need to know is exactly where I called it that eventually caused SomeCode() to raise an exception.
There are several ways to do this, described in EurekaLog's help:
Option 1
(Delphi 2009+ only)
Assuming you only need a textual representation:
Option 2
Assuming you have access to RTL's exception object:
Note: due to bugs in some older IDEs, you may need to write like this:
Option 3
Assuming you have access to EurekaLog's exception information object (such as argument in an event handler):
Option 4
Assuming you want call stack for last (e.g. most recent) exception in current thread:
Notes:
If you need current call stack - use
GetCurrentCallStackfunction fromECallStackunit:uses ECallStack; // for TEurekaBaseStackList and GetCurrentCallStack
procedure TForm1.Button1Click(Sender: TObject); var CallStack: TEurekaBaseStackList; begin CallStack := GetCurrentCallStack; // You can also use other functions from ECallStack unit try Memo1.Lines.Text := CallStack.ToString; // You can also use CallStackToString(s) routines to customize textual formatting finally FreeAndNil(CallStack); end; end;
If you need call stacks from other threads - just set it up in options, call stacks for all enabled threads will be included in the same call stack object. You can distingush between threads by
ThreadIDproperty of call stack entry.If you need to convert retrieved call stack to text/string representation for logging purposes, see this:
Option 1
Use
StackTraceproperty of exception object (Delphi 2009+):Option 2
Use
ToStringmethod to convert call stack to single string with default formatting:Option 3
Use
Assignmethod to convert call stack toTStringsobject with default formatting:Option 4
Use
CallStackToStringfunction fromECallStackunit:Option 5
Use
CallStackToStringsfunction fromECallStackunit:Available formatters are:
P.S. You may also want to consider EurekaLog's logging procedures.