-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.pas
More file actions
320 lines (265 loc) · 9.73 KB
/
logging.pas
File metadata and controls
320 lines (265 loc) · 9.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
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
316
317
318
319
320
unit Logging;
{$mode objfpc}{$H+}
interface
{$M+}
{$MODESWITCH TYPEHELPERS}
uses Classes, Sysutils, FileUtil, Forms;
type
{ TLog_File }
TLog_File = class(TObject)
private
strlist : TStringList;
FileStream1 : TFileStream;
FLogFolder, FUserName : String;
FAppendCurrentLogfile, FActivateLogging : Boolean;
szCurrentTime : String;
function GetAppendCurrentLogfile: Boolean;
function GetLogFolder: String;
function GetCurUserName: String;
procedure SetAppendCurrentLogfile(AValue: Boolean);
procedure SetLogFolder(AValue: String);
procedure SetCurUserName(AValue: String);
function CurrentDate: String; //Bepalen huidige datum
procedure Logging; //De gegevens worden in het bestand gezet
procedure CurrentTime;
procedure WriteToLog(Commentaar : String); //Tekst naar logbestand schrijven
procedure WriteToLogAndFlush(Commentaar : String); //Tekst direct naar logbestand schrijven
property LogFolder : String Read GetLogFolder Write SetLogFolder;
property Username : String Read GetCurUserName Write SetCurUserName;
public
constructor Create();
destructor Destroy; override;
procedure StartLogging; //Aanmaken/openen log bestand
procedure StopLogging; //Bestand opslaan en sluiten
procedure WriteToLogInfo(Commentaar : String); //Tekst naar logbestand schrijven
procedure WriteToLogWarning(Commentaar : String); //Tekst naar logbestand schrijven
procedure WriteToLogError(Commentaar : String); //Tekst naar logbestand schrijven
procedure WriteToLogDebug(Commentaar : String); //Tekst naar logbestand schrijven
procedure WriteToLogAndFlushInfo(Commentaar : String); //Tekst direct naar logbestand schrijven
procedure WriteToLogAndFlushWarning(Commentaar : String); //Tekst direct naar logbestand schrijven
procedure WriteToLogAndFlushError(Commentaar : String); //Tekst direct naar logbestand schrijven
procedure WriteToLogAndFlushDebug(Commentaar : String); //Tekst direct naar logbestand schrijven
property AppendLogFile : Boolean Read GetAppendCurrentLogfile Write SetAppendCurrentLogfile;
property ActivateLogging : Boolean Read FActivateLogging Write FActivateLogging;
end;
implementation
uses lazfileutils,
Settings;
{ TLog_File.TLogTypeHelper }
{ TLog_File }
//Public
{%region% properties}
function TLog_File.GetLogFolder: String;
begin
Result := FLogFolder;
end;
procedure TLog_File.SetLogFolder(AValue: String);
begin
FLogFolder := AValue;
end;
function TLog_File.GetCurUserName: String;
begin
Result := FUserName;
end;
procedure TLog_File.SetCurUserName(AValue: String);
begin
FUserName := AValue;
end;
function TLog_File.GetAppendCurrentLogfile: Boolean;
begin
Result := FAppendCurrentLogfile;
end;
procedure TLog_File.SetAppendCurrentLogfile(AValue: Boolean);
begin
FAppendCurrentLogfile := AValue;
end;
{%endregion% properties}
constructor TLog_File.Create();
begin
LogFolder := AppendPathDelim(ExtractFilePath(Application.ExeName) + Settings.LoggingFolder);
strlist := TStringList.Create;
UserName := StringReplace(GetEnvironmentVariable('USERNAME'), ' ', '_', [rfIgnoreCase, rfReplaceAll]);
{ if LogFolder = '' then
begin
ActivateLogging := False;
AppendLogFile := False;
end;}
end;
destructor TLog_File.Destroy;
begin
FileStream1.Free;
strlist.Free;
inherited;
end;
procedure TLog_File.CurrentTime; //Bepaal het huidige tijdstip
begin
szCurrentTime := FormatDateTime('hh:mm:ss', Now) + ' --> | ';
end;
function TLog_File.CurrentDate: String;
var
Present : TDateTime;
Year, Month, Day : Word;
begin
Present := Now; //de huidige datum en tijd opvragen
DecodeDate(Present, Year, Month, Day); //de datum bepalen die met present is opgehaald
Result := IntToStr(Day) + '-' + IntToStr(Month) + '-' + IntToStr(Year);
end;
procedure TLog_File.Logging;
var
retry : Boolean;
retries, i : Integer;
MyString : String;
const
MAXRETRIES = 10;
RETRYBACKOFFDELAYMS = 50;
begin
if ActivateLogging then begin
try
// Retry mechanisme voor een SaveToFile()
retry := True;
retries := 0;
while retry do
try
//wegschrijven
for I := 0 to strlist.Count-1 do
begin
try
FileStream1.seek(0,soFromEnd); ////cursor aan het eind van het bestand zetten
MyString := strlist[i] + sLineBreak;
FileStream1.WriteBuffer(MyString[1], Length(MyString) * SizeOf(Char));
finally
//
end;
end;
strlist.Clear; //Stringlist leegmaken
retry := False;
except
on EInOutError do
begin
Inc(retries);
Sleep(RETRYBACKOFFDELAYMS * retries);
if retries > MAXRETRIES then
begin
WriteToLog('INFORMATIE | Na 10 pogingen is het opslaan in het logbestand afgebroken.');
Exit;
end;
end;
end;
finally
//
end;
end;
end;
procedure TLog_File.StartLogging;
begin
if ActivateLogging then begin
if AppendLogFile = True then
begin
if FileExists(LogFolder
+ Username
+ '_'
+ Settings.LogFileName) then
begin
FileStream1 := TFileStream.Create(LogFolder
+ Username
+ '_'
+ Settings.LogFileName,
fmOpenReadWrite or fmShareDenyNone); //fmShareDenyNone : Do not lock file
end
else //wel append maar het bestand bestaat nog niet dan aanmaken
begin
FileStream1 := TFileStream.Create(LogFolder
+ Username
+ '_'
+ Settings.LogFileName,
fmCreate or fmShareDenyNone);
end;
end
else //nieuw bestand aanmaken
begin
FileStream1 := TFileStream.Create(LogFolder
+ Username
+ '_'
+ Settings.LogFileName,
fmCreate or fmShareDenyNone);
end;
try
strlist.Add('##################################################################################################');
strlist.Add(' Programma: ' + Settings.ApplicationName);
strlist.Add(' Versie : ' + Settings.Version);
strlist.Add(' Datum : ' + CurrentDate);
strlist.Add('##################################################################################################');
Logging; //Direct opslaan
CurrentTime;
except
strlist.Add('FOUT | Onverwachte fout opgetreden bij de opstart van de logging procedure.');
end;
end;
end;
procedure TLog_File.StopLogging;
begin
strlist.Add('');
strlist.Add('##################################################################################################');
strlist.Add(' Programma ' + Settings.ApplicationName + ' is afgesloten');
strlist.Add('##################################################################################################');
strlist.Add('');
strlist.Add('');
strlist.Add('');
Logging; //Direct opslaan
end;
procedure TLog_File.WriteToLogInfo(Commentaar: String);
begin
CurrentTime;
strlist.Add(szCurrentTime + ' : INFORMATIE | ' + Commentaar);
end;
procedure TLog_File.WriteToLogWarning(Commentaar: String);
begin
CurrentTime;
strlist.Add(szCurrentTime + ' : WAARSCHUWING | ' + Commentaar);
end;
procedure TLog_File.WriteToLogError(Commentaar: String);
begin
CurrentTime;
strlist.Add(szCurrentTime + ' : FOUT | ' + Commentaar);
end;
procedure TLog_File.WriteToLogDebug(Commentaar: String);
begin
CurrentTime;
strlist.Add(szCurrentTime + ' : DEBUG | ' + Commentaar);
end;
procedure TLog_File.WriteToLog(Commentaar : String);
begin
CurrentTime;
strlist.Add(szCurrentTime + ' : | ' + Commentaar); //In de stringgrid gereed zetten
end;
procedure TLog_File.WriteToLogAndFlushInfo(Commentaar: String);
begin
CurrentTime;
strlist.Add(szCurrentTime + ' : INFORMATIE | ' + Commentaar); //In de stringgrid gereed zetten
Logging;
end;
procedure TLog_File.WriteToLogAndFlushWarning(Commentaar: String);
begin
CurrentTime;
strlist.Add(szCurrentTime + ' : WAARSCHWING | ' + Commentaar); //In de stringgrid gereed zetten
Logging;
end;
procedure TLog_File.WriteToLogAndFlushError(Commentaar: String);
begin
CurrentTime;
strlist.Add(szCurrentTime + ' : ERROR | ' + Commentaar); //In de stringgrid gereed zetten
Logging;
end;
procedure TLog_File.WriteToLogAndFlushDebug(Commentaar: String);
begin
CurrentTime;
strlist.Add(szCurrentTime + ' : DEBUG | ' + Commentaar); //In de stringgrid gereed zetten
Logging;
end;
procedure TLog_File.WriteToLogAndFlush(Commentaar: String);
begin
CurrentTime;
strlist.Add(szCurrentTime + ' : | ' + Commentaar); //In de stringgrid gereed zetten
Logging; // Direct opslaan
end;
end.