-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathLog4Pascal.pas
284 lines (230 loc) · 5.65 KB
/
Log4Pascal.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
{*******************************************************}
{ }
{ Log4Pascal }
{ https://github.com/martinusso/log4pascal }
{ }
{ This software is open source, }
{ licensed under the The MIT License (MIT). }
{ }
{*******************************************************}
unit Log4Pascal;
interface
type
TLogTypes = (ltTrace, ltDebug, ltInfo, ltWarning, ltError, ltFatal);
TLogger = class
private
FFileName: string;
FIsInit: Boolean;
FOutFile: TextFile;
FQuietMode: Boolean;
FQuietTypes: set of TLogTypes;
procedure Initialize;
procedure CreateFoldersIfNecessary;
procedure Finalize;
procedure Write(const Msg: string);
public
constructor Create(const FileName: string);
destructor Destroy; override;
property FileName: string read FFileName;
procedure SetQuietMode;
procedure DisableTraceLog;
procedure DisableDebugLog;
procedure DisableInfoLog;
procedure DisableWarningLog;
procedure DisableErrorLog;
procedure DisableFatalLog;
procedure SetNoisyMode;
procedure EnableTraceLog;
procedure EnableDebugLog;
procedure EnableInfoLog;
procedure EnableWarningLog;
procedure EnableErrorLog;
procedure EnableFatalLog;
procedure Clear;
procedure Trace(const Msg: string);
procedure Debug(const Msg: string);
procedure Info(const Msg: string);
procedure Warning(const Msg: string);
procedure Error(const Msg: string);
procedure Fatal(const Msg: string);
end;
var
Logger: TLogger;
implementation
uses
Forms,
SysUtils,
Windows;
const
FORMAT_LOG = '%s %s';
PREFIX_TRACE = 'TRACE';
PREFIX_DEBUG = 'DEBUG';
PREFIX_INFO = 'INFO ';
PREFIX_WARN = 'WARN ';
PREFIX_ERROR = 'ERROR';
PREFIX_FATAL = 'FATAL';
{ TLogger }
procedure TLogger.Clear;
begin
if not FileExists(FFileName) then
Exit;
if FIsInit then
CloseFile(FOutFile);
SysUtils.DeleteFile(FFileName);
FIsInit := False;
end;
constructor TLogger.Create(const FileName: string);
begin
FFileName := FileName;
FIsInit := False;
Self.SetNoisyMode;
FQuietTypes := [];
end;
procedure TLogger.CreateFoldersIfNecessary;
var
FilePath: string;
FullApplicationPath: string;
begin
FilePath := ExtractFilePath(FFileName);
if Pos(':', FilePath) > 0 then
ForceDirectories(FilePath)
else begin
FullApplicationPath := ExtractFilePath(Application.ExeName);
ForceDirectories(IncludeTrailingPathDelimiter(FullApplicationPath) + FilePath);
end;
end;
procedure TLogger.Debug(const Msg: string);
begin
{$WARN SYMBOL_PLATFORM OFF}
if DebugHook = 0 then
Exit;
{$WARN SYMBOL_PLATFORM ON}
if not (ltDebug in FQuietTypes) then
Self.Write(Format(FORMAT_LOG, [PREFIX_DEBUG, Msg]));
end;
destructor TLogger.Destroy;
begin
Self.Finalize;
inherited;
end;
procedure TLogger.DisableDebugLog;
begin
Include(FQuietTypes, ltDebug);
end;
procedure TLogger.DisableErrorLog;
begin
Include(FQuietTypes, ltError);
end;
procedure TLogger.DisableFatalLog;
begin
Include(FQuietTypes, ltFatal);
end;
procedure TLogger.DisableInfoLog;
begin
Include(FQuietTypes, ltInfo);
end;
procedure TLogger.DisableTraceLog;
begin
Include(FQuietTypes, ltTrace);
end;
procedure TLogger.DisableWarningLog;
begin
Include(FQuietTypes, ltWarning);
end;
procedure TLogger.EnableDebugLog;
begin
Exclude(FQuietTypes, ltDebug);
end;
procedure TLogger.EnableErrorLog;
begin
Exclude(FQuietTypes, ltError);
end;
procedure TLogger.EnableFatalLog;
begin
Exclude(FQuietTypes, ltFatal);
end;
procedure TLogger.EnableInfoLog;
begin
Exclude(FQuietTypes, ltInfo);
end;
procedure TLogger.EnableTraceLog;
begin
Exclude(FQuietTypes, ltTrace);
end;
procedure TLogger.EnableWarningLog;
begin
Exclude(FQuietTypes, ltWarning);
end;
procedure TLogger.Error(const Msg: string);
begin
if not (ltError in FQuietTypes) then
Self.Write(Format(FORMAT_LOG, [PREFIX_ERROR, Msg]));
end;
procedure TLogger.Fatal(const Msg: string);
begin
if not (ltFatal in FQuietTypes) then
Self.Write(Format(FORMAT_LOG, [PREFIX_FATAL, Msg]));
end;
procedure TLogger.Finalize;
begin
if (FIsInit and (not FQuietMode)) then
CloseFile(FOutFile);
FIsInit := False;
end;
procedure TLogger.Initialize;
begin
if FIsInit then
CloseFile(FOutFile);
if not FQuietMode then
begin
Self.CreateFoldersIfNecessary;
AssignFile(FOutFile, FFileName);
if not FileExists(FFileName) then
Rewrite(FOutFile)
else
Append(FOutFile);
end;
FIsInit := True;
end;
procedure TLogger.Info(const Msg: string);
begin
if not (ltInfo in FQuietTypes) then
Self.Write(Format(FORMAT_LOG, [PREFIX_INFO, Msg]));
end;
procedure TLogger.SetNoisyMode;
begin
FQuietMode := False;
end;
procedure TLogger.SetQuietMode;
begin
FQuietMode := True;
end;
procedure TLogger.Trace(const Msg: string);
begin
if not (ltTrace in FQuietTypes) then
Self.Write(Format(FORMAT_LOG, [PREFIX_TRACE, Msg]));
end;
procedure TLogger.Warning(const Msg: string);
begin
if not (ltWarning in FQuietTypes) then
Self.Write(Format(FORMAT_LOG, [PREFIX_WARN, Msg]));
end;
procedure TLogger.Write(const Msg: string);
const
FORMAT_DATETIME_DEFAULT = 'yyyy-mm-dd hh:nn:ss';
begin
if FQuietMode then
Exit;
Self.Initialize;
try
if FIsInit then
Writeln(FOutFile, Format('%s [%s]', [Msg, FormatDateTime(FORMAT_DATETIME_DEFAULT, Now)]));
finally
Self.Finalize;
end;
end;
initialization
Logger := TLogger.Create('Log.txt');
finalization
Logger.Free;
end.