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);
46 procedure e_WriteStackTrace (const msg
: AnsiString);
50 e_WriteToStdOut
: Boolean = False;
64 driverInited
: Boolean = false;
67 function DecodeIPV4 (ip
: LongWord): string;
69 Result
:= Format('%d.%d.%d.%d', [ip
and $FF, (ip
shr 8) and $FF, (ip
shr 16) and $FF, (ip
shr 24)]);
73 function consoleAllow (const s
: String): Boolean;
76 if Pos('[Chat] ', s
) = 1 then
82 procedure e_WriteLog (TextLine
: String; RecordCategory
: TMsgType
; WriteTime
: Boolean=True);
84 e_LogWritefln('%s', [TextLine
], RecordCategory
, WriteTime
, consoleAllow(TextLine
));
88 procedure e_LogWriteln (const s
: AnsiString; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true);
90 e_LogWritefln('%s', [s
], category
, writeTime
, consoleAllow(s
));
94 // returns formatted string if `writerCB` is `nil`, empty string otherwise
95 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
96 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
98 procedure conwriter (constref buf
; len
: SizeUInt
);
107 if (len
< 1) then exit
;
111 cstr
:= GetMem(len
+ 1);
112 for slen
:= 0 to len
- 1 do
113 cstr
[slen
] := Chr(b
[slen
]);
121 if (len
> 255) then slen
:= 255 else slen
:= Integer(len
);
122 Move(b
^, ss
[1], slen
);
123 ss
[0] := AnsiChar(slen
);
133 xlogFileOpened
: Boolean = false;
134 xlogPrefix
: AnsiString;
135 xlogLastWasEOL
: Boolean = false;
136 xlogWantSpace
: Boolean = false;
137 xlogSlowAndSafe
: Boolean = false;
140 procedure e_SetSafeSlowLog (slowAndSafe
: Boolean);
142 xlogSlowAndSafe
:= slowAndSafe
;
143 if xlogSlowAndSafe
and xlogFileOpened
then
146 xlogFileOpened
:= false;
151 procedure logwriter (constref buf
; len
: SizeUInt
);
157 if (len
< 1) then exit
;
159 if xlogLastWasEOL
then
161 write(xlogFile
, xlogPrefix
);
162 xlogLastWasEOL
:= false;
163 xlogWantSpace
:= true;
168 while (slen
< len
) and (b
[slen
] <> 13) and (b
[slen
] <> 10) do Inc(slen
);
169 if (slen
> 255) then slen
:= 255;
173 if xlogWantSpace
then begin write(xlogFile
, ' '); xlogWantSpace
:= false; end;
174 Move(b
^, ss
[1], slen
);
175 ss
[0] := AnsiChar(slen
);
182 if (len
> 0) and ((b
[0] = 13) or (b
[0] = 10)) then
184 if (b
[0] = 13) then begin len
-= 1; b
+= 1; end;
185 if (len
> 0) and (b
[0] = 10) then begin len
-= 1; b
+= 1; end;
186 xlogLastWasEOL
:= false;
187 writeln(xlogFile
, '');
188 write(xlogFile
, xlogPrefix
);
194 procedure e_LogWritefln (const fmt
: AnsiString; args
: array of const; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true; writeConsole
: Boolean=true);
196 procedure xwrite (const s
: AnsiString);
198 if (Length(s
) = 0) then exit
;
199 logwriter(PAnsiChar(s
)^, Length(s
));
203 if driverInited
and (length(fmt
) > 0) and writeConsole
then
206 TMsgType
.Fatal
: write('FATAL: ');
207 TMsgType
.Warning
: write('WARNING: ');
209 formatstrf(fmt
, args
, conwriter
);
213 if (FileName
= '') then exit
;
215 if not xlogFileOpened
then
217 AssignFile(xlogFile
, FileName
);
219 if FileExists(FileName
) then Append(xlogFile
) else Rewrite(xlogFile
);
220 xlogFileOpened
:= true;
228 writeln(xlogFile
, '--- Log started at ', TimeToStr(Time
), ' ---');
229 FirstRecord
:= false;
233 if writeTime
then begin xlogPrefix
+= '['; xlogPrefix
+= TimeToStr(Time
); xlogPrefix
+= '] '; end;
235 TMsgType
.Fatal
: xlogPrefix
+= '!!!';
236 TMsgType
.Warning
: xlogPrefix
+= '! ';
237 TMsgType
.Notify
: xlogPrefix
+= '***';
239 xlogLastWasEOL
:= true; // to output prefix
240 xlogWantSpace
:= true; // after prefix
241 formatstrf(fmt
, args
, logwriter
);
242 if not xlogLastWasEOL
then writeln(xlogFile
, '') else writeln(xlogFile
, xlogPrefix
);
244 if xlogSlowAndSafe
and xlogFileOpened
then
247 xlogFileOpened
:= false;
250 //if fopened then CloseFile(xlogFile);
254 procedure e_InitLog (fFileName
: String; fWriteMode
: TWriteMode
);
256 if xlogFileOpened
then CloseFile(xlogFile
);
257 xlogFileOpened
:= false;
258 FileName
:= fFileName
;
259 if (fWriteMode
= TWriteMode
.WM_NEWFILE
) then
262 if FileExists(FileName
) then DeleteFile(FileName
);
271 procedure e_WriteStackTrace (const msg
: AnsiString);
275 e_LogWriteln(msg
, TMsgType
.Fatal
);
276 if (Length(FileName
) > 0) then
278 if xlogFileOpened
then CloseFile(xlogFile
);
279 xlogFileOpened
:= false;
280 AssignFile(tfo
, FileName
);
282 if (IOResult
<> 0) then Rewrite(tfo
);
283 if (IOResult
= 0) then begin writeln(tfo
, '====================='); DumpExceptionBackTrace(tfo
); CloseFile(tfo
); end;
288 procedure e_DeinitLog ();
290 if xlogFileOpened
then CloseFile(xlogFile
);
291 xlogFileOpened
:= false;
295 // ////////////////////////////////////////////////////////////////////////// //
296 (* Write/WriteLn driver *)
300 // TAB: tab space = 4
302 // userData[1]: current x (for tabs)
303 // userData[2]: #13 was eaten, we should skip next #10
306 TDevFunc
= function (var f
: TTextRec
): Integer;
313 procedure ProcessOutput (var tf
: TTextRec
; buf
: PChar; count
: Integer);
320 x
:= tf
.userData
[udX
];
321 wcr
:= (tf
.userData
[udWasCR
] <> 0);
324 // look for some special char
330 if (ch
= #13) or (ch
= #10) or (ch
= #9) or (ch
= #8) then break
;
340 cbufPutChars(buf
, f
);
346 // process special chars
363 if (ch
= #13) or (ch
= #10) then
368 if not wcr
or (ch
<> #10) then
377 tf
.userData
[udX
] := x
;
378 tf
.userData
[udWasCR
] := ord(wcr
);
382 function DevOpen (var f
: TTextRec
): Integer;
384 f
.userData
[udX
] := 0;
385 f
.userData
[udWasCR
] := 0;
391 function DevInOut (var f
: TTextRec
): Integer;
397 buf
:= Pointer(f
.BufPtr
);
399 if sz
> 0 then ProcessOutput(f
, buf
, sz
);
404 function DevFlush (var f
: TTextRec
): Integer;
406 result
:= DevInOut(f
);
409 function DevClose (var f
: TTextRec
): Integer;
415 procedure e_InitWritelnDriver ();
417 if not driverInited
then
419 driverInited
:= true;
420 with TTextRec(output
) do
425 BufSize
:= SizeOf(Buffer
);
428 OpenFunc
:= @DevOpen
;
429 InOutFunc
:= @DevInOut
;
430 FlushFunc
:= @DevFlush
;
431 CloseFunc
:= @DevClose
;
440 //e_InitWritelnDriver();