1
0
mirror of synced 2024-11-23 11:53:50 +00:00

Fixed: FS#22 - Log entries can end up in the wrong file

This commit is contained in:
Mark van Renswoude 2015-03-11 11:45:31 +00:00
parent a901a2d0b6
commit 437655693e
3 changed files with 32 additions and 32 deletions

View File

@ -52,7 +52,6 @@ type
TX2LogObserverWorkerThread = class(TThread) TX2LogObserverWorkerThread = class(TThread)
private private
FFileName: string;
FLogQueue: TObjectQueue<TX2LogQueueEntry>; FLogQueue: TObjectQueue<TX2LogQueueEntry>;
FLogQueueSignal: TEvent; FLogQueueSignal: TEvent;
FThreadStartSignal: TEvent; FThreadStartSignal: TEvent;
@ -66,7 +65,6 @@ type
procedure WaitForEntry; virtual; procedure WaitForEntry; virtual;
procedure ProcessEntry(AEntry: TX2LogQueueEntry); virtual; abstract; procedure ProcessEntry(AEntry: TX2LogQueueEntry); virtual; abstract;
property FileName: string read FFileName;
property LogQueue: TObjectQueue<TX2LogQueueEntry> read FLogQueue; property LogQueue: TObjectQueue<TX2LogQueueEntry> read FLogQueue;
property LogQueueSignal: TEvent read FLogQueueSignal; property LogQueueSignal: TEvent read FLogQueueSignal;
public public

View File

@ -14,28 +14,28 @@ uses
type type
TX2LogFileObserver = class(TX2LogCustomThreadedObserver) TX2LogFileObserver = class(TX2LogCustomThreadedObserver)
private private
FFileName: string; FOutputFileName: string;
protected protected
function CreateWorkerThread: TX2LogObserverWorkerThread; override; function CreateWorkerThread: TX2LogObserverWorkerThread; override;
property FileName: string read FFileName; property OutputFileName: string read FOutputFileName;
public public
constructor Create(const AFileName: string; ALogLevels: TX2LogLevels = X2LogLevelsDefault); constructor Create(const AOutputFileName: string; ALogLevels: TX2LogLevels = X2LogLevelsDefault);
constructor CreateInProgramData(const AFileName: string; ALogLevels: TX2LogLevels = X2LogLevelsDefault); constructor CreateInProgramData(const AOutputFileName: string; ALogLevels: TX2LogLevels = X2LogLevelsDefault);
constructor CreateInUserAppData(const AFileName: string; ALogLevels: TX2LogLevels = X2LogLevelsDefault); constructor CreateInUserAppData(const AOutputFileName: string; ALogLevels: TX2LogLevels = X2LogLevelsDefault);
end; end;
TX2LogFileWorkerThread = class(TX2LogObserverWorkerThread) TX2LogFileWorkerThread = class(TX2LogObserverWorkerThread)
private private
FFileName: string; FOutputFileName: string;
protected protected
function GetFileName: string; virtual; function GetFileName(AEntry: TX2LogQueueEntry): string; virtual;
procedure ProcessEntry(AEntry: TX2LogQueueEntry); override; procedure ProcessEntry(AEntry: TX2LogQueueEntry); override;
property FileName: string read GetFileName; property OutputFileName: string read FOutputFileName;
public public
constructor Create(const AFileName: string); constructor Create(const AOutputFileName: string);
end; end;
@ -52,15 +52,15 @@ uses
{ TX2LogFileObserver } { TX2LogFileObserver }
constructor TX2LogFileObserver.Create(const AFileName: string; ALogLevels: TX2LogLevels); constructor TX2LogFileObserver.Create(const AOutputFileName: string; ALogLevels: TX2LogLevels);
begin begin
FFileName := AFileName; FOutputFileName := AOutputFileName;
inherited Create(ALogLevels); inherited Create(ALogLevels);
end; end;
constructor TX2LogFileObserver.CreateInProgramData(const AFileName: string; ALogLevels: TX2LogLevels); constructor TX2LogFileObserver.CreateInProgramData(const AOutputFileName: string; ALogLevels: TX2LogLevels);
var var
path: PWideChar; path: PWideChar;
@ -68,14 +68,14 @@ begin
GetMem(path, MAX_PATH); GetMem(path, MAX_PATH);
try try
OleCheck(SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, SHGFP_TYPE_CURRENT, path)); OleCheck(SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, SHGFP_TYPE_CURRENT, path));
Create(IncludeTrailingPathDelimiter(path) + AFileName, ALogLevels); Create(IncludeTrailingPathDelimiter(path) + AOutputFileName, ALogLevels);
finally finally
FreeMem(path); FreeMem(path);
end; end;
end; end;
constructor TX2LogFileObserver.CreateInUserAppData(const AFileName: string; ALogLevels: TX2LogLevels); constructor TX2LogFileObserver.CreateInUserAppData(const AOutputFileName: string; ALogLevels: TX2LogLevels);
var var
path: PWideChar; path: PWideChar;
@ -83,7 +83,7 @@ begin
GetMem(path, MAX_PATH); GetMem(path, MAX_PATH);
try try
OleCheck(SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, path)); OleCheck(SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, path));
Create(IncludeTrailingPathDelimiter(path) + AFileName, ALogLevels); Create(IncludeTrailingPathDelimiter(path) + AOutputFileName, ALogLevels);
finally finally
FreeMem(path); FreeMem(path);
end; end;
@ -92,14 +92,14 @@ end;
function TX2LogFileObserver.CreateWorkerThread: TX2LogObserverWorkerThread; function TX2LogFileObserver.CreateWorkerThread: TX2LogObserverWorkerThread;
begin begin
Result := TX2LogFileWorkerThread.Create(FileName); Result := TX2LogFileWorkerThread.Create(OutputFileName);
end; end;
{ TX2LogFileWorkerThread } { TX2LogFileWorkerThread }
constructor TX2LogFileWorkerThread.Create(const AFileName: string); constructor TX2LogFileWorkerThread.Create(const AOutputFileName: string);
begin begin
FFileName := AFileName; FOutputFileName := AOutputFileName;
inherited Create; inherited Create;
end; end;
@ -107,6 +107,7 @@ end;
procedure TX2LogFileWorkerThread.ProcessEntry(AEntry: TX2LogQueueEntry); procedure TX2LogFileWorkerThread.ProcessEntry(AEntry: TX2LogQueueEntry);
var var
fileName: string;
baseReportFileName: string; baseReportFileName: string;
errorMsg: string; errorMsg: string;
detailsExtension: string; detailsExtension: string;
@ -118,7 +119,8 @@ var
logDetailsStreamable: IX2LogDetailsStreamable; logDetailsStreamable: IX2LogDetailsStreamable;
begin begin
ForceDirectories(ExtractFilePath(FileName)); fileName := GetFileName(AEntry);
ForceDirectories(ExtractFilePath(fileName));
if Length(AEntry.Category) > 0 then if Length(AEntry.Category) > 0 then
errorMsg := Format(GetLogResourceString(@LogFileLineCategory), [AEntry.Message, AEntry.Category]) errorMsg := Format(GetLogResourceString(@LogFileLineCategory), [AEntry.Message, AEntry.Category])
@ -127,8 +129,8 @@ begin
if Supports(AEntry.Details, IX2LogDetailsStreamable, logDetailsStreamable) then if Supports(AEntry.Details, IX2LogDetailsStreamable, logDetailsStreamable) then
begin begin
detailsExtension := ExtractFileExt(FileName); detailsExtension := ExtractFileExt(fileName);
baseReportFileName := ChangeFileExt(FileName, '_' + FormatDateTime(GetLogResourceString(@LogFileNameDateFormat), AEntry.DateTime)); baseReportFileName := ChangeFileExt(fileName, '_' + FormatDateTime(GetLogResourceString(@LogFileNameDateFormat), AEntry.DateTime));
detailsFileName := baseReportFileName + detailsExtension; detailsFileName := baseReportFileName + detailsExtension;
detailsNumber := 0; detailsNumber := 0;
@ -174,7 +176,7 @@ begin
end; end;
{ Append line to log file } { Append line to log file }
writer := TFile.AppendText(FileName); writer := TFile.AppendText(fileName);
try try
writer.WriteLine('[' + FormatDateTime(GetLogResourceString(@LogFileLineDateFormat), AEntry.DateTime) + '] ' + writer.WriteLine('[' + FormatDateTime(GetLogResourceString(@LogFileLineDateFormat), AEntry.DateTime) + '] ' +
GetLogLevelText(AEntry.Level) + ': ' + errorMsg); GetLogLevelText(AEntry.Level) + ': ' + errorMsg);
@ -184,9 +186,9 @@ begin
end; end;
function TX2LogFileWorkerThread.GetFileName: string; function TX2LogFileWorkerThread.GetFileName(AEntry: TX2LogQueueEntry): string;
begin begin
Result := FFileName; Result := FOutputFileName;
end; end;
end. end.

View File

@ -35,7 +35,7 @@ type
FFormatSettings: TFormatSettings; FFormatSettings: TFormatSettings;
FDateFormat: string; FDateFormat: string;
protected protected
function GetFileName: string; override; function GetFileName(AEntry: TX2LogQueueEntry): string; override;
function GetDateFileName(ADate: TDateTime): string; function GetDateFileName(ADate: TDateTime): string;
procedure ProcessEntry(AEntry: TX2LogQueueEntry); override; procedure ProcessEntry(AEntry: TX2LogQueueEntry); override;
@ -85,7 +85,7 @@ end;
function TX2RollingLogFileObserver.CreateWorkerThread: TX2LogObserverWorkerThread; function TX2RollingLogFileObserver.CreateWorkerThread: TX2LogObserverWorkerThread;
begin begin
Result := TX2RollingLogFileWorkerThread.Create(FileName, Days); Result := TX2RollingLogFileWorkerThread.Create(OutputFileName, Days);
end; end;
@ -100,9 +100,9 @@ begin
end; end;
function TX2RollingLogFileWorkerThread.GetFileName: string; function TX2RollingLogFileWorkerThread.GetFileName(AEntry: TX2LogQueueEntry): string;
begin begin
Result := GetDateFileName(Date); Result := GetDateFileName(AEntry.DateTime);
end; end;
@ -111,7 +111,7 @@ var
baseFileName: string; baseFileName: string;
begin begin
baseFileName := inherited GetFileName; baseFileName := OutputFileName;
Result := ChangeFileExt(baseFileName, '') + '.' + Result := ChangeFileExt(baseFileName, '') + '.' +
FormatDateTime(FDateFormat, ADate, FFormatSettings) + FormatDateTime(FDateFormat, ADate, FFormatSettings) +
ExtractFileExt(baseFileName); ExtractFileExt(baseFileName);
@ -140,7 +140,7 @@ var
fileName: string; fileName: string;
begin begin
baseFileName := inherited GetFileName; baseFileName := OutputFileName;
fileMask := ChangeFileExt(ExtractFileName(baseFileName), '') + '.*' + fileMask := ChangeFileExt(ExtractFileName(baseFileName), '') + '.*' +
ExtractFileExt(baseFileName); ExtractFileExt(baseFileName);