Alois Heimer
Alois Heimer

Reputation: 1832

How to pass call stack information to an exception using EurekaLog?

I have a threaded application and for some purpose I want to pass call stack information of a catched exception to a new custom exception:

try
    //here an unknown exception is rissen
except 
    on E: Exception do
    begin
        if ... then
          raise EMyException.Create(E, CallStackOfExceptionEAsString);
    end;
end;

What is the best way to do this, preferably using EurekaLog? I am using Delphi 2006 btw.

Upvotes: 3

Views: 1292

Answers (3)

Alex
Alex

Reputation: 5668

EurekaLog 7 has Chained Exception support, which is specifically designed for this task. Just enable it in options (it is enabled by default) and use:

try
  // here an unknown exception is rissen
except 
  on E: Exception do
  begin
    if ... then
      Exception.RaiseOuterException(EMyException.Create(E.Message));
      // for old IDEs:
      // raise EMyException.Create(E.Message);
  end;
end;

Upvotes: 0

yonojoy
yonojoy

Reputation: 5566

EurekaLog provides a function GetLastExceptionCallStack() (defined in unit ExceptionLog.pas). Using this I have written the following function (based on example code here):

function GetLastEurekalogCallStackAsString(): string;
{$IFDEF EUREKALOG}
var
  Stack: TEurekaStackList;
  Str: TStringList;
{$ENDIF}
begin
{$IFDEF EUREKALOG}
    Stack := GetLastExceptionCallStack();
    try
        Str := TStringList.Create;
        try
            CallStackToStrings(Stack, Str);
            Result := Str.Text;
        finally
            FreeAndNil(Str);
        end;
    finally
        FreeAndNil(Stack);
    end;
{$ELSE}
    Result := '';
{$ENDIF}
end;

So you can write:

try
    //here an unknown exception is rissen
except 
    on E: Exception do
    begin
        if ... then
          raise EMyException.Create(E, GetLastEurekalogCallStackAsString());
    end;
end;

Upvotes: 2

Erik Virtel
Erik Virtel

Reputation: 840

EurekaLog exposes several event handlers like OnExceptionNotify.

You can implement these in your code. For example: procedure EurekaLogExceptionNotify( EurekaExceptionRecord: TEurekaExceptionRecord; var Handled: Boolean);

Here you can see a TEurekaExceptionRecord which is defined in ExceptionLog.pas. But you maybe just own the non-source version which works just fine.

The record has a EurekaExceptionRecord.CallStack list. This proprietary list can be converted to TStringsusing the CallStackToStrings method which is also defined in the ExceptionLog unit.

Here is an example where I write the CallStack into a StringList.

CallStackList := TStringList.Create;
try
  CallStackToStrings(EurekaExceptionRecord.CallStack, CallStackList);

  LogMessage := 'An unhandled exception occured. Here is the CallStack.' + #13#10
    + CallStackList.Text;
finally
  CallStackList.Free;
end;

At least from this starting point you should be able to investigate the exposed functions, records etc.. All information is accessible.

Upvotes: 2

Related Questions