-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.pas
executable file
·249 lines (201 loc) · 5.31 KB
/
log.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
{ $Id$
This is free software; you can redistribute it and/or modify it
under the terms of the Lesser GNU General Public License (LGPL) as
published by the Free Software Foundation; either version 2,
or (at your option) any later version.
The software is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LGPL
for more details.
You should have received a copy of the LGPL along with this
software; see the file lgpl.txt. If not, write to the
Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Created on November, 18th 2000 by Hinrich Donner <[email protected]>
This software is part of the OpenXP project (www.openxp.de).
}
{$i xpdefine.inc}
unit log;
interface
const
llNone = 0; { Nichts, auch nicht Fehler }
llError = 1; { Nur Fehler }
llWarning = 2; { 1 + Warnungen }
llInform = 3; { 2 + Infos }
llDebug = 4; { 3 + Alles }
type
TLog = class
protected
FFilename : string;
FHandle : text;
FCanWrite : boolean;
FFirstLog : boolean;
FLogLevel : integer;
FisOpen : boolean;
procedure PFilename(const fn: string); virtual;
procedure PLogLevel(l: integer); virtual;
public
constructor Create;
constructor CreateWithFilename(const fn: string);
destructor Destroy; override;
property Filename: string read FFilename write PFilename;
property isOpen: boolean read FisOpen;
property LogLevel: integer read FLogLevel write PLogLevel;
procedure Close; virtual;
function Open: boolean; virtual;
procedure Log(l: integer; const s: string); virtual;
end;
type
TLogModul = class(TLog)
protected
FModulName : string;
public
constructor Create(const s: string);
constructor CreateWithFilename(const s, fn: string);
procedure Log(l: integer; const s: string); override;
end;
implementation
uses
SysUtils,
xpversion,
XPGlobal;
const
llChars: array[llNone..llDebug] of char = (' ','?','!','>','$');
{ TLog }
constructor TLog.Create;
begin
FFilename:= '';
FCanWrite:= false;
FFirstLog:= true;
FisOpen:= false;
{$ifdef DEBUG}
FLogLevel:= llDebug;
{$else}
FLogLevel:= llError;
{$endif}
end;
constructor TLog.CreateWithFilename(const fn: string);
begin
FFilename:= fn;
FCanWrite:= true;
FFirstLog:= true;
FisOpen:= false;
{$ifdef DEBUG}
FLogLevel:= llDebug;
{$else}
FLogLevel:= llError;
{$endif}
end;
destructor TLog.Destroy;
begin
if FCanWrite and (FLogLevel>llNone) then begin
Open;
if FisOpen then
writeln(FHandle,FormatDateTime('hh:nn:ss',Now),' Logging ends',newline);
end;
Close;
end;
procedure TLog.Close;
begin
if FisOpen then begin
CloseFile(FHandle);
FisOpen:= False;
end;
end;
function TLog.Open: boolean;
begin
// result:= true;
// Logs are disabled
result:= false;
FisOpen:= false;
exit;
if FLogLevel=llNone then
Exit
else if FFilename='' then begin
result:= false;
FCanWrite:= false;
end else begin
AssignFile(FHandle,FFilename);
if FileExists(FFilename) then
Append(FHandle)
else
Rewrite(FHandle);
FisOpen:= ioresult=0;
if not isOpen then begin
result:= false;
FCanWrite:= false;
end else if FFirstLog then begin
writeln(FHandle,'---------- ', xp_prver_full);
writeln(FHandle,FormatDateTime('yyyy-mm-dd"T"hh:nn:ss',Now),' Logging started');
FFirstLog:= False;
end;
end;
end;
procedure TLog.Log(l: integer; const s: string);
begin
if (l<=llNone) or not(FCanWrite) then
Exit
else if l>llDebug then
l:= llDebug;
if l<FLogLevel then
Exit;
if not FisOpen then
Open;
if FisOpen then begin
WriteLn(FHandle, FormatDateTime('yyyy-mm-dd"T"hh:nn:ss',Now),' ',llChars[l],s);
Close;
end;
end;
procedure TLog.PFilename(const fn: string);
begin
if fn<>FFilename then begin
Open;
if FisOpen then begin
writeln(FHandle, FormatDateTime('hh:nn:ss',Now) + ' Logging ends' + newline);
Close;
end;
FFilename:= fn;
FCanWrite:= true;
FFirstLog:= true;
end;
end;
procedure TLog.PLogLevel(l: integer);
begin
if l<llNone then
FLogLevel:= llNone
else if l>llDebug then
FLogLevel:= llDebug
else
FLogLevel:= l;
end;
{ TLogModul }
constructor TLogModul.Create(const s: string);
begin
inherited Create;
FModulName:= s;
end;
constructor TLogModul.CreateWithFilename(const s, fn: string);
begin
inherited CreateWithFilename(fn);
FModulName:= s;
end;
procedure TLogModul.Log(l: integer; const s: string);
begin
inherited Log(l, '['+FModulName + '] ' + s);
end;
{
$Log: log.pas,v $
Revision 1.12 2002/12/04 16:56:58 dodi
- updated uses, comments and todos
Revision 1.11 2001/09/16 19:58:31 ma
- disabled for these logging routines are not used now
Revision 1.10 2001/09/08 16:29:29 mk
- use FirstChar/LastChar/DeleteFirstChar/DeleteLastChar when possible
- some AnsiString fixes
Revision 1.9 2001/09/07 23:24:53 ml
- Kylix compatibility stage II
Revision 1.8 2001/07/31 16:18:39 mk
- removed some unused variables
- changed some LongInt to DWord
- removed other hints and warnings
}
end.