1
unit TestLazLoggerCase;
8
Classes, SysUtils, fpcunit, testutils, testregistry, LazLoggerBase, LazLogger, LazClasses;
11
TStringArray = array of string;
15
TTestLazLogger = class(TTestCase)
17
FTheLogger: TLazLoggerFile;
18
FOnDbgOutCount, FOnDebugLnCount: Integer;
19
FOnDbgOutText, FOnDebugLnText: string;
20
FArgcMem, FArgVSaved: PPChar;
24
procedure TestOnDbgOut(Sender: TObject; S: string; var Handled: Boolean);
25
procedure TestOnDebugln(Sender: TObject; S: string; var Handled: Boolean);
27
procedure AssertDbgOut(Name: string; ExpCount: integer; ExpText: String);
28
procedure AssertDebugLn(Name: string; ExpCount: integer; ExpText: String);
30
procedure RestoreArgs;
31
procedure SetArgv(List: TStringArray);
32
procedure SetArgs(List: array of string);
36
procedure TestReCreate;
39
TLazLoggerForTest = class(TLazLogger)
44
procedure TTestLazLogger.TestOnDbgOut(Sender: TObject; S: string; var Handled: Boolean);
47
FOnDbgOutText := FOnDbgOutText + s;
51
procedure TTestLazLogger.TestOnDebugln(Sender: TObject; S: string; var Handled: Boolean);
54
FOnDebugLnText := FOnDebugLnText + s + LineEnding;
58
procedure TTestLazLogger.InitLogger;
60
FreeAndNil(FTheLogger);
61
FTheLogger := TLazLoggerFile.Create;
62
FTheLogger.OnDebugLn := @TestOnDebugln;
63
FTheLogger.OnDbgOut := @TestOnDbgOut;
68
procedure TTestLazLogger.AssertDbgOut(Name: string; ExpCount: integer; ExpText: String);
70
AssertEquals(Name + ' DbgOut call count', ExpCount, FOnDbgOutCount);
71
AssertEquals(Name + ' DbgOut text', ExpText, FOnDbgOutText);
76
procedure TTestLazLogger.AssertDebugLn(Name: string; ExpCount: integer; ExpText: String);
78
AssertEquals(Name + ' DebugLn call count', ExpCount, FOnDebugLnCount);
79
AssertEquals(Name + ' DebugLn text', ExpText, FOnDebugLnText);
84
procedure TTestLazLogger.SaveArgs;
90
procedure TTestLazLogger.RestoreArgs;
96
procedure TTestLazLogger.SetArgv(List: TStringArray);
100
if List = nil then begin
103
ReAllocMem(FArgcMem, 0);
106
argc := Length(List);
107
ReAllocMem(FArgcMem, argc * SizeOf(PChar));
109
for i := 0 to argc - 1 do
110
FArgcMem[i] := PChar(List[i]);
113
procedure TTestLazLogger.SetArgs(List: array of string);
117
if Length(List) = 0 then begin
121
SetLength(FArgs, Length(List));
122
for i := 0 to Length(List) - 1 do
127
procedure TTestLazLogger.TestEvent;
131
FTheLogger.DebugLn('a');
132
AssertDebugLn('debugln a', 1, 'a'+LineEnding);
133
AssertDbgOut('debugln a', 0, '');
135
FTheLogger.DebugLn('b', 'c');
136
AssertDebugLn('debugln b,c', 1, 'bc'+LineEnding);
137
AssertDbgOut('debugln b,c', 0, '');
139
FTheLogger.DebugLn(['d', 1]);
140
AssertDebugLn('debugln d,1', 1, 'd1'+LineEnding);
141
AssertDbgOut('debugln d,1', 0, '');
143
FTheLogger.DebugLn('e %d', [1]);
144
AssertDebugLn('debugln e,1', 1, 'e 1'+LineEnding);
145
AssertDbgOut('debugln e,1', 0, '');
148
FTheLogger.DbgOut('a');
149
AssertDbgOut('DbgOut a', 1, 'a');
150
AssertDebugLn('DbgOut a', 0, '');
152
FTheLogger.DbgOut('b', 'c');
153
AssertDbgOut('DbgOut b,c', 1, 'bc');
154
AssertDebugLn('DbgOut b,c', 0, '');
156
FTheLogger.DbgOut(['d', 1]);
157
AssertDbgOut('DbgOut d,1', 1, 'd1');
158
AssertDebugLn('DbgOut d,1', 0, '');
160
FTheLogger.DbgOut('e %d', [1]);
161
AssertDbgOut('DbgOut e,1', 1, 'e 1');
162
AssertDebugLn('DbgOut e,1', 0, '');
164
FTheLogger.DebugLnEnter('a');
165
AssertDebugLn('DebugLnEnter() a', 1, 'a'+LineEnding);
166
AssertDbgOut('DebugLnEnter() a', 0, '');
168
FTheLogger.DebugLn('in enter');
169
AssertDebugLn('debugln in enter', 1, ' in enter'+LineEnding);
170
AssertDbgOut('debugln in enter', 0, '');
172
FTheLogger.DebugLnExit('b');
173
AssertDebugLn('DebugLnExit() b', 1, 'b'+LineEnding);
174
AssertDbgOut('DebugLnExit() b', 0, '');
176
FTheLogger.DebugLn('after exit');
177
AssertDebugLn('debugln after exit', 1, 'after exit'+LineEnding);
178
AssertDbgOut('debugln after exit', 0, '');
181
FreeAndNil(FTheLogger);
184
procedure TTestLazLogger.TestFilter;
185
procedure FilterInit(List: array of string);
189
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
192
procedure FilterInitNoParam(List: array of string);
198
Procedure TestNoGroup(Name: String);
200
FTheLogger.DebugLn('NoGroup'+Name);
201
AssertDebugLn(Name + 'debugln NoGroup'+Name, 1, 'NoGroup'+Name+LineEnding);
203
FTheLogger.DebugLn('NoGroupNil'+Name);
204
AssertDebugLn(Name + 'debugln [nil] NoGroupNil'+Name, 1, 'NoGroupNil'+Name+LineEnding);
207
Procedure TestGroup(Name: String; g: PLazLoggerLogGroup; ExpLog: Boolean);
209
FTheLogger.DebugLn(g, 'Group'+Name);
211
AssertDebugLn(Name + 'debugln ['+g^.ConfigName+'] Group'+Name, 1, 'Group'+Name+LineEnding)
213
AssertDebugLn(Name + 'debugln ['+g^.ConfigName+'] Group'+Name, 0, '');
215
FTheLogger.DebugLn(g, ['Group2',Name]);
217
AssertDebugLn(Name + 'debugln ['+g^.ConfigName+'] [Group2,'+Name+']', 1, 'Group2'+Name+LineEnding)
219
AssertDebugLn(Name + 'debugln ['+g^.ConfigName+'] [Group2,'+Name+']', 0, '');
223
g1: PLazLoggerLogGroup;
228
{%region g1 not default enabled}
231
FilterInit(['exe', '--debug-log=a', a]);
232
g1 := FTheLogger.RegisterLogGroup('g1');
233
TestNoGroup(Format('%s -- %s', [s, a]));
234
TestGroup (Format('%s -- %s', [s, a]), g1, False);
236
TestGroup (Format('%s -- %s', [s+'->enabled', a]), g1, True);
239
FilterInit(['exe', '--debug-log=a', a]);
240
g1 := FTheLogger.RegisterLogGroup('g1');
241
TestNoGroup(Format('%s -- %s', [s, a]));
242
TestGroup (Format('%s -- %s', [s, a]), g1, False);
245
FilterInit(['exe', '--debug-log=a', a]);
246
g1 := FTheLogger.RegisterLogGroup('g1');
247
TestNoGroup(Format('%s -- %s', [s, a]));
248
TestGroup (Format('%s -- %s', [s, a]), g1, True);
249
g1^.Enabled := False;
250
TestGroup (Format('%s -- %s', [s+'->disabled', a]), g1, False);
253
FilterInit(['exe', '--debug-log=a', a]);
254
g1 := FTheLogger.RegisterLogGroup('g1');
255
TestNoGroup(Format('%s -- %s', [s, a]));
256
TestGroup (Format('%s -- %s', [s, a]), g1, True);
259
FilterInit(['exe', '--debug-log=a', a]);
260
g1 := FTheLogger.RegisterLogGroup('g1');
261
TestNoGroup(Format('%s -- %s', [s, a]));
262
TestGroup (Format('%s -- %s', [s, a]), g1, False);
265
FilterInit(['exe', '--debug-log=a', a]);
266
g1 := FTheLogger.RegisterLogGroup('g1');
267
TestNoGroup(Format('%s -- %s', [s, a]));
268
TestGroup (Format('%s -- %s', [s, a]), g1, False);
271
FilterInit(['exe', '--debug-log=a', a]);
272
g1 := FTheLogger.RegisterLogGroup('g1');
273
TestNoGroup(Format('%s -- %s', [s, a]));
274
TestGroup (Format('%s -- %s', [s, a]), g1, True);
277
FilterInit(['exe', '--debug-log=a', a]);
278
g1 := FTheLogger.RegisterLogGroup('g1');
279
TestNoGroup(Format('%s -- %s', [s, a]));
280
TestGroup (Format('%s -- %s', [s, a]), g1, True);
283
FilterInit(['exe', '--debug-log=a', a]);
284
g1 := FTheLogger.RegisterLogGroup('g1');
285
TestNoGroup(Format('%s -- %s', [s, a]));
286
TestGroup (Format('%s -- %s', [s, a]), g1, True);
289
FilterInit(['exe', '--debug-log=a', a]);
290
g1 := FTheLogger.RegisterLogGroup('g1');
291
TestNoGroup(Format('%s -- %s', [s, a]));
292
TestGroup (Format('%s -- %s', [s, a]), g1, False);
294
a := '--dbe=- --dbe=+g1';
295
FilterInit(['exe', '--debug-log=a', '--dbe=-', '--dbe=+g1']);
296
g1 := FTheLogger.RegisterLogGroup('g1');
297
TestNoGroup(Format('%s -- %s', [s, a]));
298
TestGroup (Format('%s -- %s', [s, a]), g1, True);
299
{%endregion g1 not default enabled}
301
{%region g1 default enabled}
304
FilterInit(['exe', '--debug-log=a', a]);
305
g1 := FTheLogger.RegisterLogGroup('g1', True);
306
TestNoGroup(Format('%s -- %s', [s, a]));
307
TestGroup (Format('%s -- %s', [s, a]), g1, True);
310
FilterInit(['exe', '--debug-log=a', a]);
311
g1 := FTheLogger.RegisterLogGroup('g1', True);
312
TestNoGroup(Format('%s -- %s', [s, a]));
313
TestGroup (Format('%s -- %s', [s, a]), g1, True);
316
FilterInit(['exe', '--debug-log=a', a]);
317
g1 := FTheLogger.RegisterLogGroup('g1', True);
318
TestNoGroup(Format('%s -- %s', [s, a]));
319
TestGroup (Format('%s -- %s', [s, a]), g1, True);
322
FilterInit(['exe', '--debug-log=a', a]);
323
g1 := FTheLogger.RegisterLogGroup('g1', True);
324
TestNoGroup(Format('%s -- %s', [s, a]));
325
TestGroup (Format('%s -- %s', [s, a]), g1, False);
328
FilterInit(['exe', '--debug-log=a', a]);
329
g1 := FTheLogger.RegisterLogGroup('g1', True);
330
TestNoGroup(Format('%s -- %s', [s, a]));
331
TestGroup (Format('%s -- %s', [s, a]), g1, True);
334
FilterInit(['exe', '--debug-log=a', a]);
335
g1 := FTheLogger.RegisterLogGroup('g1', True);
336
TestNoGroup(Format('%s -- %s', [s, a]));
337
TestGroup (Format('%s -- %s', [s, a]), g1, False);
340
FilterInit(['exe', '--debug-log=a', a]);
341
g1 := FTheLogger.RegisterLogGroup('g1', True);
342
TestNoGroup(Format('%s -- %s', [s, a]));
343
TestGroup (Format('%s -- %s', [s, a]), g1, False);
344
{%endregion g1 default enabled}
347
{%region g1 not default enabled / param after}
350
FilterInitNoParam(['exe', '--debug-log=a', a]);
351
g1 := FTheLogger.RegisterLogGroup('g1');
352
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
353
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
354
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, False);
357
FilterInitNoParam(['exe', '--debug-log=a', a]);
358
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
359
g1 := FTheLogger.RegisterLogGroup('g1');
360
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
361
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, False);
364
FilterInitNoParam(['exe', '--debug-log=a', a]);
365
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
366
g1 := FTheLogger.RegisterLogGroup('g1');
367
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
368
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, True);
371
FilterInitNoParam(['exe', '--debug-log=a', a]);
372
g1 := FTheLogger.RegisterLogGroup('g1');
373
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
374
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
375
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, True);
378
FilterInitNoParam(['exe', '--debug-log=a', a]);
379
g1 := FTheLogger.RegisterLogGroup('g1');
380
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
381
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
382
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, False);
385
FilterInitNoParam(['exe', '--debug-log=a', a]);
386
g1 := FTheLogger.RegisterLogGroup('g1');
387
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
388
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
389
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, False);
392
FilterInitNoParam(['exe', '--debug-log=a', a]);
393
g1 := FTheLogger.RegisterLogGroup('g1');
394
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
395
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
396
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, True);
399
FilterInitNoParam(['exe', '--debug-log=a', a]);
400
g1 := FTheLogger.RegisterLogGroup('g1');
401
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
402
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
403
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, True);
406
FilterInitNoParam(['exe', '--debug-log=a', a]);
407
g1 := FTheLogger.RegisterLogGroup('g1');
408
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
409
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
410
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, True);
413
FilterInitNoParam(['exe', '--debug-log=a', a]);
414
g1 := FTheLogger.RegisterLogGroup('g1');
415
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
416
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
417
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, False);
419
a := '--dbe=- --dbe=+g1';
420
FilterInitNoParam(['exe', '--debug-log=a', '--dbe=-', '--dbe=+g1']);
421
g1 := FTheLogger.RegisterLogGroup('g1');
422
FTheLogger.ParamForEnabledLogGroups := '--dbe=';
423
TestNoGroup(Format('Param late: %s -- %s', [s, a]));
424
TestGroup (Format('Param late: %s -- %s', [s, a]), g1, True);
425
{%endregion g1 not default enabled}
429
FreeAndNil(FTheLogger);
433
function CreateDebugLogger: TRefCountedObject;
435
Result := TLazLoggerForTest.Create;
436
TLazLoggerFile(Result).Assign(GetExistingDebugLogger);
439
procedure TTestLazLogger.TestReCreate;
441
AssertTrue('DebugLogger is TLazLoggerFile', DebugLogger is TLazLoggerFile);
443
DebugLogger.MaxNestPrefixLen := 122;
444
LazDebugLoggerCreator := @CreateDebugLogger;
446
AssertTrue('still DebugLogger is TLazLoggerFile', DebugLogger is TLazLoggerFile);
449
AssertTrue('DebugLogger is TLazLoggerForTest', LazLoggerBase.DebugLogger is TLazLoggerForTest);
450
AssertEquals('MaxNestPrefixLen = 122', 122, TLazLoggerForTest(LazLoggerBase.DebugLogger).MaxNestPrefixLen);
457
RegisterTest(TTestLazLogger);