Skip to content

Commit a8d4b6e

Browse files
author
mattia72
committed
feat: enhance logging functionality with error handling and message display
1 parent 24f1a60 commit a8d4b6e

1 file changed

Lines changed: 57 additions & 15 deletions

File tree

src/Tools/RipGrepper.Tools.DebugUtils.pas

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ TTraceFilterTypeRec = record
1919

2020
const
2121
TRACE_TYPES : array [0 .. 7] of TTraceFilterTypeRec = (
22-
{ } (name : 'tftError'; Value : tftError),
23-
{ } (name : 'tftWarning'; Value : tftWarning),
24-
{ } (name : 'tftInfo'; Value : tftInfo),
25-
{ } (name : 'tftBegin'; Value : tftBegin),
26-
{ } (name : 'tftEnd'; Value : tftEnd),
27-
{ } (name : 'tftVerbose'; Value : tftVerbose),
28-
{ } (name : 'tftRegex'; Value : tftRegex),
29-
{ } (name : 'tftNone'; Value : tftNone)
30-
{ } );
22+
{ } (name : 'tftError'; Value : tftError),
23+
{ } (name : 'tftWarning'; Value : tftWarning),
24+
{ } (name : 'tftInfo'; Value : tftInfo),
25+
{ } (name : 'tftBegin'; Value : tftBegin),
26+
{ } (name : 'tftEnd'; Value : tftEnd),
27+
{ } (name : 'tftVerbose'; Value : tftVerbose),
28+
{ } (name : 'tftRegex'; Value : tftRegex),
29+
{ } (name : 'tftNone'; Value : tftNone)
30+
{ } );
3131

3232
type
3333
TDebugUtils = class(TObject)
34-
private
34+
private const
35+
MAX_LOG_FILE_ERROR_COUNT = 10;
36+
3537
class var
3638
FTraceFilerTypes : TTraceFilterTypes;
3739
FDebugTraceInactiveMsgShown : Boolean;
@@ -42,6 +44,9 @@ TDebugUtils = class(TObject)
4244
FLogLock : TCriticalSection;
4345
FLogFileCreationMode : ELogFileCreationMode;
4446
FLogFileSettingsApplied : Boolean;
47+
FLogFileErrorMsgShown : Boolean;
48+
FLogFileErrorCount : Integer;
49+
FLogFileWriteDisabled : Boolean;
4550
FIsFinalized : Boolean;
4651

4752
class constructor Create;
@@ -51,6 +56,7 @@ TDebugUtils = class(TObject)
5156
class procedure writeToLogFile(const _s : string);
5257
class procedure openLogFile();
5358
class procedure closeLogFile();
59+
class procedure showLogFileErrorMsgOnce(const _errorMsg : string);
5460
class function GetTimestampedLogFilePath(const _basePath : string) : string;
5561
class procedure ApplyLogFileSettings();
5662

@@ -85,7 +91,7 @@ TDebugMsgBeginEnd = record
8591
procedure MsgIf(const _bCondition : Boolean; const _sMsg : string; const _type : ETraceFilterType = tftInfo);
8692
procedure MsgFmt(const _s : string; const _args : array of const; const _type : ETraceFilterType = tftInfo);
8793
procedure MsgFmtIf(const _bCondition : Boolean; const _s : string; const _args : array of const;
88-
const _type : ETraceFilterType = tftInfo);
94+
const _type : ETraceFilterType = tftInfo);
8995
class function New(const _sProcName : string; const _bSilent : Boolean = False) : TDebugMsgBeginEnd; static;
9096
class operator Finalize(var Dest : TDebugMsgBeginEnd);
9197
end;
@@ -98,6 +104,7 @@ implementation
98104
System.SysUtils,
99105
RipGrepper.Settings.RipGrepperSettings,
100106
RipGrepper.Settings.AppSettings,
107+
RipGrepper.Helper.UI,
101108
System.RegularExpressions,
102109
RipGrepper.Common.Constants,
103110
Spring.DesignPatterns;
@@ -110,6 +117,9 @@ implementation
110117
FLogFileWriter := nil;
111118
FLogFileCreationMode := lfcmRecreateOnStart;
112119
FLogFileSettingsApplied := False;
120+
FLogFileErrorMsgShown := False;
121+
FLogFileErrorCount := 0;
122+
FLogFileWriteDisabled := False;
113123
{$IFDEF DEBUG}
114124
FTraceFilerTypes := [tftBegin, tftEnd, tftError, tftWarning, tftInfo, tftVerbose];
115125
{$ENDIF}
@@ -161,18 +171,50 @@ class procedure TDebugUtils.closeLogFile();
161171
FreeAndNil(FLogFileWriter);
162172
end;
163173

174+
class procedure TDebugUtils.showLogFileErrorMsgOnce(const _errorMsg : string);
175+
begin
176+
if FLogFileErrorMsgShown then begin
177+
Exit;
178+
end;
179+
FLogFileErrorMsgShown := True;
180+
181+
TThread.Queue(nil,
182+
procedure
183+
begin
184+
TMsgBox.ShowError('Cannot write debug log file.' + sLineBreak + sLineBreak + 'Path: ' + FLogFilePath, 'Log File Error',
185+
'Detailed error', _errorMsg);
186+
end);
187+
end;
188+
164189
class procedure TDebugUtils.writeToLogFile(const _s : string);
190+
const
191+
FNAME = 'TDebugUtils.writeToLogFile';
165192
begin
166-
if not Assigned(FLogLock) then
193+
if FLogFileWriteDisabled then begin
194+
OutputDebugString(PChar(FNAME + ': FLogFileWriteDisabled'));
167195
Exit;
196+
end;
197+
if not Assigned(FLogLock) then begin
198+
OutputDebugString(PChar(FNAME + ': not Assigned(FLogLock)'));
199+
Exit;
200+
end;
168201
FLogLock.Enter;
169202
try
170203
try
171204
openLogFile();
172205
FLogFileWriter.WriteLine(_s);
173206
except
174207
on E : Exception do begin
175-
OutputDebugString(PChar('Log file write error: ' + E.Message));
208+
Inc(FLogFileErrorCount);
209+
if FLogFileErrorCount >= MAX_LOG_FILE_ERROR_COUNT then begin
210+
FLogFileWriteDisabled := True;
211+
closeLogFile();
212+
end;
213+
OutputDebugString(PChar(FNAME + ': Log file write error: ' + E.Message));
214+
if FLogFileWriteDisabled then begin
215+
OutputDebugString(PChar(FNAME + ': Log file writing disabled after ' + IntToStr(MAX_LOG_FILE_ERROR_COUNT) + ' errors.'));
216+
end;
217+
showLogFileErrorMsgOnce(E.Message);
176218
end;
177219
end;
178220
finally
@@ -283,7 +325,7 @@ class procedure TDebugUtils.UpdateTraceActive;
283325
ApplyLogFileSettings();
284326
end;
285327
OutputDebugString(PChar(APPNAME + ' DebugTraceActive [' +
286-
{ } TraceTypesToStr(FTraceFilerTypes) + '] RegEx: "' + FTraceFilterRegex + '"'));
328+
{ } TraceTypesToStr(FTraceFilerTypes) + '] RegEx: "' + FTraceFilterRegex + '"'));
287329
end;
288330

289331
class function TDebugUtils.GetTimestampedLogFilePath(const _basePath : string) : string;
@@ -370,7 +412,7 @@ procedure TDebugMsgBeginEnd.MsgFmt(const _s : string; const _args : array of con
370412
end;
371413

372414
procedure TDebugMsgBeginEnd.MsgFmtIf(const _bCondition : Boolean; const _s : string; const _args : array of const;
373-
const _type : ETraceFilterType = tftInfo);
415+
const _type : ETraceFilterType = tftInfo);
374416
begin
375417
if _bCondition then
376418
TDebugUtils.MsgFmt(FProcName + ' - ' + _s, _args, _type);

0 commit comments

Comments
 (0)