added headless mode
[d2df-sdl.git] / src / engine / e_log.pas
blobe524c1a1a9f522e70191ef1543af9d132912e254
1 {$R-}
2 unit e_log;
4 interface
6 uses
7 SysUtils;
9 type
10 TWriteMode=(WM_NEWFILE, WM_OLDFILE);
11 TRecordCategory=(MSG_FATALERROR, MSG_WARNING, MSG_NOTIFY);
13 procedure e_InitLog(fFileName: String; fWriteMode: TWriteMode);
14 procedure e_WriteLog(TextLine: String; RecordCategory: TRecordCategory;
15 WriteTime: Boolean = True);
16 function DecodeIPV4(ip: LongWord): string;
18 var
19 e_WriteToStdOut: Boolean = False;
21 implementation
23 var
24 FirstRecord: Boolean;
25 FileName: String;
27 { TLog }
29 function DecodeIPV4(ip: LongWord): string;
30 begin
31 Result := Format('%d.%d.%d.%d', [ip and $FF, (ip shr 8) and $FF, (ip shr 16) and $FF, (ip shr 24)]);
32 end;
34 procedure e_WriteLog(TextLine: String; RecordCategory: TRecordCategory;
35 WriteTime: Boolean = True);
36 var
37 LogFile: TextFile;
38 Prefix: ShortString = '';
39 OutStr: String;
40 begin
41 if FileName = '' then Exit;
43 Assign(LogFile, FileName);
44 try
45 if FileExists(FileName) then
46 Append(LogFile)
47 else
48 Rewrite(LogFile);
49 try
50 if FirstRecord then
51 begin
52 Writeln(LogFile, '--- Log started at '+TimeToStr(Time)+' ---');
53 FirstRecord := False;
54 end;
55 case RecordCategory of
56 MSG_FATALERROR: Prefix := '!!!';
57 MSG_WARNING: Prefix := '! ';
58 MSG_NOTIFY: Prefix := '***';
59 end;
60 if WriteTime then
61 OutStr := '['+TimeToStr(Time)+'] '+Prefix+' '+TextLine
62 else
63 OutStr := Prefix+' '+TextLine;
64 Writeln(LogFile, OutStr);
65 if e_WriteToStdOut then
66 Writeln(OutStr);
67 finally
68 Close(LogFile);
69 end;
70 except // sorry
71 end;
72 end;
74 procedure e_InitLog(fFileName: String; fWriteMode: TWriteMode);
75 begin
76 FileName := fFileName;
77 if fWriteMode = WM_NEWFILE then
78 begin
79 try
80 if FileExists(FileName) then DeleteFile(FileName);
81 except // sorry
82 end;
83 end;
84 FirstRecord := True;
85 end;
87 end.