Advertisement
RandomClear

How to log/diagnose sending

Aug 11th, 2015
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.20 KB | None | 0 0
  1. // If you are using latest (modern) versions of EurekaLog - you can enable run-time debug mode:
  2. // https://www.eurekalog.com/help/eurekalog/fix_runtime_issues.php
  3. // Run your application with run-time mode enabled and find newly created run-time log file (.csl file).
  4. // Open run-time log file and browse / search / filter / organize messages to find:
  5. // "EurekaLog.Dialog" category and SendBugReport / SendBugReportInternal methods.
  6. // "EurekaLog.Sending" category and EurekaLogSend function.
  7. // The send log can be found inside these methods.
  8.  
  9. // Alternatively, you may manually log sending as shown below
  10.  
  11. // This code example will show you how to capture logging output from bug report sender
  12.  
  13. type
  14.   TForm1 = class(TForm)
  15.     Memo1: TMemo;
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. uses
  26.   EConsts,
  27.   ETypes,
  28.   ELowLevel,
  29.   ESysInfo,
  30.   ECore,
  31.  
  32.   EDialog,
  33.   ESend,
  34.  
  35.   EDialogWinAPIMSClassic,   // < - replace with your selected dialog
  36.   ESendAPIJIRA,             // < - replace with your selected sender
  37.   ECompatibility;
  38.  
  39. {$R *.dfm}
  40.  
  41. type
  42.   EEurekaSendTestException = class(Exception);
  43.  
  44. procedure TForm1.Button1Click(Sender: TObject);
  45. begin
  46.   raise EEurekaSendTestException.Create('Test exception');
  47. end;
  48.  
  49. //___________ Logging code below ___________
  50.  
  51. type
  52. {$IFNDEF UNICODE}
  53.   UnicodeString = WideString;
  54. {$ENDIF}
  55.   TELTrackerJIRASender = class(ESendAPIJIRA.TELTrackerJIRASender);  // < - replace with your selected sender
  56.   TMSClassicDialog = class(EDialogWinAPIMSClassic.TMSClassicDialog) // < - replace with your selected dialog
  57.   private
  58.     // New stuff for logging:
  59.     FSendLogFile: TStream;
  60.     procedure LogSend(const AMsg: String);
  61.   protected
  62.     // Override sending behaviour
  63.     function SendBugReportInternal: TResponse; override;
  64.   end;
  65.  
  66. { TMSClassicDialog }
  67.  
  68. function TMSClassicDialog.SendBugReportInternal: TResponse;
  69.  
  70.   procedure Prepare;
  71.   begin
  72.     // Make sure that we've used LogBuilder before possible async send
  73.     BugReport;
  74.  
  75.     // Fill necessary information for sending bug report
  76.     Options.CustomField[sifBugAppVersion]          := ESysInfo.GetVersionNumber;
  77.     Options.CustomField[sifBugID]                  := ExceptionInfo.BugIDStr;
  78.     Options.CustomField[sifBugIDSource]            := ExceptionInfo.BugIDSource;
  79.     Options.CustomField[sifBugText]                := ExceptionInfo.ExceptionMessage;
  80.     Options.CustomField[sifBugType]                := ExceptionInfo.ExceptionClass;
  81.     Options.CustomField[sifBuild]                  := ESysInfo.GetVersionNumber;
  82.     Options.CustomField[sifMachineID]              := Options.OverrideComputerName;
  83.     Options.CustomField[sifMessage]                := BugReportForSend;
  84.     Options.CustomField[sifOSBuild]                := ESysInfo.GetOSBuild;
  85.     Options.CustomField[sifOSType]                 := ESysInfo.GetOSTypeStr;
  86.     Options.CustomField[sifPlatform]               := ESysInfo.GetPlatform;
  87.     Options.CustomField[sifUserEMail]              := Options.OverrideUserEMail;
  88.     Options.CustomField[sifStepsToReproduce]       := ReproduceText;
  89.   end;
  90.  
  91.   procedure SendWithLogging;
  92.   var
  93.     ELSender: TELTrackerJIRASender;
  94.     X: Integer;
  95.   begin
  96.     LogFileName := GetFolderTemp + 'CB03001828F84110B1EF17A503B8EEE8.txt';
  97.     DeleteFile(LogFileName);
  98.     try
  99.       FSendLogFile := TFileStream.Create(LogFileName, fmCreate or fmShareExclusive);
  100.     except
  101.       FSendLogFile := nil;
  102.     end;
  103.     try
  104.       if Assigned(FSendLogFile) then
  105.         FSendLogFile.WriteBuffer(BOM_UTF16_LSB, SizeOf(BOM_UTF16_LSB));
  106.  
  107.       LogSend('Testing send with ' + TELTrackerJIRASender.ClassName + ' on ' + DateTimeToStr(Now)); // <- replace with your selected sender
  108.  
  109.       ELSender := TELTrackerJIRASender.Create; // <- replace with your selected sender
  110.       try
  111.         ELSender.OnLog := LogSend;
  112.  
  113.         LogSend(sLineBreak + 'Options:');
  114.         LogSend(ELSender.Options.ToString);
  115.         LogSend(sLineBreak + 'Files:');
  116.         for X := 0 to FilesToSend.Count - 1 do
  117.           LogSend(FilesToSend[X]);
  118.         LogSend(sLineBreak + 'Exception:');
  119.         LogSend(ExceptionInfo.ToString);
  120.  
  121.         Result := EurekaLogSend(ELSender, Options, FilesToSend, ProgressIndicator, ProcessMessages, GetEnvVariable);
  122.       finally
  123.         FreeAndNil(ELSender);
  124.       end;
  125.  
  126.     finally
  127.       FreeAndNil(FSendLogFile);
  128.       if FileExists(LogFileName) then
  129.       begin
  130.         ShellExec(LogFileName);
  131.         Sleep(10000);
  132.         DeleteFile(LogFileName);
  133.       end;
  134.     end;
  135.   end;
  136.  
  137. begin
  138.   Prepare;
  139.   SendWithLogging;
  140. end;
  141.  
  142. // This implementation will log sending into:
  143. // 1. File on disk (in TEMP folder);
  144. // 2. IDE's "Events" window;
  145. // 3. Memo1 on main form.
  146. procedure TMSClassicDialog.LogSend(const AMsg: String);
  147. var
  148.   WS: UnicodeString;
  149. begin
  150.   if IsDebugged then
  151.     OutputDebugString(PChar(AMsg));
  152.  
  153.   if Assigned(FSendLogFile) then
  154.   begin
  155.     WS := AMsg + sLineBreak;
  156.     FSendLogFile.WriteBuffer(WS[1], Length(WS) * SizeOf(WideChar));
  157.   end;
  158.  
  159.   if Assigned(Form1) then
  160.     Form1.Memo1.Lines.Add(WS);
  161. end;
  162.  
  163. initialization
  164.   RegisterDialogClassFirst(TMSClassicDialog); // < - replace with your selected dialog
  165. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement