-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogger.OutputDebugString.pas
More file actions
150 lines (120 loc) · 3.73 KB
/
Copy pathLogger.OutputDebugString.pas
File metadata and controls
150 lines (120 loc) · 3.73 KB
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
{*******************************************************}
{ }
{ Delphi.Log }
{ https://github.com/androschuk/D.Logger }
{ }
{ This software is open source, }
{ licensed under the The MIT License (MIT). }
{ }
{*******************************************************}
unit Logger.OutputDebugString;
interface
uses
System.Classes, Logger.Intf, System.SysUtils, System.SyncObjs, Logger.Storage.Core;
type
IOutputDebugStringStorageSettings = interface
['{67EF620B-D5EC-4B20-8B01-04C410655F29}']
function GetDateTimeFormat: WideString; safecall;
procedure SetDateTimeFormat(ADateTimeFormat: WideString); safecall;
procedure SetDefaults; safecall;
procedure Load; safecall;
property DateTimeFormat: WideString read GetDateTimeFormat write SetDateTimeFormat;
end;
TOutputDebugStringStorageSettings = class(TInterfacedObject, IOutputDebugStringStorageSettings)
private
FConfigPath: string;
FDateTimeFormat: WideString;
protected
{IConfig}
procedure Load; safecall;
procedure SetDefaults; safecall;
function GetDateTimeFormat: WideString; safecall;
procedure SetDateTimeFormat(ADateTimeFormat: WideString); safecall;
public
constructor Create(AConfigPath: string); reintroduce;
end;
TOutputDebugStringStorage = class(TStorage)
private
FConfig: IOutputDebugStringStorageSettings;
protected
{IStorage}
procedure Write(Args: ILogArgument); override; safecall;
function Equal(AStorage: IStorage): Boolean; override; safecall;
public
constructor Create; reintroduce;
destructor Destroy; override;
end;
implementation
uses
Logger.Utils, IniFiles, Winapi.Windows;
function GetLogFileName: string;
begin
Result := ChangeFileExt(GetAppName, '.log');
end;
function GetConfigFileName: string;
begin
Result := 'Logger.Config.ini';
end;
function GetLogFilePath: string;
begin
Result := GetAppPath + GetLogFileName;
end;
function GetConfigFilePath: string;
begin
Result := GetAppPath + GetConfigFileName;
end;
{ TOutputDebugStringStorage }
constructor TOutputDebugStringStorage.Create;
begin
FConfig := TOutputDebugStringStorageSettings.Create(GetConfigFilePath);
FConfig.Load;
end;
destructor TOutputDebugStringStorage.Destroy;
begin
inherited;
end;
function TOutputDebugStringStorage.Equal(AStorage: IStorage): Boolean;
begin
Result := SameText(Self.ClassName, AStorage.StorageClassName);
end;
procedure TOutputDebugStringStorage.Write(Args: ILogArgument);
var
LogMessage: string;
begin
LogMessage := Format('%s|%s|%s|%s',[
FormatDateTime(FConfig.DateTimeFormat, Args.TimeStamp),
Args.SourceName,
Args.LogLevel.ToString,
Args.LogMessage]);
OutputDebugString(PChar(LogMessage));
end;
{ TOutputDebugStringStorageSettings }
constructor TOutputDebugStringStorageSettings.Create(AConfigPath: string);
begin
FConfigPath := AConfigPath;
SetDefaults;
end;
function TOutputDebugStringStorageSettings.GetDateTimeFormat: WideString;
begin
Result := FDateTimeFormat;
end;
procedure TOutputDebugStringStorageSettings.SetDateTimeFormat(ADateTimeFormat: WideString);
begin
FDateTimeFormat := ADateTimeFormat;
end;
procedure TOutputDebugStringStorageSettings.SetDefaults;
begin
FDateTimeFormat := 'YYYY.MM.DD HH:NN:SS:ZZZ';
end;
procedure TOutputDebugStringStorageSettings.Load;
var
Config : TMemIniFile;
begin
Config := TMemIniFile.Create(FConfigPath);
try
FDateTimeFormat := Config.ReadString(Self.ClassName, 'DateTimeFormat', FDateTimeFormat);
finally
FreeAndNil(Config);
end;
end;
end.