523c27f068de6ab6392058a12714edf8bda543ed
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 TRecordCategory
= (MSG_FATALERROR
, MSG_WARNING
, MSG_NOTIFY
);
31 procedure e_InitLog (fFileName
: String; fWriteMode
: TWriteMode
);
32 procedure e_DeinitLog ();
34 procedure e_WriteLog (TextLine
: String; RecordCategory
: TRecordCategory
; WriteTime
: Boolean=True);
36 function DecodeIPV4 (ip
: LongWord): string;
38 // start Write/WriteLn driver. it will write everything to cbuf.
39 procedure e_InitWritelnDriver ();
41 procedure e_LogWritefln (const fmt
: AnsiString; args
: array of const; category
: TRecordCategory
=MSG_NOTIFY
; writeTime
: Boolean=true);
45 e_WriteToStdOut
: Boolean = False;
56 driverInited
: Boolean = false;
59 function DecodeIPV4 (ip
: LongWord): string;
61 Result
:= Format('%d.%d.%d.%d', [ip
and $FF, (ip
shr 8) and $FF, (ip
shr 16) and $FF, (ip
shr 24)]);
65 procedure e_WriteLog (TextLine
: String; RecordCategory
: TRecordCategory
; WriteTime
: Boolean=True);
67 e_LogWritefln('%s', [TextLine
], RecordCategory
, WriteTime
);
72 Prefix: ShortString = '';
75 if driverInited and (length(TextLine) > 0) then
77 case RecordCategory of
78 MSG_FATALERROR: write('FATAL: ');
79 MSG_WARNING: write('WARNING: ');
84 if FileName = '' then Exit;
86 Assign(LogFile, FileName);
88 if FileExists(FileName) then
95 Writeln(LogFile, '--- Log started at '+TimeToStr(Time)+' ---');
98 case RecordCategory of
99 MSG_FATALERROR: Prefix := '!!!';
100 MSG_WARNING: Prefix := '! ';
101 MSG_NOTIFY: Prefix := '***';
104 OutStr := '['+TimeToStr(Time)+'] '+Prefix+' '+TextLine
106 OutStr := Prefix+' '+TextLine;
107 Writeln(LogFile, OutStr);
108 if e_WriteToStdOut then
119 // returns formatted string if `writerCB` is `nil`, empty string otherwise
120 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
121 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
123 procedure conwriter (constref buf
; len
: SizeUInt
);
129 if (len
< 1) then exit
;
133 if (len
> 255) then slen
:= 255 else slen
:= Integer(len
);
134 Move(b
^, ss
[1], len
);
135 ss
[0] := AnsiChar(slen
);
145 xlogFileOpened
: Boolean = false;
146 xlogPrefix
: AnsiString;
147 xlogLastWasEOL
: Boolean = false;
148 xlogWantSpace
: Boolean = false;
150 procedure logwriter (constref buf
; len
: SizeUInt
);
156 if (len
< 1) then exit
;
158 if xlogLastWasEOL
then
160 write(xlogFile
, xlogPrefix
);
161 xlogLastWasEOL
:= false;
162 xlogWantSpace
:= true;
167 while (slen
< len
) and (slen
< 255) and (b
[slen
] <> 13) and (b
[slen
] <> 10) do Inc(slen
);
171 if xlogWantSpace
then begin write(xlogFile
, ' '); xlogWantSpace
:= false; end;
172 Move(b
^, ss
[1], len
);
173 ss
[0] := AnsiChar(slen
);
180 if (len
> 0) and ((b
[0] = 13) or (b
[0] = 10)) then
182 if (len
> 1) and (b
[0] = 13) and (b
[1] = 10) then
192 xlogLastWasEOL
:= false;
193 writeln(xlogFile
, '');
194 write(xlogFile
, xlogPrefix
);
200 procedure e_LogWritefln (const fmt
: AnsiString; args
: array of const; category
: TRecordCategory
=MSG_NOTIFY
; writeTime
: Boolean=true);
202 procedure xwrite (const s
: AnsiString);
204 if (Length(s
) = 0) then exit
;
205 logwriter(PAnsiChar(s
)^, Length(s
));
209 if driverInited
and (length(fmt
) > 0) then
212 MSG_FATALERROR
: write('FATAL: ');
213 MSG_WARNING
: write('WARNING: ');
215 formatstrf(fmt
, args
, conwriter
);
219 if (FileName
= '') then exit
;
221 if not xlogFileOpened
then
223 AssignFile(xlogFile
, FileName
);
225 if FileExists(FileName
) then Append(xlogFile
) else Rewrite(xlogFile
);
226 xlogFileOpened
:= true;
234 writeln(xlogFile
, '--- Log started at ', TimeToStr(Time
), ' ---');
235 FirstRecord
:= false;
239 if writeTime
then begin xlogPrefix
+= '['; xlogPrefix
+= TimeToStr(Time
); xlogPrefix
+= '] '; end;
241 MSG_FATALERROR
: xlogPrefix
+= '!!!';
242 MSG_WARNING
: xlogPrefix
+= '! ';
243 MSG_NOTIFY
: xlogPrefix
+= '***';
245 xlogLastWasEOL
:= true; // to output prefix
246 xlogWantSpace
:= true; // after prefix
247 formatstrf(fmt
, args
, logwriter
);
248 if not xlogLastWasEOL
then writeln(xlogFile
, '') else writeln(xlogFile
, xlogPrefix
);
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
= WM_NEWFILE
) then
262 if FileExists(FileName
) then DeleteFile(FileName
);
270 procedure e_DeinitLog ();
272 if xlogFileOpened
then CloseFile(xlogFile
);
273 xlogFileOpened
:= false;
277 // ////////////////////////////////////////////////////////////////////////// //
278 (* Write/WriteLn driver *)
282 // TAB: tab space = 4
284 // userData[1]: current x (for tabs)
285 // userData[2]: #13 was eaten, we should skip next #10
288 TDevFunc
= function (var f
: TTextRec
): Integer;
295 procedure ProcessOutput (var tf
: TTextRec
; buf
: PChar; count
: Integer);
302 x
:= tf
.userData
[udX
];
303 wcr
:= (tf
.userData
[udWasCR
] <> 0);
306 // look for some special char
312 if (ch
= #13) or (ch
= #10) or (ch
= #9) or (ch
= #8) then break
;
322 cbufPutChars(buf
, f
);
328 // process special chars
345 if (ch
= #13) or (ch
= #10) then
350 if not wcr
or (ch
<> #10) then
359 tf
.userData
[udX
] := x
;
360 tf
.userData
[udWasCR
] := ord(wcr
);
364 function DevOpen (var f
: TTextRec
): Integer;
366 f
.userData
[udX
] := 0;
367 f
.userData
[udWasCR
] := 0;
373 function DevInOut (var f
: TTextRec
): Integer;
379 buf
:= Pointer(f
.BufPtr
);
381 if sz
> 0 then ProcessOutput(f
, buf
, sz
);
386 function DevFlush (var f
: TTextRec
): Integer;
388 result
:= DevInOut(f
);
391 function DevClose (var f
: TTextRec
): Integer;
397 procedure e_InitWritelnDriver ();
399 if not driverInited
then
401 driverInited
:= true;
402 with TTextRec(output
) do
407 BufSize
:= SizeOf(Buffer
);
410 OpenFunc
:= @DevOpen
;
411 InOutFunc
:= @DevInOut
;
412 FlushFunc
:= @DevFlush
;
413 CloseFunc
:= @DevClose
;
422 //e_InitWritelnDriver();