-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHitLogger.pas
315 lines (203 loc) · 9.87 KB
/
HitLogger.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
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
unit
HitLogger;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
interface
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
uses
Classes,
IpUtils;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
type
THitLogger = class
public
class procedure SetProperties(const FileName: String; const FileWhat: String; FullDump: Boolean; MaxPendingHits: Integer);
public
class function IsEnabled: Boolean;
class function GetFileWhat: String;
class function GetFullDump: Boolean;
class procedure AddIPv4Hit(When: TDateTime; Client: TIPv4Address; const StatusCode: String; const DnsServerIndex: String; const DnsServerResponseTime: String; const Description: String);
class procedure AddIPv6Hit(When: TDateTime; Client: TIPv6Address; const StatusCode: String; const DnsServerIndex: String; const DnsServerResponseTime: String; const Description: String);
class procedure WriteAllPendingHitsToDisk(Async: Boolean);
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
type
THitLoggerAsyncWriter = class(TThread)
public
IsDone: Boolean;
private
FileName: String;
Contents: String;
public
constructor Create(const FileName: String; const Contents: String);
procedure Execute; override;
destructor Destroy; override;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
implementation
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
uses
SysUtils,
Windows,
Configuration,
EnvironmentVariables,
FileIO,
Tracer;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
var
THitLogger_Enabled: Boolean;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
var
THitLogger_FileName: String;
THitLogger_FileWhat: String;
THitLogger_FullDump: Boolean;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
var
THitLogger_DateTemplate: Boolean;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
var
THitLogger_BufferList: TStringList;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
var
THitLogger_MaxPendingHits: Integer;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
var
THitLogger_LastAsyncWriter: THitLoggerAsyncWriter;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
class procedure THitLogger.SetProperties(const FileName: String; const FileWhat: String; FullDump: Boolean; MaxPendingHits: Integer);
begin
if (FileName <> '') then begin
THitLogger_Enabled := True;
THitLogger_FileName := FileName;
THitLogger_DateTemplate := Pos('%DATE%', FileName) > 0;
if (Pos('%TEMP%', FileName) > 0) then THitLogger_FileName := StringReplace(THitLogger_FileName, '%TEMP%', TEnvironmentVariables.Get('TEMP', '%TEMP%'), [rfReplaceAll]);
if (Pos('%APPDATA%', FileName) > 0) then THitLogger_FileName := StringReplace(THitLogger_FileName, '%APPDATA%', TEnvironmentVariables.Get('APPDATA', '%APPDATA%'), [rfReplaceAll]);
if (Pos('%LOCALAPPDATA%', FileName) > 0) then THitLogger_FileName := StringReplace(THitLogger_FileName, '%LOCALAPPDATA%', TEnvironmentVariables.Get('LOCALAPPDATA', '%LOCALAPPDATA%'), [rfReplaceAll]);
THitLogger_FileWhat := FileWhat;
THitLogger_FullDump := FullDump;
THitLogger_MaxPendingHits := MaxPendingHits;
end;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
class function THitLogger.IsEnabled: Boolean;
begin
Result := THitLogger_Enabled;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
class function THitLogger.GetFileWhat: String;
begin
Result := THitLogger_FileWhat;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
class function THitLogger.GetFullDump: Boolean;
begin
Result := THitLogger_FullDump;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
class procedure THitLogger.AddIPv4Hit(When: TDateTime; Client: TIPv4Address; const StatusCode: String; const DnsServerIndex: String; const DnsServerResponseTime: String; const Description: String);
begin
if (THitLogger_BufferList = nil) then begin THitLogger_BufferList := TStringList.Create; THitLogger_BufferList.Capacity := THitLogger_MaxPendingHits; end; THitLogger_BufferList.Add(FormatDateTime('yyyy-mm-dd HH":"nn":"ss.zzz', When) + #9 + TIPv4AddressUtility.ToString(Client) + #9 + StatusCode + #9 + DnsServerIndex + #9 + DnsServerResponseTime + #9 + Description); if (THitLogger_BufferList.Count >= THitLogger_MaxPendingHits) then Self.WriteAllPendingHitsToDisk(True);
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
class procedure THitLogger.AddIPv6Hit(When: TDateTime; Client: TIPv6Address; const StatusCode: String; const DnsServerIndex: String; const DnsServerResponseTime: String; const Description: String);
begin
if (THitLogger_BufferList = nil) then begin THitLogger_BufferList := TStringList.Create; THitLogger_BufferList.Capacity := THitLogger_MaxPendingHits; end; THitLogger_BufferList.Add(FormatDateTime('yyyy-mm-dd HH":"nn":"ss.zzz', When) + #9 + TIPv6AddressUtility.ToString(Client) + #9 + StatusCode + #9 + DnsServerIndex + #9 + DnsServerResponseTime + #9 + Description); if (THitLogger_BufferList.Count >= THitLogger_MaxPendingHits) then Self.WriteAllPendingHitsToDisk(True);
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
class procedure THitLogger.WriteAllPendingHitsToDisk(Async: Boolean);
var
HitLoggerBufferListCount: Integer; FileName: String; Contents: String;
begin
if (THitLogger_BufferList <> nil) then begin
HitLoggerBufferListCount := THitLogger_BufferList.Count;
if (HitLoggerBufferListCount > 0) then begin
if (THitLogger_LastAsyncWriter <> nil) then begin
while not THitLogger_LastAsyncWriter.IsDone do Sleep(20); THitLogger_LastAsyncWriter.Free; THitLogger_LastAsyncWriter := nil;
end;
FileName := THitLogger_FileName; if THitLogger_DateTemplate then FileName := StringReplace(FileName, '%DATE%', FormatDateTime('yyyymmdd', Now), [rfReplaceAll]);
Contents := THitLogger_BufferList.Text;
try
if Async then begin
THitLogger_LastAsyncWriter := THitLoggerAsyncWriter.Create(FileName, Contents); if (THitLogger_LastAsyncWriter <> nil) then THitLogger_LastAsyncWriter.Resume else TFileIO.AppendAllText(FileName, Contents);
end else begin
TFileIO.AppendAllText(FileName, Contents);
end;
except
on E: Exception do if TTracer.IsEnabled then TTracer.Trace(TracePriorityError, 'THitLogger.WriteAllPendingHitsToDisk: The following error occurred during execution: ' + E.Message);
end;
THitLogger_BufferList.Clear;
end;
end;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
constructor THitLoggerAsyncWriter.Create(const FileName: String; const Contents: String);
begin
inherited Create(True); Self.FreeOnTerminate := False; Self.FileName := FileName; Self.Contents := Contents;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
procedure THitLoggerAsyncWriter.Execute;
begin
try
TFileIO.AppendAllText(Self.FileName, Self.Contents);
except
on E: Exception do if TTracer.IsEnabled then TTracer.Trace(TracePriorityError, 'THitLoggerAsyncWriter.Execute: The following error occurred during execution: ' + E.Message);
end;
Self.IsDone := True;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
destructor THitLoggerAsyncWriter.Destroy;
begin
inherited Destroy;
end;
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
begin
THitLogger_Enabled := False;
end.