Delphi:基于jcl的Bugsplat Crash收集单元

//BugSplat Crash模拟.net数据封装

unit uBugSplat;

interface

uses
  Windows, SysUtils, Classes, StrUtils, ShellAPI, JclDebug;

type
  TBugSplat = class
  class var
    Instance: TBugSplat;
  private
    FBSPath: string;
    FDBName: string;
    FAppName: string;
    FVersion: string;
    FQuietMode: Boolean;
    FUser: string;
    FEMail: string;
    FUserDescription: string;
    FLogPath: string;
    FAdditionalFiles: TStrings;

    //生成Crash报告
    procedure CreateReport(E: Exception);
    procedure WriteStack(sw: TStreamWriter; E: Exception);
    function GetTempPath: string;
    function ExecProcess(AppName, Params: string): Boolean;
    procedure AddAdditionalFileFromFolder(const AFolder: string);
  public
    constructor Create(const ADBName, AAppName, AVersion: string);

    //Exception事件接管
    procedure AppException(Sender: TObject; E: Exception);
    procedure AddAdditionalFile(const AFileName: string);

    property User: string read FUser write FUser;
    property EMail: string read FEmail write FEmail;
    property UserDescription: string read FUserDescription write FUserDescription;
    property QuietMode: Boolean read FQuietMode write FQuietMode;
    property LogPath: string read FLogPath write FLogPath;
    property AdditionalFiles: TStrings read FAdditionalFiles write FAdditionalFiles;
  end;

implementation

{ TBugSplat }

constructor TBugSplat.Create(const ADBName, AAppName, AVersion: string);
begin
  FDBName := ADBName;
  FAppName := AAppName;
  FVersion := AVersion;
  //FUserDescription := 'Crash of ' + FAppName;
  FQuietMode := True;
  FBSPath := ExtractFilePath(ParamStr(0)) + 'BsSndRpt.exe';

  FAdditionalFiles := TStringList.Create;
  if Instance = nil then Instance := Self;
end;

procedure TBugSplat.AddAdditionalFile(const AFileName: string);
begin
  if FileExists(AFileName) then
    FAdditionalFiles.Append(AFileName);
end;

procedure TBugSplat.WriteStack(sw: TStreamWriter; E: Exception);
  function RPos(const substr, str: RawByteString): Integer;
  begin
     Result := Length(str) - Pos(ReverseString(substr), ReverseString(str)) + 1;
  end;

var
  i: Integer;
  s, sFileName, sLineNumber: string;
  sl: TStrings;
begin
  sl := TStringList.Create;
  try
    sl.Text := E.StackTrace;
    //Stack头
    sw.WriteLine('<report>');
    sw.WriteLine('  <process>');
    sw.WriteLine('    <exception>');
    sw.WriteLine('      <func><![CDATA[' + sl[0] + ']]></func>');
    sw.WriteLine('      <code><![CDATA[' + E.ClassName + ': ' + E.Message + ']]></code>');
    sw.WriteLine('      <explanation><![CDATA[' + FAppName + ']]></explanation>');
    sw.WriteLine('      <file><![CDATA[]]></file>');
    sw.WriteLine('      <line><![CDATA[]]></line>');
    sw.WriteLine('      <registers></registers>');
    sw.WriteLine('    </exception>');
    sw.WriteLine('    <modules numloaded="0"></modules>');
    sw.WriteLine('    <threads count="1">');
    sw.WriteLine('      <thread id="' + IntToStr(GetCurrentThreadId()) + '" current="yes" event="yes" framecount="1">');

    //StackTrace
    //[004560E8] Controls.TWinControl.MainWndProc (Line 9065, "Controls.pas")
    for i := 0 to sl.Count - 1 do
    begin
      sFileName := '';
      sLineNumber := '';
      s := sl[i];
      if Pos('"', s) <> 0 then
        sFileName := Copy(s, Pos('"', s) + Length('"'), RPos('"', s) - Pos('"', s) - Length('"'));
      if Pos('Line', s) <> 0 then
        sLineNumber := Copy(s, Pos('Line ', s) + Length('Line '), Pos(',', s) - Pos('Line ', s) - Length('Line '));

      sw.WriteLine('        <frame>');
      sw.WriteLine('          <symbol><![CDATA[' + s + ']]></symbol>');
      sw.WriteLine('          <arguments></arguments>');
      sw.WriteLine('          <locals></locals>');
      sw.WriteLine('          <file>' + sFileName + '</file>');
      sw.WriteLine('          <line>' + sLineNumber + '</line>');
      sw.WriteLine('        </frame>');
    end;
    sw.WriteLine('      </thread>');
    sw.WriteLine('    </threads>');
    sw.WriteLine('  </process>');
    sw.WriteLine('</report>');
  finally
    sl.Free;
  end;
end;

procedure TBugSplat.AddAdditionalFileFromFolder(const AFolder: string);
var
  sr: TSearchRec;
  s: string;
begin
  //取其中文件入附加文件列表
  if FindFirst(AFolder + '\*.*', faAnyFile, sr) = 0 then
  begin
    try
      repeat
        if (sr.Name = '.') or (sr.Name = '..') then Continue;

        s := IncludeTrailingPathDelimiter(AFolder) + sr.Name;
        if sr.Attr and faDirectory = 0 then
          FAdditionalFiles.Append(s)
        else if DirectoryExists(s) then
          AddAdditionalFileFromFolder(s);
      until FindNext(sr) <> 0;
    finally
      FindClose(sr);
    end;
  end;
end;

procedure TBugSplat.AppException(Sender: TObject; E: Exception);
begin
  if not FileExists(FBSPath) then
    raise Exception.Create('BsSndRpt.exe does not exists!');

  CreateReport(E);
end;

procedure TBugSplat.CreateReport(E: Exception);
var
  i: Integer;
  xmlName, iniName, args: string;
  sw: TStreamWriter;
begin
  //写.net stack解析文件
  if Trim(E.StackTrace) <> '' then
  begin
    xmlName := IncludeTrailingPathDelimiter(GetTempPath()) + 'stack.net';
    if FileExists(xmlName) then DeleteFile(xmlName);
    sw := TStreamWriter.Create(xmlName);
    try
      WriteStack(sw, E);
    finally
      sw.Close;
    end;
  end;

  //写ini配置文件
  iniName := IncludeTrailingPathDelimiter(GetTempPath()) + 'bs.ini';
  if FileExists(iniName) then DeleteFile(iniName);
  sw := TStreamWriter.Create(iniName);
  try
    sw.WriteLine('[BugSplat]');
    sw.WriteLine('Vendor=' + FDBName);
    sw.WriteLine('Application=' + FAppName);
    sw.WriteLine('Version=' + FVersion);
    if FileExists(xmlName) then
      sw.WriteLine('DotNet=' + xmlName);
    if FUser <> '' then
      sw.WriteLine('User=' + FUser);
    if FEMail <> '' then
      sw.WriteLine('Email=' + FEMail);
    if FUserDescription <> '' then
      sw.WriteLine('UserDescription=' + FUserDescription);

    //附加文件
    if DirectoryExists(FLogPath) then AddAdditionalFileFromFolder(FLogPath);
    for i := 0 to FAdditionalFiles.Count - 1 do
    begin
      if FileExists(FAdditionalFiles[i]) then
        sw.WriteLine('AdditionalFile' + IntToStr(i) + '=' + FAdditionalFiles[i]);
    end;
  finally
    sw.Close;
  end;

  //发送
  args := '/i ' + '"' + iniName + '"';
  if FQuietMode then
    args := args + ' /q';
  ExecProcess(FBSPath, args);
end;

function TBugSplat.ExecProcess(AppName, Params: string): Boolean;
var
  // Structure containing and receiving info about application to start
  ShellExInfo: TShellExecuteInfo;
begin
  FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
  with ShellExInfo do
  begin
    cbSize := SizeOf(ShellExInfo);
    fMask := see_Mask_NoCloseProcess;
    Wnd := 0;
    lpFile := PChar(AppName);
    lpParameters := PChar(Params);
    nShow := SW_SHOWNORMAL;
  end;

  Result := ShellExecuteEx(@ShellExInfo);
end;

function TBugSplat.GetTempPath: string;
var
  p: array[0..MAX_PATH] of Char;
begin
  Windows.GetTempPath(MAX_PATH, p);
  Result := StrPas(p);
end;

//Exception事件挂接...用此其取为空,其下面的可以
//function GetExceptionStackInfoProc(P: PExceptionRecord): Pointer;
//var
//  LLines: TStringList;
//  LText: String;
//  LResult: PChar;
//begin
//  LLines := TStringList.Create;
//  try
//    JclLastExceptStackListToStrings(LLines, True, True, True, True);
//    LText := LLines.Text;
//    LResult := StrAlloc(Length(LText));
//    StrCopy(LResult, PChar(LText));
//    Result := LResult;
//  finally
//    LLines.Free;
//  end;
//end;

function GetExceptionStackInfoProc(P: PExceptionRecord): Pointer;
var
  LLines: TStringList;
  LText: String;
  LResult: PChar;
  jcl_sil: TJclStackInfoList;
begin
  LLines := TStringList.Create;
  try
    jcl_sil := TJclStackInfoList.Create(False, 7, p.ExceptAddr, False, nil, nil);
    try
      jcl_sil.AddToStrings(LLines); //, true, true, true, true);
    finally
      FreeAndNil(jcl_sil);
    end;
    LText := LLines.Text;
    LResult := StrAlloc(Length(LText));
    StrCopy(LResult, PChar(LText));
    Result := LResult;
  finally
    LLines.Free;
  end;
end;

function GetStackInfoStringProc(Info: Pointer): string;
begin
  Result := string(PChar(Info));
end;

procedure CleanUpStackInfoProc(Info: Pointer);
begin
  StrDispose(PChar(Info));
end;

initialization
// Start the Jcl exception tracking and register our Exception
// stack trace provider.
if JclStartExceptionTracking then
begin
  Exception.GetExceptionStackInfoProc := GetExceptionStackInfoProc;
  Exception.GetStackInfoStringProc := GetStackInfoStringProc;
  Exception.CleanUpStackInfoProc := CleanUpStackInfoProc;
end;

finalization
// Stop Jcl exception tracking and unregister our provider.
if JclExceptionTrackingActive then
begin
  Exception.GetExceptionStackInfoProc := nil;
  Exception.GetStackInfoStringProc := nil;
  Exception.CleanUpStackInfoProc := nil;
  JclStopExceptionTracking;
end;

end.

调用方法:

procedure InitBugSplat();
var
  sVersion: string;
begin
  sVersion := GetFileVersion(Application.ExeName);
  if TBugSplat.Instance = nil then
    TBugSplat.Create('XXX_DSB', SDefaultProductName, sVersion);

  Application.OnException := TBugSplat.Instance.AppException;
  TBugSplat.Instance.LogPath := IncludeTrailingBackslash(g_DocumentPath) + 'Log';
  TBugSplat.Instance.EMail := 'xx@xx.com';
  TBugSplat.Instance.UserDescription := 'DSB_' + sVersion;
end;

以做备忘

转载于:https://www.cnblogs.com/crwy/p/8567096.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi JCL控件是Delphi开发环境中的一个非常实用的组件库。JCL是JEDI Code Library的缩写,是一个由开源社区JEDI项目开发的自由软件。它包含了很多基本的控件和组件,用于增强和扩展Delphi IDE的功能。 JCL控件提供了许多在Delphi内置控件中没有的功能和特性。例如,JclSysInfo组件可以方便地获取系统信息,如操作系统版本、CPU类型、内存大小等。JclDebug组件可以提供调试应用程序的功能,例如捕获异常、记录调试信息等。JclMath组件可以进行高级的数学计算,包括矩阵运算、线性代数等。JclRegistry组件可以方便地读写Windows注册表,包括读取和写入键值、创建和删除项等。 除了以上提到的功能,JCL控件还包括了许多其他实用的控件和组件,如日期选择器、提示框、对话框、进度条等。这些控件都经过了开发者的精心设计和优化,可以在Delphi应用程序中快速集成和使用。 使用JCL控件不仅可以提高开发效率,还可以避免重复造轮子的问题。JCL控件具有良好的文档和示例,可以帮助开发者快速上手并了解如何使用这些控件。此外,JCL控件是开源项目,开发者可以根据自己的需求进行自定义和扩展,满足特定的项目需求。 总之,Delphi JCL控件是一个非常有用的组件库,可以扩展和增强Delphi开发环境的功能。开发者可以借助JCL控件,提高开发效率,减少重复工作,并开发出更加高效和功能丰富的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值