1 (* Copyright (C) Doom 2D: Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
27 TWriteMode
= (WM_NEWFILE
, WM_OLDFILE
);
28 TMsgType
= (Fatal
, Warning
, Notify
);
31 procedure e_InitLog (fFileName
: String; fWriteMode
: TWriteMode
);
32 procedure e_DeinitLog ();
34 procedure e_SetSafeSlowLog (slowAndSafe
: Boolean);
36 procedure e_WriteLog (TextLine
: String; RecordCategory
: TMsgType
; WriteTime
: Boolean=True);
38 function DecodeIPV4 (ip
: LongWord): string;
40 // start Write/WriteLn driver. it will write everything to cbuf.
41 procedure e_InitWritelnDriver ();
43 procedure e_LogWritefln (const fmt
: AnsiString; args
: array of const; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true);
44 procedure e_LogWriteln (const s
: AnsiString; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true);
47 procedure e_WriteStackTrace (const msg
: AnsiString);
51 e_WriteToStdOut
: Boolean = False;
62 driverInited
: Boolean = false;
65 function DecodeIPV4 (ip
: LongWord): string;
67 Result
:= Format('%d.%d.%d.%d', [ip
and $FF, (ip
shr 8) and $FF, (ip
shr 16) and $FF, (ip
shr 24)]);
71 procedure e_WriteLog (TextLine
: String; RecordCategory
: TMsgType
; WriteTime
: Boolean=True);
73 e_LogWritefln('%s', [TextLine
], RecordCategory
, WriteTime
);
77 procedure e_LogWriteln (const s
: AnsiString; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true);
79 e_LogWritefln('%s', [s
], category
, writeTime
);
83 // returns formatted string if `writerCB` is `nil`, empty string otherwise
84 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
85 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
87 procedure conwriter (constref buf
; len
: SizeUInt
);
96 if (len
< 1) then exit
;
100 cstr
:= GetMem(len
+ 1);
101 for slen
:= 0 to len
- 1 do
102 cstr
[slen
] := Chr(b
[slen
]);
110 if (len
> 255) then slen
:= 255 else slen
:= Integer(len
);
111 Move(b
^, ss
[1], slen
);
112 ss
[0] := AnsiChar(slen
);
122 xlogFileOpened
: Boolean = false;
123 xlogPrefix
: AnsiString;
124 xlogLastWasEOL
: Boolean = false;
125 xlogWantSpace
: Boolean = false;
126 xlogSlowAndSafe
: Boolean = false;
129 procedure e_SetSafeSlowLog (slowAndSafe
: Boolean);
131 xlogSlowAndSafe
:= slowAndSafe
;
132 if xlogSlowAndSafe
and xlogFileOpened
then
135 xlogFileOpened
:= false;
140 procedure logwriter (constref buf
; len
: SizeUInt
);
146 if (len
< 1) then exit
;
148 if xlogLastWasEOL
then
150 write(xlogFile
, xlogPrefix
);
151 xlogLastWasEOL
:= false;
152 xlogWantSpace
:= true;
157 while (slen
< len
) and (b
[slen
] <> 13) and (b
[slen
] <> 10) do Inc(slen
);
158 if (slen
> 255) then slen
:= 255;
162 if xlogWantSpace
then begin write(xlogFile
, ' '); xlogWantSpace
:= false; end;
163 Move(b
^, ss
[1], slen
);
164 ss
[0] := AnsiChar(slen
);
171 if (len
> 0) and ((b
[0] = 13) or (b
[0] = 10)) then
173 if (b
[0] = 13) then begin len
-= 1; b
+= 1; end;
174 if (len
> 0) and (b
[0] = 10) then begin len
-= 1; b
+= 1; end;
175 xlogLastWasEOL
:= false;
176 writeln(xlogFile
, '');
177 write(xlogFile
, xlogPrefix
);
183 procedure e_LogWritefln (const fmt
: AnsiString; args
: array of const; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true);
185 procedure xwrite (const s
: AnsiString);
187 if (Length(s
) = 0) then exit
;
188 logwriter(PAnsiChar(s
)^, Length(s
));
192 if driverInited
and (length(fmt
) > 0) then
195 TMsgType
.Fatal
: write('FATAL: ');
196 TMsgType
.Warning
: write('WARNING: ');
198 formatstrf(fmt
, args
, conwriter
);
202 if (FileName
= '') then exit
;
204 if not xlogFileOpened
then
206 AssignFile(xlogFile
, FileName
);
208 if FileExists(FileName
) then Append(xlogFile
) else Rewrite(xlogFile
);
209 xlogFileOpened
:= true;
217 writeln(xlogFile
, '--- Log started at ', TimeToStr(Time
), ' ---');
218 FirstRecord
:= false;
222 if writeTime
then begin xlogPrefix
+= '['; xlogPrefix
+= TimeToStr(Time
); xlogPrefix
+= '] '; end;
224 TMsgType
.Fatal
: xlogPrefix
+= '!!!';
225 TMsgType
.Warning
: xlogPrefix
+= '! ';
226 TMsgType
.Notify
: xlogPrefix
+= '***';
228 xlogLastWasEOL
:= true; // to output prefix
229 xlogWantSpace
:= true; // after prefix
230 formatstrf(fmt
, args
, logwriter
);
231 if not xlogLastWasEOL
then writeln(xlogFile
, '') else writeln(xlogFile
, xlogPrefix
);
233 if xlogSlowAndSafe
and xlogFileOpened
then
236 xlogFileOpened
:= false;
239 //if fopened then CloseFile(xlogFile);
243 procedure e_InitLog (fFileName
: String; fWriteMode
: TWriteMode
);
245 if xlogFileOpened
then CloseFile(xlogFile
);
246 xlogFileOpened
:= false;
247 FileName
:= fFileName
;
248 if (fWriteMode
= TWriteMode
.WM_NEWFILE
) then
251 if FileExists(FileName
) then DeleteFile(FileName
);
260 procedure e_WriteStackTrace (const msg
: AnsiString);
264 e_LogWriteln(msg
, TMsgType
.Fatal
);
265 if (Length(FileName
) > 0) then
267 if xlogFileOpened
then CloseFile(xlogFile
);
268 xlogFileOpened
:= false;
269 AssignFile(tfo
, FileName
);
271 if (IOResult
<> 0) then Rewrite(tfo
);
272 if (IOResult
= 0) then begin writeln(tfo
, '====================='); DumpExceptionBackTrace(tfo
); CloseFile(tfo
); end;
277 procedure e_DeinitLog ();
279 if xlogFileOpened
then CloseFile(xlogFile
);
280 xlogFileOpened
:= false;
284 // ////////////////////////////////////////////////////////////////////////// //
285 (* Write/WriteLn driver *)
289 // TAB: tab space = 4
291 // userData[1]: current x (for tabs)
292 // userData[2]: #13 was eaten, we should skip next #10
295 TDevFunc
= function (var f
: TTextRec
): Integer;
302 procedure ProcessOutput (var tf
: TTextRec
; buf
: PChar; count
: Integer);
309 x
:= tf
.userData
[udX
];
310 wcr
:= (tf
.userData
[udWasCR
] <> 0);
313 // look for some special char
319 if (ch
= #13) or (ch
= #10) or (ch
= #9) or (ch
= #8) then break
;
329 cbufPutChars(buf
, f
);
335 // process special chars
352 if (ch
= #13) or (ch
= #10) then
357 if not wcr
or (ch
<> #10) then
366 tf
.userData
[udX
] := x
;
367 tf
.userData
[udWasCR
] := ord(wcr
);
371 function DevOpen (var f
: TTextRec
): Integer;
373 f
.userData
[udX
] := 0;
374 f
.userData
[udWasCR
] := 0;
380 function DevInOut (var f
: TTextRec
): Integer;
386 buf
:= Pointer(f
.BufPtr
);
388 if sz
> 0 then ProcessOutput(f
, buf
, sz
);
393 function DevFlush (var f
: TTextRec
): Integer;
395 result
:= DevInOut(f
);
398 function DevClose (var f
: TTextRec
): Integer;
404 procedure e_InitWritelnDriver ();
406 if not driverInited
then
408 driverInited
:= true;
409 with TTextRec(output
) do
414 BufSize
:= SizeOf(Buffer
);
417 OpenFunc
:= @DevOpen
;
418 InOutFunc
:= @DevInOut
;
419 FlushFunc
:= @DevFlush
;
420 CloseFunc
:= @DevClose
;
429 //e_InitWritelnDriver();