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; writeConsole
: 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;
65 driverInited
: Boolean = false;
68 function DecodeIPV4 (ip
: LongWord): string;
70 Result
:= Format('%d.%d.%d.%d', [ip
and $FF, (ip
shr 8) and $FF, (ip
shr 16) and $FF, (ip
shr 24)]);
74 function consoleAllow (const s
: String): Boolean;
77 if Pos('[Chat] ', s
) = 1 then
83 procedure e_WriteLog (TextLine
: String; RecordCategory
: TMsgType
; WriteTime
: Boolean=True);
85 e_LogWritefln('%s', [TextLine
], RecordCategory
, WriteTime
, consoleAllow(TextLine
));
89 procedure e_LogWriteln (const s
: AnsiString; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true);
91 e_LogWritefln('%s', [s
], category
, writeTime
, consoleAllow(s
));
95 // returns formatted string if `writerCB` is `nil`, empty string otherwise
96 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
97 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
99 procedure conwriter (constref buf
; len
: SizeUInt
);
108 if (len
< 1) then exit
;
112 cstr
:= GetMem(len
+ 1);
113 for slen
:= 0 to len
- 1 do
114 cstr
[slen
] := Chr(b
[slen
]);
122 if (len
> 255) then slen
:= 255 else slen
:= Integer(len
);
123 Move(b
^, ss
[1], slen
);
124 ss
[0] := AnsiChar(slen
);
134 xlogFileOpened
: Boolean = false;
135 xlogPrefix
: AnsiString;
136 xlogLastWasEOL
: Boolean = false;
137 xlogWantSpace
: Boolean = false;
138 xlogSlowAndSafe
: Boolean = false;
141 procedure e_SetSafeSlowLog (slowAndSafe
: Boolean);
143 xlogSlowAndSafe
:= slowAndSafe
;
144 if xlogSlowAndSafe
and xlogFileOpened
then
147 xlogFileOpened
:= false;
152 procedure logwriter (constref buf
; len
: SizeUInt
);
158 if (len
< 1) then exit
;
160 if xlogLastWasEOL
then
162 write(xlogFile
, xlogPrefix
);
163 xlogLastWasEOL
:= false;
164 xlogWantSpace
:= true;
169 while (slen
< len
) and (b
[slen
] <> 13) and (b
[slen
] <> 10) do Inc(slen
);
170 if (slen
> 255) then slen
:= 255;
174 if xlogWantSpace
then begin write(xlogFile
, ' '); xlogWantSpace
:= false; end;
175 Move(b
^, ss
[1], slen
);
176 ss
[0] := AnsiChar(slen
);
183 if (len
> 0) and ((b
[0] = 13) or (b
[0] = 10)) then
185 if (b
[0] = 13) then begin len
-= 1; b
+= 1; end;
186 if (len
> 0) and (b
[0] = 10) then begin len
-= 1; b
+= 1; end;
187 xlogLastWasEOL
:= false;
188 writeln(xlogFile
, '');
189 write(xlogFile
, xlogPrefix
);
195 procedure e_LogWritefln (const fmt
: AnsiString; args
: array of const; category
: TMsgType
=TMsgType
.Notify
; writeTime
: Boolean=true; writeConsole
: Boolean=true);
197 procedure xwrite (const s
: AnsiString);
199 if (Length(s
) = 0) then exit
;
200 logwriter(PAnsiChar(s
)^, Length(s
));
204 if driverInited
and (length(fmt
) > 0) and writeConsole
then
207 TMsgType
.Fatal
: write('FATAL: ');
208 TMsgType
.Warning
: write('WARNING: ');
210 formatstrf(fmt
, args
, conwriter
);
214 if (FileName
= '') then exit
;
216 if not xlogFileOpened
then
218 AssignFile(xlogFile
, FileName
);
220 if FileExists(FileName
) then Append(xlogFile
) else Rewrite(xlogFile
);
221 xlogFileOpened
:= true;
229 writeln(xlogFile
, '--- Log started at ', TimeToStr(Time
), ' ---');
230 FirstRecord
:= false;
234 if writeTime
then begin xlogPrefix
+= '['; xlogPrefix
+= TimeToStr(Time
); xlogPrefix
+= '] '; end;
236 TMsgType
.Fatal
: xlogPrefix
+= '!!!';
237 TMsgType
.Warning
: xlogPrefix
+= '! ';
238 TMsgType
.Notify
: xlogPrefix
+= '***';
240 xlogLastWasEOL
:= true; // to output prefix
241 xlogWantSpace
:= true; // after prefix
242 formatstrf(fmt
, args
, logwriter
);
243 if not xlogLastWasEOL
then writeln(xlogFile
, '') else writeln(xlogFile
, xlogPrefix
);
245 if xlogSlowAndSafe
and xlogFileOpened
then
248 xlogFileOpened
:= false;
251 //if fopened then CloseFile(xlogFile);
255 procedure e_InitLog (fFileName
: String; fWriteMode
: TWriteMode
);
257 if xlogFileOpened
then CloseFile(xlogFile
);
258 xlogFileOpened
:= false;
259 FileName
:= fFileName
;
260 if (fWriteMode
= TWriteMode
.WM_NEWFILE
) then
263 if FileExists(FileName
) then DeleteFile(FileName
);
272 procedure e_WriteStackTrace (const msg
: AnsiString);
276 e_LogWriteln(msg
, TMsgType
.Fatal
);
277 if (Length(FileName
) > 0) then
279 if xlogFileOpened
then CloseFile(xlogFile
);
280 xlogFileOpened
:= false;
281 AssignFile(tfo
, FileName
);
283 if (IOResult
<> 0) then Rewrite(tfo
);
284 if (IOResult
= 0) then begin writeln(tfo
, '====================='); DumpExceptionBackTrace(tfo
); CloseFile(tfo
); end;
289 procedure e_DeinitLog ();
291 if xlogFileOpened
then CloseFile(xlogFile
);
292 xlogFileOpened
:= false;
296 // ////////////////////////////////////////////////////////////////////////// //
297 (* Write/WriteLn driver *)
301 // TAB: tab space = 4
303 // userData[1]: current x (for tabs)
304 // userData[2]: #13 was eaten, we should skip next #10
307 TDevFunc
= function (var f
: TTextRec
): Integer;
314 procedure ProcessOutput (var tf
: TTextRec
; buf
: PChar; count
: Integer);
321 x
:= tf
.userData
[udX
];
322 wcr
:= (tf
.userData
[udWasCR
] <> 0);
325 // look for some special char
331 if (ch
= #13) or (ch
= #10) or (ch
= #9) or (ch
= #8) then break
;
341 cbufPutChars(buf
, f
);
347 // process special chars
364 if (ch
= #13) or (ch
= #10) then
369 if not wcr
or (ch
<> #10) then
378 tf
.userData
[udX
] := x
;
379 tf
.userData
[udWasCR
] := ord(wcr
);
383 function DevOpen (var f
: TTextRec
): Integer;
385 f
.userData
[udX
] := 0;
386 f
.userData
[udWasCR
] := 0;
392 function DevInOut (var f
: TTextRec
): Integer;
398 buf
:= Pointer(f
.BufPtr
);
400 if sz
> 0 then ProcessOutput(f
, buf
, sz
);
405 function DevFlush (var f
: TTextRec
): Integer;
407 result
:= DevInOut(f
);
410 function DevClose (var f
: TTextRec
): Integer;
416 procedure e_InitWritelnDriver ();
418 if not driverInited
then
420 driverInited
:= true;
421 with TTextRec(output
) do
426 BufSize
:= SizeOf(Buffer
);
429 OpenFunc
:= @DevOpen
;
430 InOutFunc
:= @DevInOut
;
431 FlushFunc
:= @DevFlush
;
432 CloseFunc
:= @DevClose
;
441 //e_InitWritelnDriver();