DEADSOFTWARE

Fix error file not open on win32; Added stacktrace logging
[d2df-editor.git] / src / engine / e_log.pas
1 unit e_log;
3 {$INCLUDE ../shared/a_modes.inc}
5 interface
7 uses
8 SysUtils;
10 type
11 TWriteMode=(WM_NEWFILE, WM_OLDFILE);
12 TRecordCategory=(MSG_FATALERROR, MSG_WARNING, MSG_NOTIFY);
14 procedure e_InitLog(fFileName: String; fWriteMode: TWriteMode);
15 procedure e_WriteLog(TextLine: String; RecordCategory: TRecordCategory;
16 WriteTime: Boolean = True);
17 procedure e_WriteStackTrace (const msg: AnsiString);
18 function DecodeIPV4(ip: LongWord): string;
20 implementation
22 var
23 FirstRecord: Boolean;
24 FileName: String;
26 { TLog }
28 function DecodeIPV4(ip: LongWord): string;
29 begin
30 Result := Format('%d.%d.%d.%d', [ip and $FF, (ip shr 8) and $FF, (ip shr 16) and $FF, (ip shr 24)]);
31 end;
33 procedure e_WriteLog(TextLine: String; RecordCategory: TRecordCategory;
34 WriteTime: Boolean = True);
35 var
36 LogFile: TextFile;
37 Prefix: ShortString;
38 begin
39 if FileName = '' then Exit;
41 Assign(LogFile, FileName);
42 if FileExists(FileName) then
43 Append(LogFile)
44 else
45 Rewrite(LogFile);
46 if FirstRecord then
47 begin
48 Writeln(LogFile, '--- Log started at '+TimeToStr(Time)+' ---');
49 FirstRecord := False;
50 end;
51 case RecordCategory of
52 MSG_FATALERROR: Prefix := '!!!';
53 MSG_WARNING: Prefix := '! ';
54 MSG_NOTIFY: Prefix := '***';
55 end;
56 if WriteTime then
57 Writeln(LogFile, '['+TimeToStr(Time)+'] '+Prefix+' '+TextLine)
58 else
59 Writeln(LogFile, Prefix+' '+TextLine);
60 Close(LogFile);
61 end;
63 {$I-}
64 procedure e_WriteStackTrace (const msg: AnsiString);
65 var LogFile: TextFile;
66 begin
67 e_WriteLog(msg, TRecordCategory.MSG_FATALERROR);
69 Assign(LogFile, FileName);
70 if FileExists(FileName) then
71 Append(LogFile)
72 else
73 Rewrite(LogFile);
75 writeln(LogFile, '=====================');
76 DumpExceptionBackTrace(LogFile);
78 Close(LogFile);
79 end;
81 procedure e_InitLog(fFileName: String; fWriteMode: TWriteMode);
82 begin
83 FileName := fFileName;
84 if fWriteMode = WM_NEWFILE then
85 if FileExists(FileName) then
86 DeleteFile(FileName);
87 FirstRecord := True;
88 end;
90 end.