-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.Logger.pas
More file actions
1182 lines (1041 loc) · 39.7 KB
/
Copy pathMaxLogic.Logger.pas
File metadata and controls
1182 lines (1041 loc) · 39.7 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Unit MaxLogic.Logger;
//{$DEFINE DISABLE_PRLOGGER} // Keep this commented unless you specifically want to disable LoggerPro
{
Version: 1.2
History
2024-07-28: Added CloneWithTag for creating logger instances with different tags sharing the core writer.
Fixed bug in CheckDeleteOldLogFiles task.
Applied coding style and naming conventions.
Removed unused fCs field.
Added XML documentation stubs.
Added missing Int64 overload for put method.
2023-03-24: add possibility to disable json output
add madStackTrace and auto CallStack to errors of type ERROR
added MaxLogFileAgeInDays and deleting old log files
}
(*
JSON has become the de facto standard format for capturing and storing log data - and for good reason. Structured logging with JSON reduces the cost and complexity of extracting valuable insights from log data.30 lis 2021
this unit is based on the LoggerPro but it stores the data as json entries
That allows us to include many properties into the log and still keep it as a simple one-line-per-log-entry file that can be interpreted both by humans and computers
The log will have the following format depending on the format type:
ofkJson:
'[%{DATETIME}] [%{LEVEL}] %{JsonMESSAGE}';
ofkPlainText:
'[%{DATETIME}] [%{LEVEL}] [...other tags...] %{Message}
ofkPlainOnNextLine:
'[%{DATETIME}] [%{LEVEL}] [...other tags...]
%{Message}
ATTENTION:
in a DLL you have to manually free the TMaxLog object and
call LoggerPro.GlobalLogger.ReleaseGlobalLogger; before the process terminates.
*)
Interface
Uses
{$IFDEF MSWINDOWS}
winapi.windows,
{$ENDIF}
system.sysUtils, system.classes, system.Diagnostics, system.Threading,
syncObjs, system.Masks, system.Json, system.DateUtils, system.StrUtils,
{$IFNDEF DISABLE_PRLOGGER }
LoggerPro, LoggerPro.FileAppender, LoggerPro.OutputDebugStringAppender,
LoggerPro.GlobalLogger, LoggerPro.Proxy, LoggerPro.Renderers,
{$ENDIF}
MaxLogic.FastList, idGlobal, MaxLogic.ioUtils;
Type
// Forward declarations
TMaxLog = Class;
TLogEntry = Class;
TDoLogOnRelease = Class;
{$IFNDEF DISABLE_PRLOGGER }
TLogType = LoggerPro.TLogType;
{$ELSE}
TLogType = (Debug = 0, Info, Warning, Error);
{$ENDIF}
TOutputFormatKind = (ofkJson, ofkPlainText, ofkPlainOnNextLine);
/// <summary>
/// Interface for a single log entry, allowing fluent addition of tagged data.
/// </summary>
iLogEntry = Interface
['{AAB78F7A-4526-4BAF-B1B7-C73CB23AC63D}']
/// <summary>Sets the main message tag ("MSG").</summary>
Function msg(Const aMsg: String): iLogEntry;
/// <summary>Adds a string tag. Tag names should be simple ASCII.</summary>
Function put(Const aTagName, aTagValue: String): iLogEntry; Overload;
/// <summary>Adds an integer tag.</summary>
Function put(Const aTagName: String; aTagValue: Integer): iLogEntry; Overload;
/// <summary>Adds an Int64 tag.</summary>
Function put(Const aTagName: String; Const aTagValue: Int64): iLogEntry; Overload;
/// <summary>Adds a TDateTime tag (formatted as ISO8601).</summary>
Function put(Const aTagName: String; Const aTagValue: TDateTime): iLogEntry; Overload;
/// <summary>Adds a floating-point tag.</summary>
Function put(Const aTagName: String; Const aTagValue: Double): iLogEntry; Overload;
/// <summary>Adds component hierarchy information under the "COMP" tag.</summary>
Function putComponent(aComponent: TComponent): iLogEntry;
/// <summary>Sets the severity level for this specific log entry.</summary>
Procedure SetLogType(Const Value: TLogType);
/// <summary>Gets the severity level for this specific log entry.</summary>
Function GetLogType: TLogType;
/// <summary>Gets or sets the severity level for this specific log entry.</summary>
Property LogType: TLogType Read GetLogType Write SetLogType;
/// <summary>Formats the log entry into a string based on the specified kind.</summary>
Function Text(aKind: TOutputFormatKind): String;
/// <summary>Sets the call stack string (used for errors with MadExcept).</summary>
procedure SetCallStack(const Value: String);
/// <summary>Gets the call stack string.</summary>
function GetCallStack: String;
/// <summary>Gets or sets the call stack (relevant for errors when MadExcept is enabled).</summary>
property CallStack: String read GetCallStack write SetCallStack;
End;
/// <summary>
/// Interface for the main logger functionality.
/// </summary>
iMaxLog = Interface
['{28201268-1C2A-4102-9BFB-1E81BB3B1826}']
procedure CheckDeleteOldLogFiles;
/// <summary>Sets the default tag used when logging messages.</summary>
Procedure SetDefaultTag(Const Value: String);
/// <summary>Gets the default tag used when logging messages.</summary>
Function GetDefaultTag: String;
{$IFNDEF DISABLE_PRLOGGER }
/// <summary>Sets the underlying LoggerPro writer instance.</summary>
Procedure SetLogWriter(Const Value: ILogWriter);
/// <summary>Gets the underlying LoggerPro writer instance.</summary>
Function GetLogWriter: ILogWriter;
{$ENDIF}
/// <summary>Creates a new, empty log entry object.</summary>
Function LogENtry(Const aMsg: String = ''): iLogEntry;
/// <summary>Logs procedure entry and automatically logs exit with duration when the result goes out of scope.</summary>
Function LogEnterExitProc(Const aProcName: String; aLogType: TLogType = TLogType.Debug): iLogEntry;
/// <summary>Adds a simple string message with a specified log level.</summary>
Procedure add(Const aMsg: String; aLogType: TLogType = TLogType.Info); Overload;
/// <summary>Adds a pre-built log entry using its own log level.</summary>
Procedure add(aMsg: iLogEntry); Overload;
/// <summary>Adds a pre-built log entry, overriding its log level.</summary>
Procedure add(aMsg: iLogEntry; aLogType: TLogType); Overload;
/// <summary>Adds a raw, pre-formatted log string.</summary>
Procedure addRaw(Const aJsonLogEntryText: String; aLogType: TLogType);
// lock/Unlock to protect the LoggerPro writer access
procedure Lock;
procedure UnLock;
/// <summary>Logs an informational message (string).</summary>
Procedure Info(Const aMsg: String); Overload;
/// <summary>Logs an informational message (log entry).</summary>
Procedure Info(aMsg: iLogEntry); Overload;
/// <summary>Logs a warning message (string).</summary>
Procedure Warn(Const aMsg: String); Overload;
/// <summary>Logs a warning message (log entry).</summary>
Procedure Warn(aMsg: iLogEntry); Overload;
/// <summary>Logs an error message (string).</summary>
Procedure Error(Const aMsg: String); Overload;
// Best source: crash-time stack (must be called inside the same `except` block)
// lTrace := madExcept.GetCrashStackTrace;
Procedure Error(Const aMsg, aCallStackTrace: String); overload;
/// <summary>Logs an error message (log entry).</summary>
Procedure Error(aMsg: iLogEntry); Overload;
/// <summary>Logs a debug message (string).</summary>
Procedure Debug(Const aMsg: String); Overload;
/// <summary>Logs a debug message (log entry).</summary>
Procedure Debug(aMsg: iLogEntry); Overload;
/// <summary>Gets the output format kind (JSON, PlainText, etc.).</summary>
function GetOutputFormatKind: TOutputFormatKind;
/// <summary>Sets the output format kind.</summary>
procedure SetOutputFormatKind(const Value: TOutputFormatKind);
/// <summary>Gets whether to automatically add stack traces for errors (requires MadExcept).</summary>
function GetAutoStackTraceForErrors: Boolean;
/// <summary>Sets whether to automatically add stack traces for errors.</summary>
procedure SetAutoStackTraceForErrors(const Value: Boolean);
/// <summary>Gets the directory where log files are stored.</summary>
function GetLogDir: String;
/// <summary>Gets the maximum age in days for log files before deletion.</summary>
function GetMaxLogFileAgeInDays: Integer;
/// <summary>Sets the maximum age in days for log files before deletion (0 disables).</summary>
procedure SetMaxLogFileAgeInDays(const Value: Integer);
/// <summary>Creates a new logger instance that shares the same configuration and underlying writer but uses a different default tag.</summary>
Function CloneWithTag(Const aNewTag: String): iMaxLog;
// releases internal loggerPro Writer
procedure ShutDown;
/// <summary>Gets or sets the default tag associated with log messages from this instance.</summary>
Property DefaultTag: String Read GetDefaultTag Write SetDefaultTag;
{$IFNDEF DISABLE_PRLOGGER }
/// <summary>Gets or sets the raw underlying LoggerPro writer instance.</summary>
Property LogWriter: ILogWriter Read GetLogWriter Write SetLogWriter;
{$ENDIF}
/// <summary>Gets or sets the format for log output.</summary>
property OutputFormatKind: TOutputFormatKind read GetOutputFormatKind write SetOutputFormatKind;
/// <summary>Gets or sets whether stack traces are automatically included for error logs (requires MadExcept).</summary>
property AutoStackTraceForErrors: Boolean read GetAutoStackTraceForErrors write SetAutoStackTraceForErrors;
/// <summary>Gets the configured log directory.</summary>
property LogDir: String read GetLogDir;
/// <summary>Gets or sets the maximum age (in days) of log files to keep.</summary>
property MaxLogFileAgeInDays: Integer read GetMaxLogFileAgeInDays write SetMaxLogFileAgeInDays;
End;
/// <summary>
/// Concrete implementation of iLogEntry.
/// </summary>
TLogEntry = Class(TInterfacedObject, iLogEntry)
Private
// Tags are the message parts that will form the json object
fTags: MaxLogic.FastList.TSortedList<String, String>;
fThreadId: String;
fLogType: TLogType;
fCallStack: String;
Procedure Append(Var aText: String; Const aTagName, aTagValue: String);
Procedure CaptureThreadId;
// iLogEntry implementations
Procedure SetLogType(Const Value: TLogType);
Function GetLogType: TLogType;
procedure SetCallStack(const Value: String);
function GetCallStack: String;
Public
constructor Create;
destructor Destroy; override;
// iLogEntry implementations
Function msg(Const aMsg: String): iLogEntry;
Function put(Const aTagName, aTagValue: String): iLogEntry; Overload;
Function put(Const aTagName: String; aTagValue: Integer): iLogEntry; Overload;
Function put(Const aTagName: String; Const aTagValue: Int64): iLogEntry; Overload;
Function put(Const aTagName: String; Const aTagValue: TDateTime): iLogEntry; Overload;
Function put(Const aTagName: String; Const aTagValue: Double): iLogEntry; Overload;
Function putComponent(aComponent: TComponent): iLogEntry;
Function Text(aKind: TOutputFormatKind): String;
End;
/// <summary>
/// Concrete implementation of iMaxLog. Manages log settings and interacts with LoggerPro or basic file writing.
/// </summary>
TMaxLog = Class(TInterfacedObject, iMaxLog)
Private
fDefaultTag: String;
{$IFNDEF DISABLE_PRLOGGER }
fLogWriter: ILogWriter;
{$ENDIF}
fLogDir: String;
fOutputFormatKind: TOutputFormatKind;
fAutoStackTraceForErrors: Boolean;
fMaxLogFileAgeInDays: Integer;
fLastCheckOldLogFilesTimeStamp: TDateTime;
fFileMaskForDeletingOldLogFiles: String;
fParentInstance: iMaxLog;
// Private constructor for cloning
constructor CreateClone(aSource: TMaxLog; const aNewTag: string);
// iMaxLog private implementations
Procedure SetDefaultTag(Const Value: String);
{$IFNDEF DISABLE_PRLOGGER }
Procedure SetLogWriter(Const Value: ILogWriter);
Function GetLogWriter: ILogWriter;
{$ENDIF}
Function GetDefaultTag: String;
function GetOutputFormatKind: TOutputFormatKind;
procedure SetOutputFormatKind(const Value: TOutputFormatKind);
procedure SetAutoStackTraceForErrors(const Value: Boolean);
function GetAutoStackTraceForErrors: Boolean;
function GetLogDir: String;
procedure SetMaxLogFileAgeInDays(const Value: Integer);
function GetMaxLogFileAgeInDays: Integer;
procedure CheckDeleteOldLogFiles;
Public
// The final file name will be: aLogDir + Format(aLogFileNameFormat, [lModuleName, aFileNumber, aTag, PID]);
// Note: If aLogDir is empty, the default will be {AppDir}\Logs
constructor Create(aMaxFileCount: Integer = 10;
aMaxFileSizeInKb: Integer = 50 * 1024;
Const aLogDir: String = '';
Const aLogFileNameFormat: String
{$IFNDEF DISABLE_PRLOGGER}
= TLoggerProFileAppender.DEFAULT_FILENAME_FORMAT_WITH_PID
{$ELSE}
= ''
{$ENDIF}); overload;
destructor Destroy; override;
// iMaxLog public implementations
Function LogENtry(Const aMsg: String = ''): iLogEntry;
Function LogEnterExitProc(Const aProcName: String; aLogType: TLogType = TLogType.Debug): iLogEntry;
Procedure add(Const aMsg: String; aLogType: TLogType = TLogType.Info); Overload;
Procedure add(aMsg: iLogEntry); Overload;
Procedure add(aMsg: iLogEntry; aLogType: TLogType); Overload;
Procedure addRaw(Const aJsonLogEntryText: String; aLogType: TLogType);
// lock/Unlock to protect the LoggerPro writer access
procedure Lock;
procedure UnLock;
Procedure Info(Const aMsg: String); Overload;
Procedure Info(aMsg: iLogEntry); Overload;
Procedure Warn(Const aMsg: String); Overload;
Procedure Warn(aMsg: iLogEntry); Overload;
Procedure Error(Const aMsg: String); Overload;
// Best source: crash-time stack (must be called inside the same `except` block)
// lTrace := madExcept.GetCrashStackTrace;
Procedure Error(Const aMsg, aCallStackTrace: String); overload;
Procedure Error(aMsg: iLogEntry); Overload;
Procedure Debug(Const aMsg: String); Overload;
Procedure Debug(aMsg: iLogEntry); Overload;
Function CloneWithTag(Const aNewTag: String): iMaxLog;
// releases internal loggerPro Writer
procedure ShutDown;
// Properties defined in iMaxLog are implemented via the private getters/setters
End;
/// <summary>
/// Helper class used by LogEnterExitProc to automatically log procedure exit and duration.
/// </summary>
TDoLogOnRelease = Class(TLogEntry)
Private
fStopWatch: TStopWatch;
fMaxLog: iMaxLog; // Keep reference to the logger that created it
Public
constructor Create(aMaxLog: iMaxLog);
destructor Destroy; override;
End;
Var
glMainVclThreadId: TThreadID; // ID of the main VCL/Application thread
{$IFDEF MsWindows} // PID is constant on Windows
glPid: Int64;
glPidAsHex: String = ''; // Cache PID hex string
{$ENDIF}
GlobalAppStartTimer: TStopWatch; // Timer started at application launch
// Default global iMaxLog instance, auto-created on first use
Function maxLog: iMaxLog;
// Explicitly set the global iMaxLog instance
Procedure SetGlobalMaxLog(aInstance: iMaxLog);
// Check if the global instance has been created
Function GlobalMaxLogInstanceCreated: Boolean;
// Release the global logger instance and resources
Procedure ShutDownMaxLog;
// Helper to encode string for JSON
Function JsonEncode(Const RawText: String): String; inline;
{$IFDEF MsWindows}
// Get current process ID (Windows)
Function GetPID: Cardinal;
{$ENDIF}
// Get current process ID as a hex string
Function GetPidAsHex: String;
Implementation
Uses
{$IFDEF madExcept}
madStackTrace, // For capturing stack traces on error
{$ENDIF}
{$IFDEF LINUX}
// Linux specific units if needed, e.g., for process/thread info
Posix.Stdlib, Posix.SysStat, Posix.SysTypes, Posix.Unistd, Posix.Signal, Posix.Fcntl,
{$ENDIF}
system.IOUtils; // Added for TDirectory, TFile, TPath
const
cTagMsg = 'MSG'; // Standard tag name for the main log message
cCallStackTag = 'CallStack'; // Standard tag name for stack traces
Var
glFormatSettings: TFormatSettings; // For consistent float/date formatting
glCs: TCriticalSection; // Protects global logger instance creation
glDefaultMaxLog: iMaxLog; // The global logger instance
// -----------------------------------------------------------------------------
// Global Logger Management
// -----------------------------------------------------------------------------
Function GlobalMaxLogInstanceCreated: Boolean;
begin
Result := Assigned(glDefaultMaxLog);
end;
Function maxLog: iMaxLog;
begin
// Fast path: Instance already exists
if Assigned(glDefaultMaxLog) then
begin
Result := glDefaultMaxLog;
Exit;
end;
// Ensure critical section is created (thread-safe)
if not Assigned(glCs) then
begin
var lCs := TCriticalSection.Create;
if TInterlocked.CompareExchange(Pointer(glCs), Pointer(lCs), nil) <> nil then
lCs.Free; // Didn't win the race, free the extra CS
end;
// Slow path: Create instance within critical section
glCs.Enter;
try
// Double-check pattern
if Assigned(glDefaultMaxLog) then
Result := glDefaultMaxLog
else
begin
glDefaultMaxLog := TMaxLog.Create; // Create with default settings
Result := glDefaultMaxLog;
end;
finally
glCs.Leave;
end;
end;
Procedure SetGlobalMaxLog(aInstance: iMaxLog);
begin
// Ensure critical section is created (thread-safe)
if not Assigned(glCs) then
begin
var lCs := TCriticalSection.Create;
if TInterlocked.CompareExchange(Pointer(glCs), Pointer(lCs), nil) <> nil then
lCs.Free;
end;
glCs.Enter;
try
glDefaultMaxLog := aInstance; // Assign the provided instance
finally
glCs.Leave;
end;
end;
Procedure ShutDownMaxLog;
begin
if Assigned(glDefaultMaxLog) then
begin
if assigned(glDefaultMaxLog) then
glDefaultMaxLog.ShutDown;
glDefaultMaxLog := nil; // Release our reference
{$IFNDEF DISABLE_PRLOGGER}
// Signal LoggerPro to shut down its threads and release resources
LoggerPro.GlobalLogger.ReleaseGlobalLogger;
// Give LoggerPro's background threads some time to finish writing
// Note: This might be fragile. LoggerPro should ideally block until done.
Sleep(10); // Initial small delay
// CheckSynchronize waits for pending main thread messages, often used
// to allow background thread sync calls to complete.
while System.Classes.CheckSynchronize(50) do // Check every 50ms
Sleep(10); // Short sleep between checks
{$ENDIF}
end;
end;
// -----------------------------------------------------------------------------
// Process ID Helpers
// -----------------------------------------------------------------------------
{$IFDEF MsWindows}
Function GetPID: Cardinal;
begin
// Cache PID on Windows as it doesn't change
if glPid <> 0 then
Exit(glPid);
Result := GetCurrentProcessId;
glPid := Result; // Store for next time
end;
{$ENDIF}
Function GetPidAsHex: String;
var
lPid: Int64;
begin
{$IFDEF MsWindows}
// Use cached hex string on Windows if available
if glPidAsHex <> '' then
Exit(glPidAsHex);
{$ENDIF}
lPid := idGlobal.CurrentProcessId; // Get current PID (works cross-platform)
Result := IntToHex(lPid, 1); // Convert to hex (minimum 1 digit)
{$IFDEF MsWindows}
// Cache the result on Windows
glPidAsHex := Result;
{$ENDIF}
end;
// -----------------------------------------------------------------------------
// JSON Encoding Helper
// -----------------------------------------------------------------------------
Function JsonEncode(Const RawText: String): String;
var
lJsonStr: TJsonString;
begin
// Use System.Json for robust JSON string encoding
lJsonStr := TJsonString.Create(RawText);
try
Result := lJsonStr.ToJson;
finally
lJsonStr.Free; // Ensure TJsonString is freed
end;
end;
// -----------------------------------------------------------------------------
// TLogEntry Implementation
// -----------------------------------------------------------------------------
constructor TLogEntry.Create;
begin
inherited Create;
// Initialize the sorted list for tags with a reasonable default capacity
fTags := TSortedList<String, String>.Create;
fTags.Capacity := 10;
CaptureThreadId; // Record the creating thread's ID/name
fLogType := TLogType.Info; // Default log level
end;
destructor TLogEntry.Destroy;
begin
// TSortedList will be freed automatically by TInterfacedObject mechanism
// if fTags holds the only reference (which it should)
inherited;
end;
Procedure TLogEntry.Append(Var aText: String; Const aTagName, aTagValue: String);
var
s: String;
begin
// Don't append empty values
if aTagValue = '' then
Exit;
// Format as "TagName": "EncodedValue"
s := '"' + aTagName + '":' + JsonEncode(aTagValue);
// Append with comma separator if needed
if aText = '' then
aText := s
else
aText := aText + ', ' + s;
end;
Procedure TLogEntry.CaptureThreadId;
var
tid: TThreadID;
begin
// Only capture if not already done
if fThreadId = '' then
begin
{$IFDEF MSWINDOWS}
tid := GetCurrentThreadId;
{$ELSE}
tid := TThread.CurrentThread.ThreadID; // Use TThread for cross-platform
{$ENDIF}
// Identify the main thread
if tid = glMainVclThreadId then
fThreadId := 'Main'
else
begin
// ToDo: Consider integrating with TThread.Name for more descriptive IDs
fThreadId := IntToHex(tid, 1); // Use Hex ID for other threads
end;
end;
end;
function TLogEntry.GetCallStack: String;
begin
Result := fCallStack;
end;
procedure TLogEntry.SetCallStack(const Value: String);
begin
fCallStack := Value;
end;
Function TLogEntry.GetLogType: TLogType;
begin
Result := fLogType;
end;
Procedure TLogEntry.SetLogType(Const Value: TLogType);
begin
fLogType := Value;
end;
Function TLogEntry.msg(Const aMsg: String): iLogEntry;
begin
Result := put(cTagMsg, aMsg); // Use the constant for the message tag
end;
Function TLogEntry.put(Const aTagName, aTagValue: String): iLogEntry;
begin
fTags.addOrSet(aTagValue, aTagName); // Add or update the tag
Result := Self; // Return self for fluent interface chaining
end;
Function TLogEntry.put(Const aTagName: String; aTagValue: Integer): iLogEntry;
begin
Result := put(aTagName, aTagValue.ToString); // Convert integer to string
end;
Function TLogEntry.put(Const aTagName: String; Const aTagValue: Int64): iLogEntry;
begin
Result := put(aTagName, aTagValue.ToString); // Convert Int64 to string
end;
Function TLogEntry.put(Const aTagName: String; Const aTagValue: Double): iLogEntry;
var
s: String;
begin
// Use global format settings for consistent float formatting (e.g., decimal point)
s := FloatToStr(aTagValue, glFormatSettings);
Result := put(aTagName, s);
end;
Function TLogEntry.put(Const aTagName: String; Const aTagValue: TDateTime): iLogEntry;
var
s: String;
begin
// Use ISO8601 format for dates, which is standard and unambiguous
s := system.DateUtils.DateToISO8601(aTagValue, True); // Include time zone info
Result := put(aTagName, s);
end;
Function TLogEntry.putComponent(aComponent: TComponent): iLogEntry;
var
s: String;
procedure PrependName(Const aValue: String);
begin
if s = '' then
s := aValue
else
s := s + '.' + aValue; // Build hierarchy string like Owner.Child.Grandchild
end;
begin
s := ''; // Initialize hierarchy string
repeat
if (aComponent = nil) then
begin
if s = '' then // Handle case where input component is nil
PrependName('<NIL>');
Break; // Exit loop once owner chain reaches nil
end
else
begin
// Prepend "Name(ClassName)" or "$Address(ClassName)" if no name
PrependName(
IfThen(aComponent.Name <> '', aComponent.Name, '$' + IntToHex(NativeInt(aComponent), 1)) +
'(' + aComponent.ClassName + ')'
);
aComponent := aComponent.Owner; // Move up the owner chain
end;
until False; // Loop controlled by break condition
Result := put('COMP', s); // Add the hierarchy under the "COMP" tag
end;
Function TLogEntry.Text(aKind: TOutputFormatKind): String;
var
x, i: Integer;
lPid: Int64; // Local variable for current PID check
lTagValue: String;
lTagName: String;
begin
Result := '';
// Add standard metadata tags common to all formats
put('TID', fThreadId);
put('PID', GetPidAsHex);
// Check if process forked (PID changed since startup - relevant on Linux)
lPid := idGlobal.CurrentProcessId;
{$IFDEF MSWINDOWS}
if lPid <> glPid then // Compare with cached startup PID on Windows
put('fork-PID', IntToHex(lPid, 1));
{$ELSE}
// On Linux, we don't cache glPid, so this check isn't meaningful in the same way.
// You might need a different mechanism if fork detection is critical on Linux.
{$ENDIF}
put('MsSinceAppStart', GlobalAppStartTimer.ElapsedMilliseconds); // Add uptime
case aKind of
ofkJson:
begin
// Build JSON object: { "Tag1": "Value1", "Tag2": "Value2", ... }
for x := 0 to fTags.Count - 1 do
begin
lTagName := fTags.Items[x].iid;
lTagValue := fTags.Items[x].data;
Append(Result, lTagName, lTagValue); // Append "tag": "value"
end;
// Add call stack last in JSON if available
if fCallStack <> '' then
Append(Result, cCallStackTag, fCallStack);
Result := '{' + Result + '}'; // Wrap in braces
end;
ofkPlainText, ofkPlainOnNextLine:
begin
// Build plain text: [TAG1: Value1] [TAG2: Value2] ... Message
for x := 0 to fTags.Count - 1 do
begin
lTagName := fTags.Items[x].iid;
// Skip the main message tag here; handle it separately
if SameText(lTagName, cTagMsg) then // Case-insensitive compare
Continue;
lTagValue := fTags.Items[x].data;
Result := Result + '[' + lTagName + ': ' + lTagValue + '] ';
end;
// Find and append the main message
if fTags.Find(cTagMsg, i) then
begin
if aKind = ofkPlainOnNextLine then
Result := Trim(Result) + sLineBreak + fTags[i] // Message on new line
else
Result := Result + fTags[i]; // Message on same line
end;
// Append call stack on new lines if available
if fCallStack <> '' then
begin
Result := Result + sLineBreak +
cCallStackTag + ':' + sLineBreak + // Add a label for the stack
fCallStack;
end;
end;
else // Should not happen
Result := 'Unknown OutputFormatKind';
end;
end;
// -----------------------------------------------------------------------------
// TMaxLog Implementation
// -----------------------------------------------------------------------------
// Main constructor
constructor TMaxLog.Create(aMaxFileCount: Integer; aMaxFileSizeInKb: Integer;
const aLogDir: String; const aLogFileNameFormat: String);
var
{$IFNDEF DISABLE_PRLOGGER }
lFileAppender: TLoggerProFileAppender;
{$ENDIF}
lPid: Int64;
lModuleName: string; // For potential use in filename format
begin
inherited Create;
fOutputFormatKind := ofkJson; // Default to JSON format
fAutoStackTraceForErrors := True; // Default to adding stack traces for errors
// Determine and normalize the log directory
if aLogDir = '' then
// Default to 'Logs' subdirectory relative to the executable/DLL
fLogDir := IncludeTrailingPathDelimiter(ExtractFilePath(MaxLogic.ioUtils.GetCurrentDLLName) + 'logs')
else
// Expand potential relative paths or environment variables
fLogDir := IncludeTrailingPathDelimiter(ExpandFileName(aLogDir));
// Ensure the log directory exists
ForceDirectories(fLogDir);
lPid := idGlobal.CurrentProcessId;
fDefaultTag := 'Main'; // Default tag, can be overridden
// Calculate the file mask for deleting old log files based on the format string
fFileMaskForDeletingOldLogFiles := ChangeFileExt(aLogFileNameFormat, '*.log');
// Replace format specifiers with wildcards
lModuleName := TPath.GetFileNameWithoutExtension(MaxLogic.ioUtils.GetCurrentDLLName); // Get module name for replacements
fFileMaskForDeletingOldLogFiles := StringReplace(fFileMaskForDeletingOldLogFiles, '%s', lModuleName, [rfIgnoreCase]); // Assume first %s is module
fFileMaskForDeletingOldLogFiles := StringReplace(fFileMaskForDeletingOldLogFiles, '%s', '*', [rfReplaceAll, rfIgnoreCase]); // Replace remaining %s with *
fFileMaskForDeletingOldLogFiles := StringReplace(fFileMaskForDeletingOldLogFiles, '%d', '*', [rfReplaceAll, rfIgnoreCase]);
fFileMaskForDeletingOldLogFiles := StringReplace(fFileMaskForDeletingOldLogFiles, '%2.2d', '*', [rfReplaceAll, rfIgnoreCase]);
fFileMaskForDeletingOldLogFiles := StringReplace(fFileMaskForDeletingOldLogFiles, '*.*', '*', [rfReplaceAll, rfIgnoreCase]); // Collapse wildcards
fFileMaskForDeletingOldLogFiles := StringReplace(fFileMaskForDeletingOldLogFiles, '**', '*', [rfReplaceAll, rfIgnoreCase]); // Collapse wildcards
{$IFNDEF DISABLE_PRLOGGER}
// Create the LoggerPro file appender
lFileAppender := TLoggerProFileAppender.Create(
aMaxFileCount, // Max number of log files
aMaxFileSizeInKb, // Max size per file
fLogDir, // Log directory
aLogFileNameFormat, // Filename pattern
TLogItemRendererNoTag.Create, // Use a renderer that omits the tag (we put it in the message)
TEncoding.UTF8 // Use UTF-8 encoding
);
// Build the log writer with the file appender
// You could add other appenders here, like TLoggerProOutputDebugStringAppender.Create
fLogWriter := BuildLogWriter([lFileAppender]);
{$ENDIF}
// Initialize other fields
fMaxLogFileAgeInDays := 0; // Default: Don't delete old logs
fLastCheckOldLogFilesTimeStamp := 0; // Initialize timestamp for log deletion check
end;
// Cloning constructor (private)
constructor TMaxLog.CreateClone(aSource: TMaxLog; const aNewTag: string);
begin
inherited Create;
// Copy configuration and shared resources from the source instance
fDefaultTag := aNewTag; // Set the new tag
fLogDir := aSource.fLogDir;
fOutputFormatKind := aSource.fOutputFormatKind;
fAutoStackTraceForErrors := aSource.fAutoStackTraceForErrors;
fMaxLogFileAgeInDays := 0; // disable deleting of old log files, only the main instace should do that
fParentInstance:= aSource;
end;
destructor TMaxLog.Destroy;
begin
ShutDown;
inherited;
end;
procedure TMaxLog.Error(const aMsg, aCallStackTrace: String);
var
lMsg: iLogEntry;
begin
lMsg:= LogENtry(aMsg);
lMsg.CallStack := aCallStackTrace;
Add(lMsg, TLogType.Error);
end;
// iMaxLog Interface Method Implementations
function TMaxLog.GetLogDir: String;
begin
Result := fLogDir;
end;
procedure TMaxLog.SetMaxLogFileAgeInDays(const Value: Integer);
begin
// Basic validation could be added (e.g., Value >= 0)
fMaxLogFileAgeInDays := Value;
// Reset timestamp to force check soon if age limit was just set
if Value > 0 then
fLastCheckOldLogFilesTimeStamp := 0;
end;
function TMaxLog.GetMaxLogFileAgeInDays: Integer;
begin
Result := fMaxLogFileAgeInDays;
end;
procedure TMaxLog.SetAutoStackTraceForErrors(const Value: Boolean);
begin
fAutoStackTraceForErrors := Value;
end;
function TMaxLog.GetAutoStackTraceForErrors: Boolean;
begin
Result := fAutoStackTraceForErrors;
end;
procedure TMaxLog.SetOutputFormatKind(const Value: TOutputFormatKind);
begin
fOutputFormatKind := Value;
end;
procedure TMaxLog.ShutDown;
begin
{$IFNDEF DISABLE_PRLOGGER }
if fLogWriter = nil then
Exit;
try
Lock;
try
fLogWriter:= nil;
finally
UnLock;
end;
Except
// do nothing
end;
{$ENDIF}
end;
procedure TMaxLog.UnLock;
begin
system.TMonitor.Exit(Self);
end;
function TMaxLog.GetOutputFormatKind: TOutputFormatKind;
begin
Result := fOutputFormatKind;
end;
Procedure TMaxLog.SetDefaultTag(Const Value: String);
begin
fDefaultTag := Value;
end;
Function TMaxLog.GetDefaultTag: String;
begin
Result := fDefaultTag;
end;
{$IFNDEF DISABLE_PRLOGGER }
Procedure TMaxLog.SetLogWriter(Const Value: ILogWriter);
begin
Lock;
try
fLogWriter := Value; // Allow replacing the writer
finally
UnLock;
end;
end;
Function TMaxLog.GetLogWriter: ILogWriter;
begin
Result := fLogWriter;
end;
{$ENDIF}
Function TMaxLog.CloneWithTag(Const aNewTag: String): iMaxLog;
begin
// Use the private cloning constructor
Result := TMaxLog.CreateClone(Self, aNewTag);
end;
Function TMaxLog.LogENtry(Const aMsg: String): iLogEntry;
begin
// Create a new log entry object
Result := TLogEntry.Create;
// Optionally add the initial message
if aMsg <> '' then
Result.msg(aMsg);
end;
procedure TMaxLog.Lock;
begin
system.TMonitor.Enter(Self);
end;
Function TMaxLog.LogEnterExitProc(Const aProcName: String;
aLogType: TLogType): iLogEntry;
var
lEntryHelper: TDoLogOnRelease; // Use specific type
lMsgEntry: TLogEntry; // Use specific type
begin
// Log the entry message immediately
lMsgEntry := TLogEntry.Create;
lMsgEntry.put('EnterProc', aProcName);
Self.add(lMsgEntry, aLogType); // Use Self to call the instance's add method
// Create the helper object that logs on destruction
lEntryHelper := TDoLogOnRelease.Create(Self); // Pass Self (iMaxLog)
lEntryHelper.put('ExitProc', aProcName); // Set the exit message tag
lEntryHelper.fLogType := aLogType; // Set the log level for the exit message
Result := lEntryHelper; // Return the helper interface
end;
Procedure TMaxLog.addRaw(Const aJsonLogEntryText: String; aLogType: TLogType);
begin
// Check if it's time to delete old log files
CheckDeleteOldLogFiles;
{$IFNDEF DISABLE_PRLOGGER }
// Use LoggerPro if enabled
Lock;
try
if Assigned(fLogWriter) then
fLogWriter.Log(aLogType, aJsonLogEntryText, fDefaultTag)
else if assigned(fParentInstance) then
begin
fParentInstance.Lock;
try
if Assigned(fParentInstance.LogWriter) then
fParentInstance.LogWriter.Log(aLogType, aJsonLogEntryText, fDefaultTag);
finally
fParentInstance.UnLock;
end;
end;
finally
UnLock;
end;
{$ELSE}
// Simple file logging if LoggerPro is disabled
var f: TextFile;
var fn := Self.fLogDir + ChangeFileExt(ExtractFileName(MaxLogic.ioUtils.GetCurrentDLLName), '.log');
// Note: Simplified filename without PID/Tag if LoggerPro is disabled
System.Assign(f, fn);
try
if TFile.Exists(fn) then
System.Append(f) // Append if file exists
else
System.Rewrite(f); // Create new file otherwise
System.WriteLn(f, aJsonLogEntryText); // Write the log line
System.Flush(f); // Ensure it's written to disk
finally
System.CloseFile(f); // Close the file handle
end;
{$ENDIF}
end;
Procedure TMaxLog.add(aMsg: iLogEntry; aLogType: TLogType);
begin
{$IFDEF MadExcept}
// Automatically add stack trace for errors if enabled and MadExcept is defined
if fAutoStackTraceForErrors and (aLogType = TLogType.Error) then
begin
if (aMsg.CallStack = '') then
aMsg.CallStack := MadStackTrace.StackTrace;
end;
{$EndIf}
// Set the log type on the entry before formatting
aMsg.LogType := aLogType;
// Format the entry and pass it to addRaw
addRaw(aMsg.Text(fOutputFormatKind), aLogType);
end;
Procedure TMaxLog.add(aMsg: iLogEntry);