-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFLog.pas
311 lines (271 loc) · 8.71 KB
/
GFLog.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
(* ************************************************************
KEYNOTE: MOZILLA PUBLIC LICENSE STATEMENT.
-----------------------------------------------------------
The contents of this file are subject to the Mozilla Public
License Version 1.1 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.
The Original Code is KeyNote 1.0.
The Initial Developer of the Original Code is Marek Jedlinski
<eristic@lodz.pdi.net> (Poland).
Portions created by Marek Jedlinski are
Copyright (C) 2000, 2001. All Rights Reserved.
-----------------------------------------------------------
Contributor(s):
-----------------------------------------------------------
History:
-----------------------------------------------------------
Released: 30 June 2001
-----------------------------------------------------------
URLs:
- for OpenSource development:
http://keynote.sourceforge.net
- original author's software site:
http://www.lodz.pdi.net/~eristic/free/index.html
http://go.to/generalfrenetics
Email addresses (at least one should be valid)
<eristic@lodz.pdi.net>
<cicho@polbox.com>
<cicho@tenbit.pl>
************************************************************ *)
unit GFLog;
{$I gf_base.inc}
interface
uses
Windows, ShellAPI, Messages,
Dialogs, SysUtils, Classes,
StdCtrls, gf_misc;
type
TGFLogOnAdding = procedure( Sender : TObject; var ALine : string; var OKToAdd : boolean ) of object;
TGFLogOnAdded = procedure( Sender : TObject; const ALine : string ) of object;
type
TGFLog = class( TComponent )
private
fActive : boolean;
fMaxLines : integer;
fLines : TStringList;
fFileName : string;
fDateStamp : boolean;
fTimeStamp : boolean;
fUniqueFileName : boolean;
fAppendToFile : boolean;
fModified : boolean;
fDisplayControl : TCustomMemo; // TMemo, TRichEdit are descended from this
fOnAdding : TGFLogOnAdding;
fOnAdded : TGFLogOnAdded;
fIDString : string;
fLogWasSaved : boolean;
fLastError : string;
fShowErrors : boolean;
fDeactivateOnError : boolean;
fSeparator : string;
procedure SetMaxLines( const AMaxLines : integer );
procedure SetDisplayControl( const AControl : TCustomMemo );
function AddLine( AStr : string ) : integer;
function GetLastError : string;
public
constructor Create( AOwner : TComponent ); override;
destructor Destroy; override;
function Add( AStr : string ) : integer;
function AddEx( Sender : TObject; AStr : string ) : integer;
procedure Flush( const AndSave : boolean );
procedure Save( const aFileName : string );
// function SenderAdd( Sender : TObject; AStr : string ) : integer;
published
// properties
property Active : boolean read fActive write fActive;
property MaxLines : integer read fMaxLines write SetMaxLines;
property Lines : TStringList read fLines;
property FileName : string read fFileName write fFileName;
property DateStamp : boolean read fDateStamp write fDateStamp;
property TimeStamp : boolean read fTimeStamp write fTimeStamp;
property UniqueFileName : boolean read fUniqueFileName write fUniqueFileName;
property AppendToFile : boolean read fAppendToFile write fAppendToFile;
property Modified : boolean read fModified;
property DisplayControl : TCustomMemo read fDisplayControl write SetDisplayControl;
property IDString : String read fIDString write fIDString;
property LastError : string read GetLastError;
property ShowErrors : boolean read fShowErrors write fShowErrors;
property DeactivateOnError : boolean read fDeactivateOnError write fDeactivateOnError;
property Separator : string read fSeparator write fSeparator;
// events
property OnAdding : TGFLogOnAdding read fOnAdding write fOnAdding;
property OnAdded : TGFLogOnAdded read fOnAdded write fOnAdded;
end;
procedure Register;
implementation
const
_MAXLOGLINES = 1024;
constructor TGFLog.Create( AOwner : TComponent );
begin
inherited Create( AOwner );
fMaxLines := _MAXLOGLINES;
fLines := TStringList.Create;
with fLines do
begin
Sorted := false;
Duplicates := dupAccept;
Capacity := succ( fMaxLines );
end;
fLines.Add( 'LOG SESSION BEGINS ' + datetimetostr( now ) + ' (' + Name + ')' );
fActive := true;
fFileName := '';
fDateStamp := true;
fTimeStamp := true;
fUniqueFileName := false;
fAppendToFile := true;
fModified := false;
fDisplayControl := nil;
fOnAdding := nil;
fOnAdded := nil;
fIDString := '';
fLogWasSaved := false;
fLastError := '';
fShowErrors := true;
fDeactivateOnError := false;
fSeparator := '----- LOG SESSION ENDS -----';
end; // CREATE
destructor TGFLog.Destroy;
begin
// [x] save if necessary
Flush( true );
if assigned( fLines ) then fLines.Free;
inherited Destroy;
end; // DESTROY
procedure TGFLog.SetMaxLines( const AMaxLines : integer );
begin
if ( fMaxLines = AMaxLines ) then exit;
fMaxLines := AMaxLines;
// [x] if already has more lines than Max, update
end; // SetMaxLines
procedure TGFLog.SetDisplayControl( const AControl : TCustomMemo );
begin
if ( fDisplayControl = AControl ) then exit;
fDisplayControl := AControl;
// [x]
end; // SetDisplayControl
function TGFLog.AddEx( Sender : TObject; AStr : string ) : integer;
begin
if assigned( Sender ) then
result := AddLine( '(' + Sender.ClassName + ') ' + AStr )
else
result := AddLine( '(nil object) ' + AStr )
end; // AddEx
function TGFLog.Add( AStr : string ) : integer;
begin
result := AddLine( AStr )
end; // ADD
{
function TGFLog.SenderAdd( Sender : TObject; AStr : string ) : integer;
begin
result := AddLine( 'From ' + Sender.ClassName + ': ' + AStr )
end; // SenderAdd;
}
function TGFLog.AddLine( AStr : string ) : integer;
var
OKToAdd : boolean;
myStr : string;
begin
result := -1;
if ( not fActive ) then exit;
OKToAdd := true;
if assigned( fOnAdding ) then
fOnAdding( self, AStr, OkToAdd );
if OKToAdd then
begin
myStr := fIDString;
if fDateStamp then
myStr := myStr + datetostr( now ) + #32;
if fTimeStamp then
myStr := myStr + timetostr( now ) + #32;
result := fLines.Add( myStr + AStr );
if ( assigned( fOnAdded ) and ( result >= 0 )) then
fOnAdded( self, fLines[result] );
fModified := true;
if (( fMaxLines > 0 ) and ( fLines.Count > fMaxLines )) then
begin
Flush( true );
end;
end;
end; // AddLine;
procedure TGFLog.Flush( const AndSave : boolean );
begin
if AndSave then
Save( '' )
else
fModified := false;
if ( not fModified ) then
fLines.Clear;
end; // Flush
procedure TGFLog.Save( const aFileName : string );
var
LFile : textfile;
i : integer;
begin
if (( not fActive ) or ( not fModified )) then exit;
if ( fFileName = '' ) then
fFileName := aFileName;
if ( fFileName = '' ) then
fFileName := normalFN( changefileext( ParamStr( 0 ), '.log' ));
assignfile( LFile, fFileName );
try
if fileexists( fFileName ) then
begin
if ( fLogWasSaved or fAppendToFile ) then
append( LFile )
else
rewrite( LFile );
end
else
begin
rewrite( LFile );
end;
except
on E : Exception do
begin
fLastError := E.Message;
if fDeactivateOnError then
fActive := false;
if fShowErrors then
messagedlg( Name + ' Log error: Cannot REWRITE' +#13+ fLastError, mtError, [mbOK], 0 );
end;
end;
try
try
if ( fLines.Count > 0 ) then
begin
for i := 0 to pred( fLines.Count ) do
writeln( LFile, fLines[i] );
writeln( LFile, fSeparator );
writeln( LFile );
end;
except
on E : Exception do
begin
fLastError := E.Message;
if fDeactivateOnError then
fActive := false;
if fShowErrors then
messagedlg( Name + ' Log error: Cannot SAVE' +#13+ fLastError, mtError, [mbOK], 0 );
end;
end;
finally
fLogWasSaved := true;
closefile( LFile );
end;
fModified := false;
end; // Save
function TGFLog.GetLastError : string;
begin
result := fLastError;
fLastError := '';
end; // GetLastError
procedure Register;
begin
RegisterComponents( 'Samples', [TGFLog] );
end;
end.