Advertisement
RandomClear

How to convert call stack to text

Jun 6th, 2013
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.79 KB | None | 0 0
  1. // See also: http://pastebin.com/36yMX1Mf - "how to get call stack for last exception"
  2. // See also: http://pastebin.com/ACtr0r6S - "how to silently log exception"
  3. // See also: http://pastebin.com/rdNbEwPH - "how to get call stack on demand"
  4.  
  5. // Case 0: use StackTrace property of exception object (Delphi 2009+)
  6.  
  7. except
  8.   on E: Exception do
  9.     Memo1.Lines.Text := E.StackTrace;
  10. end;
  11.  
  12. // Case 1: use ToString method to convert call stack to single string with default formatting
  13.  
  14. var
  15.   CallStack: TEurekaBaseStackList;
  16. begin
  17.   CallStack := ...;
  18.   Memo1.Lines.Text := CallStack.ToString;
  19. end;
  20.  
  21.  
  22. // Case 2: use Assign method to convert call stack to TStrings object with default formatting
  23.  
  24. var
  25.   CallStack: TEurekaBaseStackList;
  26. begin
  27.   CallStack := ...;
  28.   Memo1.Lines.Assign(CallStack);
  29. end;
  30.  
  31.  
  32. // Case 3: use CallStackToString function
  33. // (CallStackToString function allows you to override header and formatting)
  34.  
  35. var
  36.   CallStack: TEurekaBaseStackList;
  37.   Formatter: TCompactStackFormatter;
  38. begin
  39.   CallStack := ...;
  40.  
  41.   // Default formatting and header:
  42.   Memo1.Lines.Text := CallStackToString(CallStack);
  43.  
  44.   // Custom header:
  45.   Memo1.Lines.Text := CallStackToString(CallStack, 'Error Details:');
  46.  
  47.   // Custom formatting:
  48.   Formatter := TCompactStackFormatter.Create;
  49.   try
  50.     // <- here you can customize Formatter (for example: alter captions for columns, etc.)
  51.     Memo1.Lines.Text := CallStackToString(CallStack, '', Formatter);
  52.   finally
  53.     FreeAndNil(Formatter);
  54.   end;
  55. end;
  56.  
  57.  
  58. // Case 4: use CallStackToStrings function
  59. // (CallStackToStrings function allows you to override header and formatting)
  60.  
  61. var
  62.   CallStack: TEurekaBaseStackList;
  63.   Formatter: TCompactStackFormatter;
  64. begin
  65.   CallStack := ...;
  66.  
  67.   // Default formatting and header:
  68.   CallStackToStrings(CallStack, Memo1.Lines);
  69.  
  70.   // Custom header:
  71.   CallStackToStrings(CallStack, Memo1.Lines, 'Error Details:');
  72.  
  73.   // Custom formatting:
  74.   Formatter := TCompactStackFormatter.Create;
  75.   try
  76.     // <- here you can customize Formatter (for example: alter captions for columns, etc.)
  77.     CallStackToStrings(CallStack, Memo1.Lines, '', Formatter);
  78.   finally
  79.     FreeAndNil(Formatter);
  80.   end;
  81. end;
  82.  
  83.  
  84.  
  85.  
  86. {
  87.  
  88. Available build-in formatters for call stack are:
  89. - TEurekaStackFormatter - general formatter for EurekaLog-style call stack (i.e. fixed-width table with columns) - best to be used in text files
  90. - TEurekaStackFormatterV6 - backward-compatibility formatter to produce call stack in EurekaLog V6 format (less columns)
  91. - TSimpleStackFormatter - produces list-like view of call stack (no columns) suitable for variable-width fonts (best to be used in message boxes)
  92. - TCompactStackFormatter - similar to TSimpleStackFormatter, but produces more compact output with less details (good for quick preview)
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement