2014-05-18 18:09:07 +00:00
|
|
|
unit X2Log.Observer.LogFile;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
System.Classes,
|
|
|
|
System.Generics.Collections,
|
|
|
|
System.SyncObjs,
|
|
|
|
|
|
|
|
X2Log.Intf,
|
|
|
|
X2Log.Observer.Custom,
|
2016-10-15 09:22:51 +00:00
|
|
|
X2Log.Observer.CustomThreaded,
|
|
|
|
X2Log.TextFormatter.Intf;
|
2014-05-18 18:09:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
TX2LogFileObserver = class(TX2LogCustomThreadedObserver)
|
|
|
|
private
|
2015-03-11 11:45:31 +00:00
|
|
|
FOutputFileName: string;
|
2015-06-18 15:39:23 +00:00
|
|
|
FLogDetails: Boolean;
|
2016-10-15 09:22:51 +00:00
|
|
|
FTextFormatter: IX2LogTextFormatter;
|
2014-05-18 18:09:07 +00:00
|
|
|
protected
|
2016-10-15 09:22:51 +00:00
|
|
|
function GetTextFormatter: IX2LogTextFormatter; virtual;
|
2014-05-18 18:09:07 +00:00
|
|
|
function CreateWorkerThread: TX2LogObserverWorkerThread; override;
|
2015-03-06 11:51:59 +00:00
|
|
|
|
2015-03-11 11:45:31 +00:00
|
|
|
property OutputFileName: string read FOutputFileName;
|
2015-06-18 15:39:23 +00:00
|
|
|
property LogDetails: Boolean read FLogDetails;
|
2014-05-18 18:09:07 +00:00
|
|
|
public
|
2016-10-15 09:22:51 +00:00
|
|
|
constructor Create(const AOutputFileName: string; ALogLevels: TX2LogLevels = X2LogLevelsDefault; ALogDetails: Boolean = True; ATextFormatter: IX2LogTextFormatter = nil);
|
|
|
|
constructor CreateInProgramData(const AOutputFileName: string; ALogLevels: TX2LogLevels = X2LogLevelsDefault; ALogDetails: Boolean = True; ATextFormatter: IX2LogTextFormatter = nil);
|
|
|
|
constructor CreateInUserAppData(const AOutputFileName: string; ALogLevels: TX2LogLevels = X2LogLevelsDefault; ALogDetails: Boolean = True; ATextFormatter: IX2LogTextFormatter = nil);
|
2014-05-18 18:09:07 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
TX2LogFileWorkerThread = class(TX2LogObserverWorkerThread)
|
|
|
|
private
|
2015-03-11 11:45:31 +00:00
|
|
|
FOutputFileName: string;
|
2015-06-18 15:39:23 +00:00
|
|
|
FLogDetails: Boolean;
|
2016-10-15 09:22:51 +00:00
|
|
|
FTextFormatter: IX2LogTextFormatter;
|
2014-05-18 18:09:07 +00:00
|
|
|
protected
|
2015-03-11 11:45:31 +00:00
|
|
|
function GetFileName(AEntry: TX2LogQueueEntry): string; virtual;
|
2014-05-18 18:09:07 +00:00
|
|
|
procedure ProcessEntry(AEntry: TX2LogQueueEntry); override;
|
|
|
|
|
2015-03-11 11:45:31 +00:00
|
|
|
property OutputFileName: string read FOutputFileName;
|
2015-06-18 15:39:23 +00:00
|
|
|
property LogDetails: Boolean read FLogDetails;
|
2016-10-15 09:22:51 +00:00
|
|
|
property TextFormatter: IX2LogTextFormatter read FTextFormatter;
|
2014-05-18 18:09:07 +00:00
|
|
|
public
|
2016-10-15 09:22:51 +00:00
|
|
|
constructor Create(const AOutputFileName: string; ATextFormatter: IX2LogTextFormatter; ALogDetails: Boolean = True);
|
2014-05-18 18:09:07 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2015-03-06 11:51:59 +00:00
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
System.IOUtils,
|
|
|
|
System.SysUtils,
|
|
|
|
System.Win.ComObj,
|
|
|
|
Winapi.SHFolder,
|
|
|
|
Winapi.Windows,
|
|
|
|
|
2016-10-15 09:22:51 +00:00
|
|
|
X2Log.Constants,
|
|
|
|
X2Log.TextFormatter.Default;
|
2015-03-06 11:51:59 +00:00
|
|
|
|
|
|
|
|
2016-10-15 09:22:51 +00:00
|
|
|
type
|
|
|
|
TX2LogFileTextFormatterHelper = class(TInterfacedObject, IX2LogTextFormatterHelper)
|
|
|
|
private
|
|
|
|
FEntry: TX2LogQueueEntry;
|
|
|
|
FLogFileName: string;
|
|
|
|
FLogDetails: Boolean;
|
|
|
|
protected
|
|
|
|
{ IX2LogTextFormatterHelper }
|
|
|
|
function GetDetailsFilename: string;
|
|
|
|
|
|
|
|
property Entry: TX2LogQueueEntry read FEntry;
|
|
|
|
property LogFileName: string read FLogFileName;
|
|
|
|
property LogDetails: Boolean read FLogDetails;
|
|
|
|
public
|
|
|
|
constructor Create(AEntry: TX2LogQueueEntry; const ALogFileName: string; ALogDetails: Boolean);
|
|
|
|
end;
|
|
|
|
|
2014-05-18 18:09:07 +00:00
|
|
|
|
|
|
|
{ TX2LogFileObserver }
|
2016-10-15 09:22:51 +00:00
|
|
|
constructor TX2LogFileObserver.Create(const AOutputFileName: string; ALogLevels: TX2LogLevels; ALogDetails: Boolean; ATextFormatter: IX2LogTextFormatter);
|
2014-05-18 18:09:07 +00:00
|
|
|
begin
|
2015-03-11 11:45:31 +00:00
|
|
|
FOutputFileName := AOutputFileName;
|
2015-06-19 08:18:17 +00:00
|
|
|
FLogDetails := ALogDetails;
|
2016-10-15 09:22:51 +00:00
|
|
|
FTextFormatter := ATextFormatter;
|
|
|
|
if not Assigned(FTextFormatter) then
|
|
|
|
FTextFormatter := TX2LogDefaultTextFormatter.Create;
|
2014-05-18 18:09:07 +00:00
|
|
|
|
|
|
|
inherited Create(ALogLevels);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2016-10-15 09:22:51 +00:00
|
|
|
constructor TX2LogFileObserver.CreateInProgramData(const AOutputFileName: string; ALogLevels: TX2LogLevels; ALogDetails: Boolean; ATextFormatter: IX2LogTextFormatter);
|
2014-05-18 18:09:07 +00:00
|
|
|
var
|
|
|
|
path: PWideChar;
|
|
|
|
|
|
|
|
begin
|
|
|
|
GetMem(path, MAX_PATH);
|
|
|
|
try
|
|
|
|
OleCheck(SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, SHGFP_TYPE_CURRENT, path));
|
2016-10-15 09:22:51 +00:00
|
|
|
Create(IncludeTrailingPathDelimiter(path) + AOutputFileName, ALogLevels, ALogDetails, ATextFormatter);
|
2014-05-18 18:09:07 +00:00
|
|
|
finally
|
|
|
|
FreeMem(path);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2016-10-15 09:22:51 +00:00
|
|
|
constructor TX2LogFileObserver.CreateInUserAppData(const AOutputFileName: string; ALogLevels: TX2LogLevels; ALogDetails: Boolean; ATextFormatter: IX2LogTextFormatter);
|
2014-05-18 18:09:07 +00:00
|
|
|
var
|
|
|
|
path: PWideChar;
|
|
|
|
|
|
|
|
begin
|
|
|
|
GetMem(path, MAX_PATH);
|
|
|
|
try
|
|
|
|
OleCheck(SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, path));
|
2016-10-15 09:22:51 +00:00
|
|
|
Create(IncludeTrailingPathDelimiter(path) + AOutputFileName, ALogLevels, ALogDetails, ATextFormatter);
|
2014-05-18 18:09:07 +00:00
|
|
|
finally
|
|
|
|
FreeMem(path);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2LogFileObserver.CreateWorkerThread: TX2LogObserverWorkerThread;
|
|
|
|
begin
|
2016-10-15 09:22:51 +00:00
|
|
|
Result := TX2LogFileWorkerThread.Create(OutputFileName, GetTextFormatter, LogDetails);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2LogFileObserver.GetTextFormatter: IX2LogTextFormatter;
|
|
|
|
begin
|
|
|
|
Result := FTextFormatter;
|
2014-05-18 18:09:07 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{ TX2LogFileWorkerThread }
|
2016-10-15 09:22:51 +00:00
|
|
|
constructor TX2LogFileWorkerThread.Create(const AOutputFileName: string; ATextFormatter: IX2LogTextFormatter; ALogDetails: Boolean);
|
2014-05-18 18:09:07 +00:00
|
|
|
begin
|
2015-03-11 11:45:31 +00:00
|
|
|
FOutputFileName := AOutputFileName;
|
2015-06-18 15:39:23 +00:00
|
|
|
FLogDetails := ALogDetails;
|
2016-10-15 09:22:51 +00:00
|
|
|
FTextFormatter := ATextFormatter;
|
2014-05-18 18:09:07 +00:00
|
|
|
|
|
|
|
inherited Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2LogFileWorkerThread.ProcessEntry(AEntry: TX2LogQueueEntry);
|
|
|
|
var
|
2015-03-11 11:45:31 +00:00
|
|
|
fileName: string;
|
2016-10-15 09:22:51 +00:00
|
|
|
line: string;
|
|
|
|
writer: TStreamWriter;
|
|
|
|
|
|
|
|
begin
|
|
|
|
fileName := GetFileName(AEntry);
|
|
|
|
ForceDirectories(ExtractFilePath(fileName));
|
|
|
|
|
|
|
|
line := TextFormatter.GetText(TX2LogFileTextFormatterHelper.Create(AEntry, fileName, LogDetails),
|
|
|
|
AEntry.Level, AEntry.DateTime, AEntry.Message, AEntry.Category, AEntry.Details);
|
|
|
|
|
|
|
|
{ Append line to log file }
|
|
|
|
writer := TFile.AppendText(fileName);
|
|
|
|
try
|
|
|
|
writer.WriteLine(line);
|
|
|
|
finally
|
|
|
|
FreeAndNil(writer);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2LogFileWorkerThread.GetFileName(AEntry: TX2LogQueueEntry): string;
|
|
|
|
begin
|
|
|
|
Result := FOutputFileName;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{ TX2LogFileTextFormatterHelper }
|
|
|
|
constructor TX2LogFileTextFormatterHelper.Create(AEntry: TX2LogQueueEntry; const ALogFileName: string; ALogDetails: Boolean);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
|
|
|
|
FEntry := AEntry;
|
|
|
|
FLogFileName := ALogFileName;
|
|
|
|
FLogDetails := ALogDetails;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2LogFileTextFormatterHelper.GetDetailsFilename: string;
|
|
|
|
var
|
|
|
|
logDetailsStreamable: IX2LogDetailsStreamable;
|
2014-05-18 18:09:07 +00:00
|
|
|
baseReportFileName: string;
|
|
|
|
detailsExtension: string;
|
|
|
|
detailsFile: THandle;
|
|
|
|
detailsFileStream: THandleStream;
|
|
|
|
detailsFileName: string;
|
|
|
|
detailsNumber: Integer;
|
|
|
|
|
|
|
|
begin
|
2016-10-15 09:22:51 +00:00
|
|
|
Result := '';
|
|
|
|
if not LogDetails then
|
|
|
|
exit;
|
2014-10-22 09:38:51 +00:00
|
|
|
|
2016-10-15 09:22:51 +00:00
|
|
|
if Supports(Entry.Details, IX2LogDetailsStreamable, logDetailsStreamable) then
|
2014-05-18 18:09:07 +00:00
|
|
|
begin
|
2016-10-15 09:22:51 +00:00
|
|
|
detailsExtension := ExtractFileExt(LogFileName);
|
|
|
|
baseReportFileName := ChangeFileExt(LogFileName, '_' + FormatDateTime(GetLogResourceString(@LogFileNameDateFormat), Entry.DateTime));
|
2014-05-18 18:09:07 +00:00
|
|
|
detailsFileName := baseReportFileName + detailsExtension;
|
|
|
|
detailsNumber := 0;
|
|
|
|
|
|
|
|
if ForceDirectories(ExtractFilePath(detailsFileName)) then
|
|
|
|
begin
|
|
|
|
repeat
|
|
|
|
{ TFileStream lacks the ability to create a file only when it does not exist }
|
|
|
|
detailsFile := CreateFile(PChar(detailsFileName), GENERIC_READ or GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, CREATE_NEW,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
|
|
|
|
if detailsFile = INVALID_HANDLE_VALUE then
|
|
|
|
begin
|
|
|
|
if GetLastError = ERROR_FILE_EXISTS then
|
|
|
|
begin
|
|
|
|
{ Generate a new file name }
|
|
|
|
Inc(detailsNumber);
|
|
|
|
detailsFileName := Format('%s_%d%s', [baseReportFileName, detailsNumber,
|
|
|
|
detailsExtension]);
|
|
|
|
end else
|
|
|
|
break;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
{ Details file succesfully generated }
|
|
|
|
try
|
|
|
|
detailsFileStream := THandleStream.Create(detailsFile);
|
|
|
|
try
|
2014-05-30 12:51:01 +00:00
|
|
|
logDetailsStreamable.SaveToStream(detailsFileStream);
|
2014-05-18 18:09:07 +00:00
|
|
|
finally
|
|
|
|
FreeAndNil(detailsFileStream);
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
CloseHandle(detailsFile);
|
|
|
|
end;
|
|
|
|
|
2016-10-15 09:22:51 +00:00
|
|
|
Result := detailsFileName;
|
2014-05-18 18:09:07 +00:00
|
|
|
break;
|
|
|
|
end;
|
|
|
|
until False;
|
|
|
|
end;
|
|
|
|
end;
|
2015-03-06 11:51:59 +00:00
|
|
|
end;
|
|
|
|
|
2014-05-18 18:09:07 +00:00
|
|
|
end.
|