delphi Bug 보고서 클래스

1435 단어
unit uBugReport;

interface

uses
  Windows, SysUtils, SyncObjs;

  procedure ReportBug(pszBugStr: PChar); stdcall;

implementation

var
  LogFileLock: TCriticalSection;

function GetAppRoot: string;
var
  path: string;
begin
  SetLength(path, MAX_PATH);
  SetLength(path, GetModuleFileName(HInstance, PChar(path), MAX_PATH));
  path := ExtractFilePath(path);
  Result := StringReplace(path, 'bin\', '', [rfIgnoreCase]);
end;

procedure ReportBug(pszBugStr: PChar);
var
  strPath: string;
  strFileName: string;
  F: Text;
  S: string;
  dtNow: TDateTime;
begin
  try
    OutputDebugString(pszBugStr);

    LogFileLock.Enter();
    try
      dtNow := Now();

      strPath := GetAppRoot() + 'Logs\';
      strFileName := strPath + FormatDateTime('YYYY-MM-DD', dtNow) + '.log';
      if not DirectoryExists(strPath) then ForceDirectories(strPath);

      AssignFile(F, strFileName);
      try
        if FileExists(strFileName) then
          Append(F)
        else
          Rewrite(F);

        S := FormatDateTime('YYYY-MM-DD HH:MM:SS', dtNow) + ' ' + StrPas(pszBugStr);
        Writeln(F, S);
      finally
        CloseFile(F);
      end;
    finally
      LogFileLock.Leave();
    end;
  except
  end;
end;

initialization
  IsMultiThread := True;
  LogFileLock := TCriticalSection.Create();

finalization
  FreeAndNil(LogFileLock);

end.

좋은 웹페이지 즐겨찾기