-
Notifications
You must be signed in to change notification settings - Fork 1
/
HotLog.pas
5612 lines (5137 loc) · 193 KB
/
HotLog.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
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 HotLog;
{$WARN SYMBOL_PLATFORM OFF}
(*********************************************************************
v 3.3 (2019-02) Rolf Wetjen ([email protected])
- CheckLogFiles moved to THotLogWriter.Execute
- THotLogWriter.Execute: F is TFileStreamUTF8
- References to unit RWUtils removed
- Additional index FSafeIndex for safe filenames in THLFileDef
- ddname can be used to set a prefix for safe filenames.
Filename in this case: ddname-yyyymmddd-hhnnss-FSafeIndex
- SafeGdgMax now working
- UseFileSizeLimit, FUseSizeLimit removed.
Set LogFileMaxSize > 0 instead.
- References to CodeSiteLogging removed.
v 3.2.1 (2017-07) Rolf Wetjen ([email protected])
- Bug fixing
v 3.2 (2017-01) Rolf Wetjen ([email protected])
- FormatDateTime(..,Now) replaced with DateTimeToStr
- THotLogWriter CreateFile & OpenFile with flag FILE_FLAG_WRITE_THROUGH
v 3.1 (2016-05) Rolf Wetjen ([email protected])
- No need to call THotLog.StartLogging. THotLog.Add... will do this.
- New property THotLog.Started
Bug fixing
v 3.0 (2016-04) Rolf Wetjen ([email protected])
- Support for Delphi XE7
- Logfile is UTF8 encoded
- Heap monitoring changed (GetMemoryManagerState)
- Ram monitoring changed (GlobalMemoryStatusEx)
- Unicode support for "array of const" functions
- {disk...} tags show information for all disks
- Delphi & Lazarus
- {app_prm} is now a standalone tag
Bug fixing, of course
Thanks to Olivier Touzot for earlier versions.
*********************************************************************)
{ ****************************************************************** }
{ }
{ Delphi (6&7) unit -- LogFile manager, buffered and }
{ multithreaded. }
{ }
{ Copyright © 2004 by Olivier Touzot "QnnO" }
{ (http://mapage.noos.fr/qnno/delphi_en.htm - [email protected]) }
{ }
{ ---------------------------- }
{ }
{ v 2.0 (2005-07-10). }
{ Thanks to Robert Becker's work, ([email protected]) the log }
{ file size can now be limited, and Hotlog will create a new one }
{ once a limit is reached. }
{ v 2.1 (2005-07-24). }
{ Support for Delphi 5 added by Femi Fadayomi. }
{ v 2.2 (2005-07-24). Luis Gonzalo Constantini Von Rickel }
{ - Faster Memo Cleanning, Stop Repainting During Process, and do }
{ the Scroll to the Last Line of the Memo. }
{ - TFileStream Object Creating is placed outside try...finally }
{ block to avoid Access Violation Exception if the object can not}
{ be created. }
{ }
{ Older versions fixed several bugs, see Readme.txt for history. }
{ ****************************************************************** }
// This unit is freeware, but under copyrights that remain mine for my
// parts of the code, and original writters for their parts of the code.
// This is mainly the case about "variant open array parameters"
// copying routines, that come from Rudy Velthuis' pages on the web at :
// http://rvelthuis.bei.t-online.de/index.htm
// This unit can be freely used in any application, freeware, shareware
// or commercial. However, I would apreciate your sending me an email if
// you decide to use it. Of course, you use it under your own and single
// responsability. Neither me, nor contributors, could be held responsible
// for any problem resulting from the use of this unit. ;-)
// It can also be freely distributed, provided this licence and the copyright
// notice remains within it unchanged, and its help file is distributed
// with it too.
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
interface
uses Classes,
{$IFDEF FPC}
LCLIntf,
{$ELSE}
Windows,
{$ENDIF}
Messages,
SysUtils,
StrUtils,
Math,
Forms, // needed by "application.XXX"
Registry, // SysInfo, Timing
StdCtrls; // needed for visual feedback;
// --- Rolf
// ExtCtrls; // need for LogFileTimer - rbecker
const
//Windows messaging sub system:
UM_HLSOON_USED = 0; // Change this, to slide HotLog's UM values if needed
//parser
UM_HLSERVICEMSG = WM_USER + UM_HLSOON_USED + 10; // Parser wake-up message,
UM_HLACTIONMSG = WM_USER + UM_HLSOON_USED + 11; // Parser, line(s) to add.
//writer & feedBack
UM_HLSIMPLESTRING = WM_USER + UM_HLSOON_USED + 12; // Writer thread; decides what to...
UM_HLSTRINGLIST = WM_USER + UM_HLSOON_USED + 13; // ...work with (String or StringList)
//UM_STARTNEWLOG = WM_USER + UM_HLSOON_USED + 14; // Rolf // writer thread, start a new log file - rbecker
//messages for Parser : wParam ("job to do")
HL_LINENUMCHANGED = 0;
HL_SIMPLESTRING = 1;
HL_PARSEEXCEPTION = 2;
HL_JUMPLINES = 3;
HL_PARSELINE = 4;
HL_PARSELIST = 5;
HL_PARSEARRAY = 6;
HL_HEAPMONITOR = 7;
HL_RAMMONITOR = 8;
HL_DSKMONITOR = 9;
HL_RESETHEAPTABLE = 10;
HL_RESETRAMTABLE = 11;
HL_RESETRULER = 12;
// Parser tags
HLT_AND = 38; HLT_PAD = 42; HLT_AT = 64;
HLT_RAM = 16799; HLT_HEAP = 38281; HLT_DISK = 38039;
HLT_RAM1 = 28319; HLT_HEAP1 = 38326; HLT_DISK1 = 38084;
HLT_RAM2 = 28364; HLT_HEAP2 = 49846; HLT_DISK2 = 49604;
HLT_RAM3 = 39884; HLT_HEAP3 = 49891; HLT_DISK3 = 49649;
HLT_CPUI = 39320; HLT_OSVI = 40101; HLT_MEM = 17818;
HLT_TIMERS = 57843; HLT_DHG = 18571; HLT_DTE = 21641;
HLT_GTC = 21642; HLT_HMS = 19867; HLT_LNUM = 39841;
HLT_NOW = 20389; HLT_CRLF = 47; HLT_LNUMOFF = 57910;
HLT_APP_NAME = 79404; HLT_APP_PATH = 80181; HLT_APP_LFN = 63019;
HLT_APP_VER = 62777; HLT_RULER = 39664; HLT_RULER1 = 50672;
HLT_APP_PRM = 66094; APP_PRM_LN = 77614;
//other const
MB = 1024*1024; // Bytes conversion to Megs.
HL_MAX_INT = High(integer);
FROM_LINE : boolean = true;
FROM_LIST : boolean = false;
(* begin rbecker *)
// timer delays
OneSecond = 1000; //in milleseconds
OneMinute = OneSecond * 60;
OneHour = OneMinute * 60;
OneHalfDay = OneHour * 12;
OneDay = OneHalfDay * 2;
// log file sizes - approximate
OneKilobyte = 1024;
OneMegabyte = OneKilobyte * 1024; //probably the most reasonable size
TenMegabyte = OneMegabyte * 10;
(* end rbecker *)
//VarRec -> String
Bool2Str : array[boolean] of string = ('false', 'true');
// --- Rolf
{$IFDEF FPC}
vTypeDesc : array[0..18] of string = (
'vtInteger', 'vtBoolean', 'vtChar', 'vtExtended',
'vtString', 'vtPointer', 'vtPChar', 'vtObject',
'vtClass', 'vtWideChar', 'vtPWideChar', 'vtAnsiString',
'vtCurrency', 'vtVariant', 'vtInterface', 'vtWideString',
'vtInt64', 'vtQWord', 'vtUnicodeString');
vTypeAsSring : array[0..19] of string = (
'LongInt', 'Boolean', 'Char', 'Extended',
'ShortString', 'Pointer', 'PChar', 'TObject',
'TClass', 'WideChar', 'PWideChar', 'AnsiString',
'Currency', 'Variant', 'Interface', 'WideString',
'int64', 'QWord', 'UnicodeString', '#HLType');
{$ELSE}
vTypeDesc : array[0..17] of string = (
'vtInteger', 'vtBoolean', 'vtChar',
'vtExtended', 'vtString', 'vtPointer', 'vtPChar',
'vtObject', 'vtClass', 'vtWideChar', 'vtPWideChar',
'vtAnsiString', 'vtCurrency', 'vtVariant', 'vtInterface',
'vtWideString', 'vtInt64', 'vtUnicodeString');
vTypeAsSring : array[0..18] of string = (
'Integer', 'Boolean', 'AnsiChar',
'Extended', 'ShortString', 'Pointer', 'AnsiChar',
'TObject', 'TClass', 'WideChar', 'WideChar',
'AnsiString', 'Currency', 'Variant', 'Interface',
'WideString', 'int64', 'UnicodeString', '#HLType');
{$ENDIF}
TimeScale2Str: array[0..3] of string = ('s.', 'ms', 'µs', 'ns');
TimerUnit2Str: array[0..3] of string = ( 's.', 'ms.', '"units"', 'cycles' );
type
{Types généraux}
// --- Rolf
TVarRecValue = record
vType: integer;
Value: string;
end;
//TConstArray = array of TVarRec; // "Variant open array parameters"
TConstArray = array of TVarRecValue;
TMonitorKind = (mkHeap, mkRam);
TVarRecStyle = (vsNone, vsBasic, vsExtended); // Wanted output format for VarRec values
TInt4Array = array[0..3] of integer;
TParseItemKind = (ikStr, ikTag); // Kind of a part of a string ("TParseItem"), before parsing
PParseItem = ^TParseItem;
TParseItem = record // item (to) parse(d)
kind: TParseItemKind; // (ikTag | ikStr);
sValue: string; // Tag or string original | parsed value ;
iValue: cardinal; // Tag value, after convertion to a unique integer identifier
isSingle: boolean; // Filters inLine / standAlone tags
end;
//Heap and ram monitoring
// --- Rolf
PHeapMRec = ^THeapMRec;
THeapMRec = record
Extra: string;
case integer of
// Heap
{$IFDEF FPC}
0: (MaxHeapSize: PtrUInt; // Maximium allowed size for the heap, in bytes
MaxHeapUsed: PtrUInt; // Maximum used size for the heap, in bytes
CurrHeapSize: PtrUInt; // Current heap size, in bytes
CurrHeapUsed: PtrUInt; // Currently used heap size, in bytes
CurrHeapFree: PtrUInt); // Currently free memory on heap, in bytes
{$ELSE}
0: (AllocatedSmallBlockCount: cardinal;
TotalAllocatedSmallBlockSize: NativeUInt;
ReservedSmallBlockAddressSpace: NativeUInt;
AllocatedMediumBlockCount: cardinal;
TotalAllocatedMediumBlockSize: NativeUInt;
ReservedMediumBlockAddressSpace: NativeUInt;
AllocatedLargeBlockCount: cardinal;
TotalAllocatedLargeBlockSize: NativeUInt;
ReservedLargeBlockAddressSpace: NativeUInt);
{$ENDIF}
// Ram
1: (MemoryLoad: cardinal;
TotalPhysical: UInt64;
AvailPhysical: UInt64;
TotalPageFile: UInt64;
AvailPageFile: UInt64;
TotalVirtual: UInt64;
AvailVirtual: UInt64);
end;
THeapMonitor = class (TObject)
private
FKindOfVals: TMonitorKind;
heapRecords: array of THeapMRec;
// --- Rolf
procedure AddRec (rec: THeapMRec);
procedure ResetMemTable;
function GetFirstEntry: THeapMRec;
public
constructor Create (asKind: TMonitorKind);
destructor Destroy; override;
end;
//Timer
TQTimeScale = (tsSeconds, tsMilliSec, tsMicroSec, tsNanosSec);
TQTimerAction = (taStart,taStop,taGlobalStop);
TQTimerWanted = (twStart,twStop,twDelta);
TQTimerKind = (tkHMS,tkGTC,tkQPC,tkRDT);
TQTimerEntry = record
tStart,
tStop: int64;
kind: TQTimerKind;
end;
TQTimer = class (TObject)
private
FEntry: array of TQTimerEntry;
FIsFinalized: boolean;
FRegFrq, // Registry stored frequency * 1000000
FQpcFrq, // Returned value of QueryPerformanceFrequency
FRDTOverhead, // direct ASM call overhead
FQpcOverhead: int64; // direct API call overhead
//Internals
function GetCount: integer;
function GetStartAsInt (ix: integer): int64;
function GetStopAsInt (ix: integer): int64;
function GetDeltaAsInt (ix: integer): int64;
function GetStartAsStr (ix: integer): string;
function GetStopAsStr (ix: integer): string;
function GetDeltaAsStr (ix: integer): string;
function GetFormatedDeltaAsExt (ix: integer): extended;
function GetFormatedDeltaAsStr (ix: integer): string;
function GetEntry (ix: integer; wanted: TQTimerWanted): int64;
function Overhead (ix: integer): integer;
function GetTimeMeasureAs (timeScale: TQTimeScale; measure: int64; counter: TQTimerKind): real;
function GetOptimalMeasure (measure: int64; counter: TQTimerKind; var tScale: TQTimeScale): real;
procedure Store (value: int64; inOrOut: TQTimerAction; timerKind: TQTimerKind);
function Int64ToTime (value: int64): string;
procedure InitTimerEnvir;
public
fmtOutput: string; // Used for the Format() function ; Defaults to '%3.9n'
timeUnitsWanted: set of TQTimeScale;
removeOverHead,
deltaShowNatives, // outputs deltas in native units;
deltaShowOptimal: boolean; // outputs deltas in the most "readable" unit possible;
constructor Create;
destructor Destroy; override;
function HMS (startOrStop: TQTimerAction = taStart): TDateTime;
function GTC (startOrStop: TQTimerAction = taStart): integer;
function QPC (startOrStop: TQTimerAction = taStart): int64;
function RDT (startOrStop: TQTimerAction = taStart): int64;
procedure GlobalStop;
procedure Reset;
property isFinalized: boolean read FIsFinalized;
property RegistryFreq: int64 read FRegFrq;
property QueryPerfFreq: int64 read FQpcFrq;
property ReaDTSCOverhead: int64 read FRDTOverhead;
property QueryPerfOverhead: int64 read FQpcOverhead;
property Count: integer read GetCount;
property iStart [ix: integer]: int64 read GetStartAsInt;
property iStop [ix: integer]: int64 read GetStopAsInt;
property iDelta [ix: integer]: int64 read GetDeltaAsInt;
property sStart [ix: integer]: string read GetStartAsStr;
property sStop [ix: integer]: string read GetStopAsStr;
property sDelta [ix: integer]: string read GetDeltaAsStr;
property iDeltaFmt [ix: integer]: extended read GetFormatedDeltaAsExt;
property sDeltaFmt [ix: integer]: string read GetFormatedDeltaAsStr;
end;
//log file name management
PFlName = ^FlName;
FlName = record // Will temporary store existing files names...
fullName: TFileName; // ...descriptions in order to manage generations.
ext3: string;
isGdg: boolean;
end;
THLFileDef = class (TObject) // Log file name and acces mode definition
private
//fields
FPath, // Log path
FDdn, // log name, without path nor ext
FExt, // log extention. '.log' by default if non Gdg, Gdg count otherwise
FLFN: string; // Long file name
FGdgMax: word; // max Number of generations to keep. 0 < Gdg < 999. //0 <=> no Gdg
FSafeGdgMax: word; // same, but for safe log file names - rbecker
FDoAppend: boolean; // Shall log be emptied or not ? , if exists. Ignored if FGdgMax > 0
FBuild: boolean; // "BuildFileName" function soon called or not;
// --- Rolf: not used
// FUseSizeLimit: boolean; // close log file and start a new one when size limit exceeded? - rbecker
FLogFileMaxSize: integer; // maximum size of the log file, used for continuous operation - rbecker
FUseSafeFileNames: boolean; // use a unique name for each log file rather than generational - rbecker
// --- Rolf
FSafeIndex: cardinal; // Additional index to make safe filenames unique
FSafePrefix: string; // Use ddname property for safe file names too
//procs & functions
procedure SetPath (Value: string);
procedure SetExt (Value: string);
procedure SetFileName (lfn: TFileName);
procedure SetDoAppend (Value: boolean);
procedure SetGdgMax (Value: word);
function BuildFileName: TFileName;
function GetGdg (sL: TList): string;
function GetFileName: TFileName;
function GetOpenMode: word;
function FileInUse (f: string): boolean;
public
constructor Create;
destructor Destroy; override;
property ddname: string read FDdn write FDdn;
property path: string read FPath write SetPath;
property ext: string read FExt write SetExt;
property fileName: TFileName read GetFileName write SetFileName;
property append: boolean read FDoAppend write SetDoAppend;
property GdgMax: word read FGdgMax write SetGdgMax;
property SafeGdgMax: word read FSafeGdgMax write FSafeGdgMax; //rbecker
property OpMode: word read GetOpenMode;
// --- Rolf: not used
// property UseFileSizeLimit: boolean read FUseSizeLimit write FUseSizeLimit; //rbecker
property LogFileMaxSize: integer read FLogFileMaxSize write FLogFileMaxSize; //rbecker
property UseSafeFilenames: boolean read FUseSafeFileNames write FUseSafeFileNames; //rbecker
procedure BuildSafeFileName; //rbecker
end;{class THLFileDef}
{Internal messages, used for communication between threads}
THLStringMsg = class (TObject) // basic string
private
FHlMsg: string;
procedure Post (toThread: THandle; kindOfString: integer = HL_SIMPLESTRING);
public
constructor Create (s: string);
destructor Destroy; override;
end;
THLConstArrayMsg = class (TObject) // Arrays of const
FConstArray: TConstArray;
FOutputStyle: TVarRecStyle; // basic or extended output
procedure Post (toThread: THandle);
public
constructor Create (outputStyle: TVarRecStyle);
destructor Destroy; override;
end;
THLErrMsg = class (TObject) // Exceptions ans errors to be parsed
private
FEMsg: string;
FlastErr: integer;
Ffull: boolean;
FFunc: string;
Fargs: TConstArray;
procedure Post (toThread: THandle);
public
constructor Create;
destructor Destroy; override;
end;
// --- Rolf
PHLHeapMonMsg = ^THLHeapMonMsg;
THLHeapMonMsg = class (TObject) // Heap & Ram values monitoring message
private
hmr: THeapMRec;
procedure Post (toThread: THandle; RamOrHeap: integer; dirOutput: boolean = false);
public
// Ram
constructor Create (ML: cardinal; TP,AP,TPF,APF,TV,AV: UInt64; ex: string); overload;
// Heap
{$IFDEF FPC}
constructor Create (MHS, MHU,CHS,CHU,CHF: PtrUInt; ex: string); overload;
{$ELSE}
constructor Create (ASBC: cardinal; TASBS, RSBAS: NativeUInt;
AMBC: cardinal; TAMBS, RMBAS: NativeUInt;
ALBC: cardinal; TALBS, RLBAS: NativeUInt;
ex: string); overload;
{$ENDIF}
destructor Destroy; override;
end;
{Thread : Parser}
THotLogParser = class(TThread)
private
mxHLLineNum: THandle;
FStarted: boolean;
FWriterTID: THandle; // Writer ThreadId, needed to post messages to.
//Lines counting
FpvLineCount: cardinal;
FshowLineNum: boolean;
FpvLnNumLen: word;
FShowLNTemp: boolean; // used for temporary override;
FpvMaxValue: cardinal; // ;-)
FpvLnAlign: TAlignment; // line numbers output formatting;
//Ruler
fRulerLength: integer;
fRulerDots,
fRulerNums: string;
fRulerBuild: boolean;
//Error displaying
FErrorCaption: string;
FErrViewStyle: TVarRecStyle; // (vsBasic, vsExtended)
FErrJumpLines,
FErrIsSurrounded: boolean;
FErrSurroundChar,
FErrSurroundLine,
FErrWideSurround: string;
//Timers
FsRegSpeed: string; // Proc speed, from registry
protected
FLnShowNum: boolean;
FLnNumLen: integer;
FLnAlign: TAlignment;
private
HMonitor: THeapMonitor;
RMonitor: THeapMonitor;
//Line Numbers management
procedure UpdateLineNum;
function GetLineNum: string;
function AddLineNum (line: string): string; overload;
procedure AddLineNum (var lst: TStringList); overload;
//Errors display setting
procedure SetFErrCaption (Value: string);
procedure SetFErrSurroundChar (Value: string);
//VarRec handling
// --- Rolf
function VarRecToStr (vrv: TVarRecValue): string;
function ConstArrayToString (const Elements: TConstArray;
style: TVarRecStyle = vsBasic): string;
function ConstArrayToTSList (const Elements: TConstArray): TStringList;
procedure FinalizeConstArray (var Arr: TConstArray);
// procedure FinalizeVarRec (var Item: TVarRec);
function GetBasicValue (s: string): string;
// function GetOriginalValue (s: string): string;
//parsing
function ParseLine (source: string): TStringList;
// --- Rolf
function ParseList (StringList: TStringList): TStringList;
// function ParseList (source: NativeInt): TStringList;
function ParseArray (HLConstArrayMsg: THLConstArrayMsg): TStringList;
// function ParseArray (source: NativeInt): TStringList;
function LineToList (line: string): TList;
function ExtractItem (var source: string; var itm: PParseItem): boolean;
procedure PrepareParsing (var lst: Tlist);
procedure TranslateTags (var lst: Tlist; var omitLNum: boolean);
function PadString (src,tag: string; posPtr: integer): string;
procedure FreeParsedList (var l: TList);
function GetRegValue (root: HKey; key,wanted: string): string;
// --- Rolf
function GetParams: string; overload; // single line
function GetParams (var lst: TList): string; overload; // multi line
function GetVersionAsText: string;
function CurrentNumVer: TInt4Array;
procedure GetRuler (var lst: TList; showNums: boolean = true);
procedure ResetRuler (value: integer);
function GetHeap (PHeapRec: PHeapMRec; outFmt: integer): TStringList; // --- Rolf
function GetRam (PHeapRec: PHeapMRec; outFmt: integer): TStringList; // --- Rolf
function GetDisk (outFmt: integer): TStringList;
procedure GetMemoryStatus (var lst: TList);
procedure GetCPUInfoAsPitem (var lst: Tlist);
procedure GetOSVIAsPitem (var lst: Tlist);
procedure GetRamOrHeapAsPitem (PHeapRec: PHeapMRec; RamOrHeap, outFmt, // --- Rolf
ptr: integer; var lst: Tlist);
procedure GetTimerResolutionsAsPitem (var lst: Tlist);
function ParseException (HLErrMsg: THLErrMsg): TstringList;
property ErrorCaption: string read FErrorCaption write SetFErrCaption;
property SurroundCh: string read FErrSurroundChar write SetFErrSurroundChar;
public
// --- Rolf
constructor Create;
destructor Destroy; override;
procedure Execute; override;
procedure Terminate; // --- Rolf
end;
{Thread : Writer}
// Writer thread will have to partly manage the feedback, in order to :
// -1- Choose whether freeing memory of received lines, or
// -2- Send them to the FeedBackThread, which thus will be in charge of
// freing the memory.
THotLogWriter = class(TThread)
private
FExecuting: boolean; // Sergueï
FStarted,
FDoFeedBack: boolean;
FVisualTID: THandle; // Feedbacker ThreadId
public
hlFileDef: THLFileDef; // The log file name to use
// --- Rolf
constructor Create;
destructor Destroy; override;
procedure Execute; override;
procedure Terminate; // --- Rolf
end;
{Thread : Visual feedback}
THotLogVisual = class(TThread)
private
mxHLFeedBack: THandle; // Feedback suspention control.
FfbMemo: TMemo;
FfbMemoLimit: cardinal; // Oleg;
FDoFeedBack,
FDoScroll: boolean;
// --- Rolf
FStarted: boolean;
s: string; // -> DisplayLine
sl: TStringList; // -> DisplayList
{$ifndef USE_SLOW_METHOD}
procedure ClearExtraLines;
procedure DoTheScroll;
{$endif}
public
constructor Create;
destructor Destroy; override;
procedure Execute; override;
procedure Terminate; // --- Rolf
procedure RemoveCRLF (fromWhat: boolean);
procedure DisplayLine; // Synchronised procs. Execute...
procedure DisplayList; // ... in main thread
end;
{HotLog public interface}
THotLog = class (TObject)
private
FStarted: boolean;
hlParser: THotLogParser; // parser thread
hlVisual: THotLogVisual; // feedBack manager thread
//VarRec handling
// --- Rolf
function CreateConstArray (const Elements: array of const): TConstArray;
function CopyVarRec (const Item: TVarRec): TVarRecValue;
public
hlWriter: THotLogWriter; // made public, to allow direct acces to the log file name definition properties.
header,
footer: TStringList;
// --- Rolf
// LogFileTimer: TTimer; //rbecker
constructor Create;
destructor Destroy; override;
//Settings
procedure DisplayFeedBackInto (aMemo: TMemo);
function StopVisualFeedBack: boolean;
procedure ScrollMemo (doScroll: boolean);
procedure SetMemoLimit (value: cardinal); // Oleg
procedure SetErrorCaption (value: string; surroundChar: string;
jumpLines: boolean = true);
procedure SetErrorViewStyle (style: TVarRecStyle);
procedure SetRulerLength (newLength: integer);
function ModifyLineNumbering (doShow: boolean; alignment: TAlignment = taRightJustify;
size: integer = 3): boolean;
//ram & heap monitoring
procedure ResetHeapTable;
function HeapMonitor (hmExtra: string = '';
directOutput: boolean = false): THeapMRec;
procedure ResetRamTable;
function RamMonitor (hmExtra: string = '';
directOutput: boolean = false): THeapMRec;
//Logging
procedure StartLogging;
procedure JumpLine (LinesToJump: integer = 1);
// Parsing involved
procedure Add (aString: string); overload;
procedure Add (aStringList: TStringList); overload;
procedure Add (style: TVarRecStyle; aConstArray: array of const); overload;
// -No- parsing
procedure AddStr (aString: string);
procedure AddException (ex: Exception; err: integer = 0;
freeAfterPost: boolean = false); overload;
procedure AddException (ex: Exception; func: string; args : array of const;
err: integer = 0; freeAfterPost: boolean = false); overload;
procedure AddError (err: integer); overload;
procedure AddError (err: integer; func: string; args: array of const); overload;
// --- Rolf
// procedure SetLogFileTimerInterval (Interval: cardinal); //rbecker
// procedure OnLogFileTimer (Sender: TObject); //rbecker
// --- Rolf
property Started: boolean read FStarted;
end;
{Thread:Main(VCL)}
function CompareGdgNames (p1, p2: pointer): integer;
function RDTSC: int64; assembler;
var
// hLog itself, alone but proud...
hLog: THotLog;
implementation
uses
{$IFDEF FPC}
Windows, LCLType, Controls, LazUTF8,
JwaWinBase, JwaWinNt,
// --- Rolf
LazUTF8Classes, LazFileUtils;
{$ELSE}
Controls;
{$ENDIF}
{-------------------------------------------------------------------------------
Map UTF8 string functions to simple ones for Delphi
-------------------------------------------------------------------------------}
{$IFNDEF FPC}
function UTF8Copy (const AString: string; const AFrom, ALength: integer): string;
begin
Result:=copy(AString,AFrom,ALength);
end;
function UTF8Pos (SubStr, Source: string): integer;
begin
Result:=Pos(SubStr,Source);
end;
function UTF8LeftStr (const AString: string; const ACount: integer): string;
begin
Result:=LeftStr(AString,ACount);
end;
function UTF8RightStr (const AString: string; const ACount: integer): string;
begin
Result:=RightStr(AString,ACount);
end;
function UTF8Length (const AString: string): integer;
begin
Result:=Length(AString);
end;
function UTF8Encode (const AString: string): string;
begin
Result:=AString;
end;
function UTF8Decode (const AString: string): string;
begin
Result:=AString;
end;
function UTF8StringOfChar (AChar: string; ACount: integer): string;
begin
Result:='';
if AChar='' then
exit;
Result:=StringOfChar(AChar[1],ACount);
end;
function FileExistsUTF8 (const FileName: string): boolean;
begin
Result:=SysUtils.FileExists(FileName);
end;
function FindFirstUTF8 (const Path: string; Attr: Integer; var F: TSearchRec): Integer;
begin
Result:=SysUtils.FindFirst(Path,Attr,F);
end;
function FindNextUTF8 (var F: TSearchRec): Integer;
begin
Result:=SysUtils.FindNext(F);
end;
procedure FindCloseUTF8 (var F: TSearchRec);
begin
SysUtils.FindClose(F);
end;
function DeleteFileUTF8 (const FileName: string): Boolean;
begin
Result:=SysUtils.DeleteFile(FileName);
end;
function SysErrorMessageUTF8 (ErrorCode: Cardinal): string;
begin
Result:=SysUtils.SysErrorMessage(ErrorCode);
end;
type
TFileStreamUTF8 = class(TFileStream);
{$ELSE}
function UnicodeToUtf8 (Dest: PChar; MaxDestBytes: SizeUInt; Source: PUnicodeChar; SourceChars: SizeUInt): SizeUInt;
var
i,j: SizeUInt;
lw: longword;
begin
Result:=0;
if Source=nil then
exit;
i:=0;
j:=0;
if Assigned(Dest) then
begin
while (i<SourceChars) and (j<MaxDestBytes) do
begin
lw:=ord(Source[i]);
case lw of
$0000..$007F: begin
Dest[j]:=Char(lw);
inc(j);
end;
$0080..$07FF: begin
if j+1>=MaxDestBytes then
Break;
Dest[j]:=Char($C0 or (lw shr 6));
Dest[j+1]:=Char($80 or (lw and $3F));
Inc(j,2);
end;
$0800..$D7FF,
$E000..$FFFF: begin
if j+2>=MaxDestBytes then
Break;
Dest[j]:=Char($e0 or (lw shr 12));
Dest[j+1]:=Char($80 or ((lw shr 6) and $3F));
Dest[j+2]:=Char($80 or (lw and $3F));
Inc(j,3);
end;
$D800..$DBFF: begin // High Surrogates
if j+3>=MaxDestBytes then
Break;
if (i+1<SourceChars) and
(word(Source[i+1])>=$DC00) and
(word(Source[i+1])<=$DFFF) then
begin // $D7C0 is ($D800 - ($10000 shr 10))
lw:=(longword(lw-$D7C0) shl 10) + (ord(source[i+1]) xor $DC00);
Dest[j]:=Char($F0 or (lw shr 18));
Dest[j+1]:=Char($80 or ((lw shr 12) and $3F));
Dest[j+2]:=char($80 or ((lw shr 6) and $3F));
Dest[j+3]:=char($80 or (lw and $3F));
Inc(j,4);
Inc(i);
end;
end;
end;
Inc(i);
end;
if j>MaxDestBytes-1 then
j:=MaxDestBytes-1;
Dest[j]:=#0;
end
else
begin
while i<SourceChars do
begin
case word(Source[i]) of
$0000..$007F: Inc(j);
$0080..$07FF: Inc(j,2);
$0800..$D7FF,
$E000..$FFFF: Inc(j,3);
$D800..$DBFF: begin
if (i+1<SourceChars) and
(word(Source[i+1])>=$DC00) and
(word(Source[i+1])<=$DFFF) then
begin
Inc(j,4);
Inc(i);
end;
end;
end;
Inc(i);
end;
end;
Result:=j+1;
end;
function UTF8Encode (const s: UnicodeString; Count: integer = -1): RawByteString; overload;
var
i,m: SizeInt;
u8: UTF8String;
begin
Result:='';
if s='' then
exit;
if Count=0 then
exit;
if Count>0 then
m:=Count
else
m:=min(Length(s),StrLen(pWideChar(s)));
if m<=0 then
exit;
SetLength(u8,m*3);
i:=UnicodeToUTF8(pChar(u8),Length(u8)+1,PUnicodeChar(s),m);
if i>0 then
begin
SetLength(u8,i-1);
Result:=u8;
end;
end;
function UTF8Encode (const s: WideString; Count: integer = -1): RawByteString; overload;
var
i,m: SizeInt;
u8: UTF8String;
begin
Result:='';
if s='' then
exit;
if Count=0 then
exit;
if Count>0 then
m:=Count
else
m:=min(Length(s),StrLen(pWideChar(s)));
if m<=0 then
exit;
SetLength(u8,m*3);
i:=UnicodeToUTF8(pChar(u8),Length(u8)+1,PWideChar(s),m);
if i>0 then
begin
SetLength(u8,i-1);
Result:=u8;
end;
end;
{$ENDIF}
procedure DbgSendMsg (const Message: string); overload;
begin
OutputDebugStringW(pWideChar(UTF8Decode(Message)));
end;
procedure DbgSendMsg (const Message: string; const Args: array of const); overload;
begin
DbgSendMsg(Format(Message,Args));
end;
{$IFDEF FPC}
function QueryPerformanceCounter(var lpPerformanceCount: Int64): LongBool; stdcall;
external kernel32 name 'QueryPerformanceCounter';
function QueryPerformanceFrequency(var lpFrequency: TLargeInteger): BOOL; stdcall;
external kernel32 name 'QueryPerformanceFrequency';
{$ENDIF}
{----------------------------------------------------------}
{--- Threads:Main(VCL) : File definition ---}
{----------------------------------------------------------}
{ Will be used to acces the log file. File name is set ; }
{ If generations have to be managed, -> rename (and needed }
{ deletions) are made. }
{----------------------------------------------------------}
constructor THLFileDef.Create;
begin
inherited;
Path := ExtractFilePath(Application.ExeName);
FDdn := ChangeFileExt(ExtractFileName(Application.ExeName),'');
FExt := '.log';
FGdgMax := 0; // as long as an ext is provided, no Gdg is managed
FDoAppend := false; // if exists, will be overridden
FBuild := false;
(* begin rbecker *)
SafeGdgMax := 0;
// --- Rolf
//UseFileSizeLimit := false;
LogFileMaxSize := 0;
UseSafeFilenames := false;
(* end rbecker *)
// --- Rolf
FSafeIndex := 0;
FSafePrefix := '*'; // Invalid value
end;
destructor THLFileDef.Destroy;
begin
inherited;
end;
function THLFileDef.GetFileName:TFileName;
begin
If (FGdgMax = 0) Or (Self.FBuild)
then result := FPath + FDdn + FExt
else result := BuildFileName;
end;
procedure THLFileDef.SetFileName(lfn:TFileName);
// Sets the full log name
begin
If lfn <> '' then
begin
FPath := ExtractFilePath(lfn); // if not provided...