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, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE ../shared/a_modes.inc}
26 TWriteMode
= (WM_NEWFILE
, WM_OLDFILE
);
27 TMsgType
= (Fatal
, Warning
, Notify
);
30 procedure e_InitLog (fFileName
: String; fWriteMode
: TWriteMode
);
31 procedure e_DeinitLog ();
33 procedure e_SetSafeSlowLog (slowAndSafe
: Boolean);
35 procedure e_WriteLog (TextLine
: String; RecordCategory
: TMsgType
; WriteTime
: Boolean=True);
37 function DecodeIPV4 (ip
: LongWord): string;
39 // start Write/WriteLn driver. it will write everything to cbuf.
40 procedure e_InitWritelnDriver ();
42 procedure e_LogWritefln (const fmt
: AnsiString; args
: array of const; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true; writeConsole
: Boolean=true);
43 procedure e_LogWriteln (const s
: AnsiString; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true);
45 procedure e_WriteStackTrace (const msg
: AnsiString);
58 driverInited
: Boolean = false;
61 function DecodeIPV4 (ip
: LongWord): string;
63 Result
:= Format('%d.%d.%d.%d', [ip
and $FF, (ip
shr 8) and $FF, (ip
shr 16) and $FF, (ip
shr 24)]);
67 function consoleAllow (const s
: String): Boolean;
70 if Pos('[Chat] ', s
) = 1 then
76 procedure e_WriteLog (TextLine
: String; RecordCategory
: TMsgType
; WriteTime
: Boolean=True);
78 e_LogWritefln('%s', [TextLine
], RecordCategory
, WriteTime
, consoleAllow(TextLine
));
82 procedure e_LogWriteln (const s
: AnsiString; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true);
84 e_LogWritefln('%s', [s
], category
, writeTime
, consoleAllow(s
));
88 // returns formatted string if `writerCB` is `nil`, empty string otherwise
89 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
90 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
92 procedure conwriter (constref buf
; len
: SizeUInt
);
101 if (len
< 1) then exit
;
105 cstr
:= GetMem(len
+ 1);
106 for slen
:= 0 to len
- 1 do
107 cstr
[slen
] := Chr(b
[slen
]);
115 if (len
> 255) then slen
:= 255 else slen
:= Integer(len
);
116 Move(b
^, ss
[1], slen
);
117 ss
[0] := AnsiChar(slen
);
127 xlogFileOpened
: Boolean = false;
128 xlogPrefix
: AnsiString;
129 xlogLastWasEOL
: Boolean = false;
130 xlogWantSpace
: Boolean = false;
131 xlogSlowAndSafe
: Boolean = false;
134 procedure e_SetSafeSlowLog (slowAndSafe
: Boolean);
136 xlogSlowAndSafe
:= slowAndSafe
;
137 if xlogSlowAndSafe
and xlogFileOpened
then
140 xlogFileOpened
:= false;
145 procedure logwriter (constref buf
; len
: SizeUInt
);
151 if (len
< 1) then exit
;
153 if xlogLastWasEOL
then
155 write(xlogFile
, xlogPrefix
);
156 xlogLastWasEOL
:= false;
157 xlogWantSpace
:= true;
162 while (slen
< len
) and (b
[slen
] <> 13) and (b
[slen
] <> 10) do Inc(slen
);
163 if (slen
> 255) then slen
:= 255;
167 if xlogWantSpace
then begin write(xlogFile
, ' '); xlogWantSpace
:= false; end;
168 Move(b
^, ss
[1], slen
);
169 ss
[0] := AnsiChar(slen
);
176 if (len
> 0) and ((b
[0] = 13) or (b
[0] = 10)) then
178 if (b
[0] = 13) then begin len
-= 1; b
+= 1; end;
179 if (len
> 0) and (b
[0] = 10) then begin len
-= 1; b
+= 1; end;
180 xlogLastWasEOL
:= false;
181 writeln(xlogFile
, '');
182 write(xlogFile
, xlogPrefix
);
188 procedure e_LogWritefln (const fmt
: AnsiString; args
: array of const; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true; writeConsole
: Boolean=true);
190 procedure xwrite (const s
: AnsiString);
192 if (Length(s
) = 0) then exit
;
193 logwriter(PAnsiChar(s
)^, Length(s
));
197 if driverInited
and (length(fmt
) > 0) and writeConsole
then
200 TMsgType
.Fatal
: write('FATAL: ');
201 TMsgType
.Warning
: write('WARNING: ');
203 formatstrf(fmt
, args
, conwriter
);
207 if (FileName
= '') then exit
;
209 if not xlogFileOpened
then
211 AssignFile(xlogFile
, FileName
);
213 if FileExists(FileName
) then Append(xlogFile
) else Rewrite(xlogFile
);
214 xlogFileOpened
:= true;
222 writeln(xlogFile
, '--- Log started at ', TimeToStr(Time
), ' ---');
223 FirstRecord
:= false;
227 if writeTime
then begin xlogPrefix
+= '['; xlogPrefix
+= TimeToStr(Time
); xlogPrefix
+= '] '; end;
229 TMsgType
.Fatal
: xlogPrefix
+= '!!!';
230 TMsgType
.Warning
: xlogPrefix
+= '! ';
231 TMsgType
.Notify
: xlogPrefix
+= '***';
233 xlogLastWasEOL
:= true; // to output prefix
234 xlogWantSpace
:= true; // after prefix
235 formatstrf(fmt
, args
, logwriter
);
236 if not xlogLastWasEOL
then writeln(xlogFile
, '') else writeln(xlogFile
, xlogPrefix
);
238 if xlogSlowAndSafe
and xlogFileOpened
then
241 xlogFileOpened
:= false;
244 //if fopened then CloseFile(xlogFile);
248 procedure e_InitLog (fFileName
: String; fWriteMode
: TWriteMode
);
250 if xlogFileOpened
then CloseFile(xlogFile
);
251 xlogFileOpened
:= false;
252 FileName
:= fFileName
;
253 if (fWriteMode
= TWriteMode
.WM_NEWFILE
) then
256 if FileExists(FileName
) then DeleteFile(FileName
);
265 procedure e_WriteStackTrace (const msg
: AnsiString);
269 e_LogWriteln(msg
, TMsgType
.Fatal
);
270 if (Length(FileName
) > 0) then
272 if xlogFileOpened
then CloseFile(xlogFile
);
273 xlogFileOpened
:= false;
274 AssignFile(tfo
, FileName
);
276 if (IOResult
<> 0) then Rewrite(tfo
);
277 if (IOResult
= 0) then begin writeln(tfo
, '====================='); DumpExceptionBackTrace(tfo
); CloseFile(tfo
); end;
282 procedure e_DeinitLog ();
284 if xlogFileOpened
then CloseFile(xlogFile
);
285 xlogFileOpened
:= false;
289 // ////////////////////////////////////////////////////////////////////////// //
290 (* Write/WriteLn driver *)
294 // TAB: tab space = 4
296 // userData[1]: current x (for tabs)
297 // userData[2]: #13 was eaten, we should skip next #10
300 TDevFunc
= function (var f
: TTextRec
): Integer;
307 procedure ProcessOutput (var tf
: TTextRec
; buf
: PChar; count
: Integer);
314 x
:= tf
.userData
[udX
];
315 wcr
:= (tf
.userData
[udWasCR
] <> 0);
318 // look for some special char
324 if (ch
= #13) or (ch
= #10) or (ch
= #9) or (ch
= #8) then break
;
334 cbufPutChars(buf
, f
);
340 // process special chars
357 if (ch
= #13) or (ch
= #10) then
362 if not wcr
or (ch
<> #10) then
371 tf
.userData
[udX
] := x
;
372 tf
.userData
[udWasCR
] := ord(wcr
);
376 function DevOpen (var f
: TTextRec
): Integer;
378 f
.userData
[udX
] := 0;
379 f
.userData
[udWasCR
] := 0;
385 function DevInOut (var f
: TTextRec
): Integer;
391 buf
:= Pointer(f
.BufPtr
);
393 if sz
> 0 then ProcessOutput(f
, buf
, sz
);
398 function DevFlush (var f
: TTextRec
): Integer;
400 result
:= DevInOut(f
);
403 function DevClose (var f
: TTextRec
): Integer;
409 procedure e_InitWritelnDriver ();
411 if not driverInited
then
413 driverInited
:= true;
414 with TTextRec(output
) do
419 BufSize
:= SizeOf(Buffer
);
422 OpenFunc
:= @DevOpen
;
423 InOutFunc
:= @DevInOut
;
424 FlushFunc
:= @DevFlush
;
425 CloseFunc
:= @DevClose
;
434 //e_InitWritelnDriver();