-
Notifications
You must be signed in to change notification settings - Fork 1
/
DX.Utils.Logger.pas
390 lines (351 loc) · 9.37 KB
/
DX.Utils.Logger.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
{$REGION 'Documentation'}
/// <summary>
/// DX.Utils.Logger provides an easy to use logging mechanism
/// </summary>
/// <remarks>
/// <para>
/// DX.Utils.Logger is part of DX.Library
/// </para>
/// <para>
/// See: <see href="http://code.google.com/p/dx-library/" />
/// </para>
/// </remarks>
/// <example>
/// <para>
/// uses
/// </para>
/// <para>
/// DX.Utils.Logger;
/// </para>
/// <para>
/// Log('Some log message')
/// </para>
/// </example>
{$ENDREGION}
unit DX.Utils.Logger;
interface
uses
System.Classes, System.SysUtils;
type
{$REGION 'Documentation'}
/// <summary>
/// TFXLogger provides a thread-safe logging mechanism.
/// </summary>
{$ENDREGION}
TDXLogger = class(TObject)
private
class var FInstance: TDXLogger;
class var FTerminating: Boolean;
private
FExternalStringsOnTop: Boolean;
FExternalStrings: TStrings;
FLogBuffer: TStrings;
FThread: TThread;
FDateFormat: string;
protected
constructor Create;
destructor Destroy; reintroduce;
class function GetDateFormat: string; static;
class procedure SetExternalStrings(AStrings: TStrings); static;
class procedure SetExternalStringsAppendOnTop(const Value: Boolean); static;
class procedure SetDateFormat(ADateFormat: string); static;
function ExternalStringsAssigned: Boolean;
public
/// <summary>
/// External Strings can be used to log to a TMemo. Updates are done via Synchronize/Main Thread
/// </summary>
class property ExternalStrings: TStrings write SetExternalStrings;
/// <summary>
/// Insert new log messages on top of external strings.
/// </summary>
class property ExternalStringsAppendOnTop: Boolean write SetExternalStringsAppendOnTop;
/// <summary>
/// DateFormat is used to format the timestamp which prefixes every log message.
/// Defaults to YYYY-MM-DD hh:nn:ss,zzz
/// </summary>
class property DateFormat: string read GetDateFormat write SetDateFormat;
class procedure Log(const AMessage: string);
class function Instance: TDXLogger;
end;
/// <summary>
/// Shortcut to log a message
/// </summary>
procedure Log(const AMessage: string); overload;
/// <summary>
/// Shortcut to log a message with format string
/// </summary>
procedure Log(const AFormatString: string; const AValues: Array of const); overload;
/// <summary>
/// Shortcut to to avoid name clashes with other logging systems
/// </summary>
procedure DXLog(const AMessage: string); overload;
/// Shortcut to to avoid name clashes with other logging systems
procedure DXLog(const AFormatString: string; const AValues: Array of const); overload;
implementation
{$IFDEF MSWINDOWS}
uses
Windows,
Messages;
{$ENDIF}
{$IF defined(IOS) or Defined(MACOS)}
uses
DX.Apple.Utils;
{$ENDIF}
{$IFDEF Android}
uses
AndroidAPI.Log;
{$ENDIF}
type
TLogThread = class(TThread)
private
procedure UpdateConsole;
procedure UpdateLogFile;
procedure UpdateExternalStrings;
public
FTempBuffer: TStrings; // Used to decouple the main buffer
FExternalBuffer: TStrings; // Used as a separate buffer for writing to the external strings
constructor Create;
destructor Destroy; Override;
procedure Execute; Override;
procedure SyncronizeExternalStrings;
end;
procedure Log(const AMessage: string);
begin
TDXLogger.Log(AMessage);
end;
procedure Log(const AFormatString: string; const AValues: Array of const);
begin
TDXLogger.Log(Format(AFormatString, AValues));
end;
procedure DXLog(const AMessage: string); overload;
begin
Log(AMessage);
end;
procedure DXLog(const AFormatString: string; const AValues: Array of const); overload;
begin
Log(AFormatString, AValues);
end;
{ TDXLogger }
constructor TDXLogger.Create;
begin
inherited;
FLogBuffer := TStringList.Create;
FThread := TLogThread.Create;
FExternalStrings := nil;
FDateFormat := 'YYYY-MM-DD hh:nn:ss,zzz';
FThread.Start;
end;
destructor TDXLogger.Destroy;
begin
FreeAndNil(FThread);
FreeAndNil(FLogBuffer);
inherited;
end;
function TDXLogger.ExternalStringsAssigned: Boolean;
begin
TMonitor.Enter(Instance);
try
result := Assigned(FExternalStrings);
finally
TMonitor.Exit(Instance);
end;
end;
class function TDXLogger.GetDateFormat: string;
begin
TMonitor.Enter(Instance);
try
result := Instance.FDateFormat;
finally
TMonitor.Exit(Instance);
end;
end;
class function TDXLogger.Instance: TDXLogger;
begin
if FTerminating then
raise EAbort.Create('Terminating');
if FInstance = nil then
TDXLogger.FInstance := TDXLogger.Create;
result := FInstance;
end;
class procedure TDXLogger.Log(const AMessage: string);
var
LMessage: string;
begin
LMessage := FormatDateTime(DateFormat, now) + ' : ' + AMessage;
if Assigned(Instance.FLogBuffer) then
begin
TMonitor.Enter(Instance);
try
Instance.FLogBuffer.Add(LMessage);
finally
TMonitor.Exit(Instance);
end;
end;
end;
class procedure TDXLogger.SetDateFormat(ADateFormat: string);
begin
TMonitor.Enter(Instance);
try
Instance.FDateFormat := ADateFormat;
finally
TMonitor.Exit(Instance);
end;
end;
class procedure TDXLogger.SetExternalStrings(AStrings: TStrings);
begin
TMonitor.Enter(Instance);
try
Instance.FExternalStrings := AStrings;
finally
TMonitor.Exit(Instance);
end;
end;
class procedure TDXLogger.SetExternalStringsAppendOnTop(const Value: Boolean);
begin
TMonitor.Enter(Instance);
try
Instance.FExternalStringsOnTop := Value;
finally
TMonitor.Exit(Instance);
end;
end;
{ TLogThread }
constructor TLogThread.Create;
begin
inherited Create(true);
FTempBuffer := TStringList.Create;
FExternalBuffer := TStringList.Create;
end;
destructor TLogThread.Destroy;
begin
Terminate;
FreeAndNil(FExternalBuffer);
FreeAndNil(FTempBuffer);
inherited;
end;
procedure TLogThread.Execute;
begin
while not terminated do
begin
sleep(100);
TMonitor.Enter(TDXLogger.Instance);
try
if (TDXLogger.Instance.FLogBuffer.Count > 0) then
begin
FTempBuffer.AddStrings(TDXLogger.Instance.FLogBuffer);
TDXLogger.Instance.FLogBuffer.Clear;
end;
finally
TMonitor.Exit(TDXLogger.Instance);
end;
if FTempBuffer.Count > 0 then
begin
// From here FTempBuffer will only be used in this LogThread
// Log to console (iOS: NSLog, Windows: OutputDebugString)
UpdateConsole;
// Write Log file (Windows, MacOS: Appname.log)
UpdateLogFile;
// Write to External Strings (if, assigned, e.g. Memo, synchronized to Main Thread)
SyncronizeExternalStrings;
// Done with TempBuffer
FTempBuffer.Clear;
end;
end;
end;
procedure TLogThread.SyncronizeExternalStrings;
begin
if TDXLogger.Instance.ExternalStringsAssigned then
begin
TMonitor.Enter(FExternalBuffer); // FExternalBuffer is cleared in the contex of the main thread (via Synchronize)
try
FExternalBuffer.AddStrings(FTempBuffer);
finally
TMonitor.Exit(FExternalBuffer);
end;
Synchronize(UpdateExternalStrings);
end;
end;
procedure TLogThread.UpdateLogFile;
{$IF (defined(MSWindows) or defined(MacOS)) and not (defined(IOS)))}
var
F: TextFile;
s: String;
LFileName: string;
{$ENDIF}
begin
{$IF (defined(MSWindows) or defined(MacOS)) and not (defined(IOS))) }
try
LFileName := ParamStr(0) + '.log';
AssignFile(F, LFileName);
try
if FileExists(LFileName) then
Append(F)
else
Rewrite(F);
for s in FTempBuffer do
begin
Writeln(F, s);
end;
Flush(F);
finally
CloseFile(F);
end;
except
// Nothing - if logging doesn't work, then there nothing we can do about.
end;
{$ENDIF}
end;
procedure TLogThread.UpdateConsole;
var
LMessage: string;
{$IFDEF Android}
LMarshaller: TMarshaller;
{$ENDIF}
begin
for LMessage in FTempBuffer do
begin
{$IF defined(IOS) or Defined(MACOS)}
NSLog2(LMessage);
{$ENDIF}
{$IFDEF MSWINDOWS}
OutputDebugString(pchar(LMessage));
{$ENDIF}
{$IFDEF ANDROID}
LOGI(LMarshaller.AsAnsi(LMessage).ToPointer);
{$ENDIF}
end;
end;
procedure TLogThread.UpdateExternalStrings;
var
s: string;
begin
if (TDXLogger.Instance <> nil) and Assigned(TDXLogger.Instance.FExternalStrings) and Assigned(FExternalBuffer) and not TDXLogger.FTerminating
then
try
TMonitor.Enter(FExternalBuffer);
try
if TDXLogger.Instance.FExternalStringsOnTop then
begin
for s in FExternalBuffer do
begin
TDXLogger.Instance.FExternalStrings.Insert(0, s);
end;
end
else
TDXLogger.Instance.FExternalStrings.AddStrings(FExternalBuffer);
// We might offer a method to hand over a scrolling control handle
// SendMessage(FMemo.Handle, EM_LINESCROLL, 0, GLogger.FMemo.Lines.Count);
FExternalBuffer.Clear;
finally
TMonitor.Exit(FExternalBuffer);
end;
except
// If logging fails, then there is nocthng we can do
end;
end;
initialization
TDXLogger.FTerminating := false;
finalization
TDXLogger.FTerminating := true;
FreeAndNil(TDXLogger.FInstance);
end.