DEADSOFTWARE

1a2499060453086ace60781d3c3b85e7fabd995d
[d2df-sdl.git] / src / engine / e_log.pas
1 {$MODE DELPHI}
2 {$R-}
3 unit e_log;
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 function DecodeIPV4(ip: LongWord): string;
19 var
20 e_WriteToStdOut: Boolean = False;
22 implementation
24 var
25 FirstRecord: Boolean;
26 FileName: String;
28 { TLog }
30 function DecodeIPV4(ip: LongWord): string;
31 begin
32 Result := Format('%d.%d.%d.%d', [ip and $FF, (ip shr 8) and $FF, (ip shr 16) and $FF, (ip shr 24)]);
33 end;
35 procedure e_WriteLog(TextLine: String; RecordCategory: TRecordCategory;
36 WriteTime: Boolean = True);
37 var
38 LogFile: TextFile;
39 Prefix: ShortString = '';
40 OutStr: String;
41 begin
42 if FileName = '' then Exit;
44 Assign(LogFile, FileName);
45 try
46 if FileExists(FileName) then
47 Append(LogFile)
48 else
49 Rewrite(LogFile);
50 try
51 if FirstRecord then
52 begin
53 Writeln(LogFile, '--- Log started at '+TimeToStr(Time)+' ---');
54 FirstRecord := False;
55 end;
56 case RecordCategory of
57 MSG_FATALERROR: Prefix := '!!!';
58 MSG_WARNING: Prefix := '! ';
59 MSG_NOTIFY: Prefix := '***';
60 end;
61 if WriteTime then
62 OutStr := '['+TimeToStr(Time)+'] '+Prefix+' '+TextLine
63 else
64 OutStr := Prefix+' '+TextLine;
65 Writeln(LogFile, OutStr);
66 if e_WriteToStdOut then
67 Writeln(OutStr);
68 finally
69 Close(LogFile);
70 end;
71 except // sorry
72 end;
73 end;
75 procedure e_InitLog(fFileName: String; fWriteMode: TWriteMode);
76 begin
77 FileName := fFileName;
78 if fWriteMode = WM_NEWFILE then
79 begin
80 try
81 if FileExists(FileName) then DeleteFile(FileName);
82 except // sorry
83 end;
84 end;
85 FirstRecord := True;
86 end;
88 end.