-
Notifications
You must be signed in to change notification settings - Fork 0
/
uDMUtil.pas
59 lines (49 loc) · 1.37 KB
/
uDMUtil.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
unit uDMUtil;
interface
uses
System.SysUtils, System.Classes, Vcl.AppEvnts, Vcl.forms, inifiles;
type
TDMUtil = class(TDataModule)
ApplicationEvents1: TApplicationEvents;
procedure ApplicationEvents1Exception(Sender: TObject; E: Exception);
private
{ Private declarations }
public
{ Public declarations }
procedure ExceptionLogger(E: Exception; s: string);
end;
var
DMUtil: TDMUtil;
implementation
{ %CLASSGROUP 'Vcl.Controls.TControl' }
{$R *.dfm}
procedure TDMUtil.ExceptionLogger(E: Exception; s: string);
var
ErrorLogFileName: string;
ErrorFile: TextFile;
ErrorData: string;
begin
ErrorLogFileName := ChangeFileExt(Application.ExeName, '.error.log');
AssignFile(ErrorFile, ErrorLogFileName);
// either create an error log file, or append to an existing one
if FileExists(ErrorLogFileName) then
Append(ErrorFile)
else
Rewrite(ErrorFile);
try
// add the current date/time and the exception message to the log
if E <> nil then
ErrorData := Format('%s : %s - %s', [DateTimeToStr(Now), E.ClassName,
E.Message + '[' + s + ']']);
if (s <> '') and (E = nil) then
ErrorData := DateTimeToStr(Now)+': '+s;
WriteLn(ErrorFile, ErrorData);
finally
CloseFile(ErrorFile)
end;
end;
procedure TDMUtil.ApplicationEvents1Exception(Sender: TObject; E: Exception);
begin
ExceptionLogger(E, 'Global');
end;
end.