您的位置:首页 > 编程语言 > Delphi

delphi 简单的Bug报告类

2014-04-17 17:11 274 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: