-
Notifications
You must be signed in to change notification settings - Fork 13
/
nosodebug.pas
447 lines (394 loc) · 11.2 KB
/
nosodebug.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
unit nosodebug;
{
Nosodebug 1.3
September 22th, 2023
Unit to implement debug functionalities on noso project apps.
}
{$mode ObjFPC}{$H+}
INTERFACE
uses
Classes, SysUtils;
type
Tperformance = Record
tag : string;
Start : int64;
Average : int64;
Max : int64;
Min : int64;
Count : int64;
Total : int64;
end;
TLogND = record
tag : string;
Count : integer;
ToDisk : boolean;
Filename : string;
end;
TCoreManager = record
ThName : string;
ThStart : int64;
ThLast : int64;
end;
TFileManager = Record
FiType : string;
FiFile : string;
FiPeer : string;
FiLast : int64;
end;
TProcessCopy = array of TCoreManager;
TFileMCopy = array of TFileManager;
Procedure BeginPerformance(Tag:String);
Function EndPerformance(Tag:String):int64;
Function PerformanceToFile(Destination:String):boolean;
Procedure CreateNewLog(LogName: string; LogFileName:String = '');
Procedure ToLog(LogTag,NewLine : String);
Function GetLogLine(LogTag:string;out LineContent:string):boolean;
Procedure AddNewOpenThread(ThName:String;TimeStamp:int64);
Procedure UpdateOpenThread(ThName:String;TimeStamp:int64);
Procedure CloseOpenThread(ThName:String);
Function GetProcessCopy():TProcessCopy;
Procedure AddFileProcess(FiType, FiFile, FiPeer:String;TimeStamp:int64);
Function CloseFileProcess(FiType, FiFile, FiPeer:String;TimeStamp:int64):int64;
Function GetFileProcessCopy():TFileMCopy;
Procedure InitDeepDeb(LFileName:String;SysInfo:String='');
Procedure ToDeepDeb(LLine:String);
Function GetDeepDebLine(out LineContent:string):boolean;
var
ArrPerformance : array of TPerformance;
NosoDebug_UsePerformance : boolean = false;
ArrNDLogs : Array of TLogND;
ArrNDCSs : array of TRTLCriticalSection;
ArrNDSLs : array of TStringList;
ArrProcess : Array of TCoreManager;
CS_ThManager : TRTLCriticalSection;
ArrFileMgr : Array of TFileManager;
CS_FileManager : TRTLCriticalSection;
SLDeepDebLog : TStringList;
CS_DeepDeb : TRTLCriticalSection;
DeepDebFilename: String = '';
IMPLEMENTATION
{$REGION Performance}
{Starts a performance measure}
Procedure BeginPerformance(Tag:String);
var
counter : integer;
NewData : TPerformance;
Begin
if not NosoDebug_UsePerformance then exit;
for counter := 0 to high(ArrPerformance) do
begin
if Tag = ArrPerformance[counter].tag then
begin
ArrPerformance[counter].Start:=GetTickCount64;
Inc(ArrPerformance[counter].Count);
exit;
end;
end;
NewData := default(TPerformance);
NewData.tag :=tag;
NewData.Min :=99999;
NewData.Start :=GetTickCount64;
NewData.Count :=1;
Insert(NewData,ArrPerformance,length(ArrPerformance));
End;
{Ends a performance}
Function EndPerformance(Tag:String):int64;
var
counter : integer;
duration : int64 = 0;
Begin
result := 0;
if not NosoDebug_UsePerformance then exit;
for counter := 0 to high(ArrPerformance) do
begin
if tag = ArrPerformance[counter].tag then
begin
duration :=GetTickCount64-ArrPerformance[counter].Start;
ArrPerformance[counter].Total := ArrPerformance[counter].Total+Duration;
ArrPerformance[counter].Average:=ArrPerformance[counter].Total div ArrPerformance[counter].Count;
if duration>ArrPerformance[counter].Max then
ArrPerformance[counter].Max := duration;
if duration < ArrPerformance[counter].Min then
ArrPerformance[counter].Min := duration;
break;
end;
end;
Result := duration;
End;
Function PerformanceToFile(Destination:String):boolean;
var
counter : integer;
Lines : TStringList;
ThisLine : String;
Tag,count,max,average : string;
Begin
Result := true;
Lines := TStringlist.Create;
Lines.Add('TAG Count Max Average');
for counter := 0 to high(ArrPerformance) do
begin
Tag := Format('%0:-40s',[ArrPerformance[counter].tag]);
Count := Format('%0:10s',[IntToStr(ArrPerformance[counter].Count)]);
Max := Format('%0:10s',[IntToStr(ArrPerformance[counter].Max)]);
Average := Format('%0:10s',[IntToStr(ArrPerformance[counter].Average)]);
ThisLine := Format('%s %s %s %s',[Tag,count,max,average]);
Lines.Add(ThisLine);
end;
Lines.SaveToFile(Destination);
Lines.Free;
End;
{$ENDREGION}
{$REGION Logs}
{private: verify that the file for the log exists}
Function InitializeLogFile(Filename:String;OptionText:string = ''):boolean;
var
LFile : textfile;
Begin
Result := true;
if not fileexists(Filename) then
begin
TRY
Assignfile(LFile, Filename);
rewrite(LFile);
if OptionText <> '' then
Writeln(LFile,OptionText);
Closefile(LFile);
EXCEPT on E:Exception do
begin
ToDeepDeb('Nosodebug,InitializeLogFile,'+E.Message);
Result := false;
end;
END; {Try}
end;
End;
{private: if enabled, saves the line to the log file}
Procedure SaveTextToDisk(TextLine, Filename:String);
var
LFile : textfile;
IOCode : integer;
Begin
Assignfile(LFile, Filename);
{$I-}Append(LFile){$I+};
IOCode := IOResult;
If IOCode = 0 then
begin
TRY
Writeln(LFile, TextLine);
Except on E:Exception do
ToDeepDeb('Nosodebug,SaveTextToDisk,'+E.Message);
END; {Try}
Closefile(LFile);
end
else if IOCode = 5 then
{$I-}Closefile(LFile){$I+};
End;
{Creates a new log and assigns an optional file to save it}
Procedure CreateNewLog(LogName: string; LogFileName:String = '');
var
NewData : TLogND;
Begin
NewData := Default(TLogND);
NewData.tag:=Uppercase(Logname);
NewData.Filename:=LogFileName;
SetLength(ArrNDCSs,length(ArrNDCSs)+1);
InitCriticalSection(ArrNDCSs[length(ArrNDCSs)-1]);
SetLEngth(ArrNDSLs,length(ArrNDSLs)+1);
ArrNDSLs[length(ArrNDSLs)-1] := TStringlist.Create;
if LogFileName <> '' then
begin
InitializeLogFile(LogFileName);
NewData.ToDisk:=true;
end;
Insert(NewData,ArrNDLogs,length(ArrNDLogs));
End;
{Adds one line to the specified log}
Procedure ToLog(LogTag,NewLine : String);
var
counter : integer;
Begin
for counter := 0 to length(ArrNDLogs)-1 do
begin
if ArrNDLogs[counter].tag = Uppercase(LogTag) then
begin
EnterCriticalSection(ArrNDCSs[counter]);
ArrNDSLs[counter].Add(NewLine);
Inc(ArrNDLogs[counter].Count);
LeaveCriticalSection(ArrNDCSs[counter]);
end;
end;
End;
{Retireves the oldest line in the specified log, assigning value to LineContent}
Function GetLogLine(LogTag:string;out LineContent:string):boolean;
var
counter : integer;
Begin
Result:= False;
For counter := 0 to length(ArrNDLogs)-1 do
begin
if ArrNDLogs[counter].tag = Uppercase(LogTag) then
begin
if ArrNDSLs[counter].Count>0 then
begin
EnterCriticalSection(ArrNDCSs[counter]);
LineContent := ArrNDSLs[counter][0];
Result := true;
ArrNDSLs[counter].Delete(0);
if ArrNDLogs[counter].ToDisk then SaveTextToDisk(LineContent,ArrNDLogs[counter].Filename);
LeaveCriticalSection(ArrNDCSs[counter]);
break;
end;
end;
end;
End;
{Private: Free all data at close}
Procedure FreeAllLogs;
var
counter : integer;
Begin
for counter := 0 to length(ArrNDLogs)-1 do
begin
ArrNDSLs[counter].Free;
DoneCriticalsection(ArrNDCSs[counter]);
end;
End;
{$ENDREGION}
{$REGION Thread manager}
Procedure AddNewOpenThread(ThName:String;TimeStamp:int64);
var
NewValue : TCoreManager;
Begin
NewValue := Default(TCoreManager);
NewValue.ThName := ThName;
NewValue.ThStart := TimeStamp;
NewValue.ThLast := TimeStamp;
EnterCriticalSection(CS_ThManager);
Insert(NewValue,ArrProcess,Length(ArrProcess));
LeaveCriticalSection(CS_ThManager);
End;
Procedure UpdateOpenThread(ThName:String;TimeStamp:int64);
var
counter : integer;
Begin
EnterCriticalSection(CS_ThManager);
for counter := 0 to High(ArrProcess) do
begin
if UpperCase(ArrProcess[counter].ThName) = UpperCase(ThName) then
begin
ArrProcess[counter].ThLast:=TimeStamp;
Break;
end;
end;
LeaveCriticalSection(CS_ThManager);
End;
Procedure CloseOpenThread(ThName:String);
var
counter : integer;
Begin
EnterCriticalSection(CS_ThManager);
for counter := 0 to High(ArrProcess) do
begin
if UpperCase(ArrProcess[counter].ThName) = UpperCase(ThName) then
begin
Delete(ArrProcess,Counter,1);
Break;
end;
end;
LeaveCriticalSection(CS_ThManager);
End;
Function GetProcessCopy():TProcessCopy;
Begin
Setlength(Result,0);
EnterCriticalSection(CS_ThManager);
Result := copy(ArrProcess,0,length(ArrProcess));
LeaveCriticalSection(CS_ThManager);
End;
{$ENDREGION}
{$REGION Files manager}
Procedure AddFileProcess(FiType, FiFile, FiPeer:String;TimeStamp:int64);
var
NewValue : TFileManager;
Begin
NewValue := Default(TFileManager);
NewValue.FiType := FiType;
NewValue.FiFile := FiFile;
NewValue.FiPeer := FiPeer;
NewValue.FiLast := TimeStamp;
EnterCriticalSection(CS_FileManager);
Insert(NewValue,ArrFileMgr,Length(ArrFileMgr));
LeaveCriticalSection(CS_FileManager);
End;
Function CloseFileProcess(FiType, FiFile, FiPeer:String;TimeStamp:int64):int64;
var
counter : integer;
Begin
Result := 0;
EnterCriticalSection(CS_FileManager);
for counter := 0 to High(ArrFileMgr) do
begin
if ( (UpperCase(ArrFileMgr[counter].FiType) = UpperCase(FiType)) and
(UpperCase(ArrFileMgr[counter].FiFile) = UpperCase(FiFile)) and
(UpperCase(ArrFileMgr[counter].FiPeer) = UpperCase(FiPeer)) ) then
begin
Result := TimeStamp - ArrFileMgr[counter].FiLast;
Delete(ArrFileMgr,Counter,1);
Break;
end;
end;
LeaveCriticalSection(CS_FileManager);
End;
Function GetFileProcessCopy():TFileMCopy;
Begin
Setlength(Result,0);
EnterCriticalSection(CS_FileManager);
Result := copy(ArrFileMgr,0,length(ArrFileMgr));
LeaveCriticalSection(CS_FileManager);
End;
{$ENDREGION}
{$REGION Deep debug control}
Procedure InitDeepDeb(LFileName:String;SysInfo:String='');
Begin
if DeepDebFilename<>'' then Exit;
if InitializeLogFile(LFileName,SysInfo) then
begin
DeepDebFilename := LFileName;
//ToDeepDeb(SysInfo);
end;
End;
Procedure ToDeepDeb(LLine:String);
Begin
EnterCriticalSection(CS_DeepDeb);
SLDeepDebLog.Add(LLine);
LeaveCriticalSection(CS_DeepDeb);
End;
Function GetDeepDebLine(out LineContent:string):boolean;
Begin
result := false;
if SLDeepDebLog.Count>0 then
begin
EnterCriticalSection(CS_DeepDeb);
LineContent := SLDeepDebLog[0];
SLDeepDebLog.Delete(0);
Result := true;
if DeepDebFilename<>'' then SaveTextToDisk(DateTimeToStr(Now)+' '+LineContent,DeepDebFilename);
LeaveCriticalSection(CS_DeepDeb);
end;
End;
{$ENDREGION}
INITIALIZATION
SLDeepDebLog := TStringList.Create;
Setlength(ArrPerformance,0);
Setlength(ArrNDLogs,0);
Setlength(ArrNDCSs,0);
Setlength(ArrNDSLs,0);
Setlength(ArrProcess,0);
Setlength(ArrFileMgr,0);
InitCriticalSection(CS_ThManager);
InitCriticalSection(CS_FileManager);
InitCriticalSection(CS_DeepDeb);
FINALIZATION
DoneCriticalSection(CS_ThManager);
DoneCriticalSection(CS_FileManager);
DoneCriticalSection(CS_DeepDeb);
FreeAllLogs;
SLDeepDebLog.Free;
END. {END UNIT}