forked from Al-Muhandis/fp-telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgstatlog.pas
252 lines (210 loc) · 5.57 KB
/
tgstatlog.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
unit tgstatlog;
{$mode objfpc}{$H+}
interface
uses SysUtils,Classes;
Type
{ TtgStatLog }
TtgStatLog = Class(TComponent)
Private
fAppendContent : Boolean;
FDirectory: String;
FFieldDelimiter: String;
FFilePostfix: String;
FFilePrefix: String;
FStream : TFileStream;
FActive: Boolean;
FDefaultEventType: TEventType;
FTimeStampFormat: String;
FPaused : Boolean;
function GetFileName: String;
procedure SetActive(const Value: Boolean);
procedure ActivateLog;
procedure DeActivateLog;
procedure ActivateFileLog;
procedure SetDirectory(AValue: String);
procedure SetFieldDelimiter(AValue: String);
procedure SetFilePostfix(AValue: String);
procedure SetFilePrefix(AValue: String);
procedure WriteFileLog(const Msg: String);
procedure DeActivateFileLog;
Protected
Procedure CheckInactive;
function CheckForNewLogFile: Boolean;
Procedure EnsureActive;
procedure CloseAndCreateNewLog;
Public
constructor Create(AOwner: TComponent); override;
Destructor Destroy; override;
function GetFileNameFromDate(ADate: TDate): String;
Procedure Pause;
Procedure Resume;
Procedure Log (const Msg : String);
procedure Log(const Fmt: String; Args: array of const);
procedure Log(AFields: array of String);
Published
Property AppendContent : Boolean Read fAppendContent Write fAppendContent;
Property Active : Boolean Read FActive write SetActive;
Property DefaultEventType : TEventType Read FDEfaultEventType Write FDefaultEventType;
property Directory: String read FDirectory write SetDirectory;
property FieldDelimiter: String read FFieldDelimiter write SetFieldDelimiter;
Property FileName : String Read GetFileName;
property FilePrefix: String read FFilePrefix write SetFilePrefix;
property FilePostfix: String read FFilePostfix write SetFilePostfix;
Property TimeStampFormat : String Read FTimeStampFormat Write FTimeStampFormat;
Property Paused : Boolean Read FPaused Write FPaused;
End;
ELogError = Class(Exception);
implementation
{ TtgStatLog }
Resourcestring
SErrOperationNotAllowed = 'Operation not allowed when eventlog is active.';
procedure TtgStatLog.CheckInactive;
begin
If Active then
Raise ELogError.Create(SErrOperationNotAllowed);
end;
function TtgStatLog.CheckForNewLogFile: Boolean;
begin
Result:= not SameText(FStream.FileName, GetFileName);
end;
procedure TtgStatLog.EnsureActive;
begin
If Not Active then
Active:=True;
end;
procedure TtgStatLog.CloseAndCreateNewLog;
begin
DeActivateLog;
ActivateLog;
end;
constructor TtgStatLog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FPaused:=True;
FDirectory:='';
FFilePostfix:='.csv';
FFilePrefix:='~log';
FFieldDelimiter:='; ';
FTimeStampFormat:='';
AppendContent:=True;
end;
procedure TtgStatLog.Pause;
begin
Paused:=True;
end;
procedure TtgStatLog.Resume;
begin
Paused:=False;
end;
procedure TtgStatLog.Log(const Msg: String);
begin
If Paused then
exit;
EnsureActive;
WriteFileLog(Msg);
end;
procedure TtgStatLog.WriteFileLog(const Msg : String);
Var
S,TS: String;
begin
if CheckForNewLogFile then
CloseAndCreateNewLog;
If FTimeStampFormat='' then
FTimeStampFormat:='yyyy-mm-dd hh:nn:ss';
TS:=FormatDateTime(FTimeStampFormat,Now);
S:=TS+FieldDelimiter+Msg+LineEnding;
try
FStream.WriteBuffer(S[1],Length(S));
S:='';
except
On E : Exception do
S:=E.Message;
end;
end;
procedure TtgStatLog.Log(const Fmt: String; Args: array of const);
begin
Log(Format(Fmt,Args));
end;
procedure TtgStatLog.Log(AFields: array of String);
var
i: Integer;
S: String;
begin
S:='';
for i:=0 to Length(AFields)-1 do
S+=AFields[i]+FieldDelimiter;
Log(S);
end;
procedure TtgStatLog.SetActive(const Value: Boolean);
begin
If Value<>FActive then
begin
If Value then
ActivateLog
else
DeActivateLog;
FActive:=Value;
end;
end;
function TtgStatLog.GetFileName: String;
begin
Result:=GetFileNameFromDate(Date);
end;
procedure TtgStatLog.ActivateLog;
begin
ActivateFileLog;
end;
procedure TtgStatLog.DeActivateLog;
begin
DeActivateFileLog;
end;
procedure TtgStatLog.ActivateFileLog;
var
fFileFlags : Word;
AFileName: String;
begin
AFileName:=GetFileName;
if fAppendContent and FileExists(AFileName) then
fFileFlags := fmOpenWrite
else
fFileFlags := fmCreate;
fFileFlags := fFileFlags or fmShareDenyWrite;
FStream:=TFileStream.Create(AFileName,fFileFlags);
if fAppendContent then
FStream.Seek(0,soFromEnd);
end;
procedure TtgStatLog.SetDirectory(AValue: String);
begin
if FDirectory=AValue then Exit;
FDirectory:=AValue;
end;
procedure TtgStatLog.SetFieldDelimiter(AValue: String);
begin
if FFieldDelimiter=AValue then Exit;
FFieldDelimiter:=AValue;
end;
procedure TtgStatLog.SetFilePostfix(AValue: String);
begin
if FFilePostfix=AValue then Exit;
FFilePostfix:=AValue;
end;
procedure TtgStatLog.DeActivateFileLog;
begin
FStream.Free;
FStream:=Nil;
end;
function TtgStatLog.GetFileNameFromDate(ADate: TDate): String;
begin
Result:=Directory+FilePrefix+FormatDateTime('yyyymmdd', ADate)+FilePostfix;
end;
procedure TtgStatLog.SetFilePrefix(AValue: String);
begin
if FFilePrefix=AValue then Exit;
FFilePrefix:=AValue;
end;
destructor TtgStatLog.Destroy;
begin
Active:=False;
inherited;
end;
end.