forked from IndySockets/Indy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdLogBase.pas
201 lines (169 loc) · 4.3 KB
/
IdLogBase.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
{
$Project$
$Workfile$
$Revision$
$DateUTC$
$Id$
This file is part of the Indy (Internet Direct) project, and is offered
under the dual-licensing agreement described on the Indy website.
(http://www.indyproject.org/)
Copyright:
(c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
}
{
$Log$
}
{
Rev 1.5 2004.02.03 4:17:14 PM czhower
For unit name changes.
Rev 1.4 2004.01.20 10:03:28 PM czhower
InitComponent
Rev 1.3 2003.10.17 6:15:54 PM czhower
Upgrades
Rev 1.2 2003.10.14 1:27:08 PM czhower
Uupdates + Intercept support
Rev 1.1 6/16/2003 10:39:02 AM EHill
Done: Expose Open/Close as public in TIdLogBase
Rev 1.0 11/13/2002 07:55:58 AM JPMugaas
}
unit IdLogBase;
interface
{$I IdCompilerDefines.inc}
//Put FPC into Delphi mode
uses
Classes,
IdIntercept, IdGlobal, IdSocketHandle, IdBaseComponent;
type
TIdLogBase = class(TIdConnectionIntercept)
protected
FActive: Boolean;
FLogTime: Boolean;
FReplaceCRLF: Boolean;
//
procedure InitComponent; override;
procedure LogStatus(const AText: string); virtual; abstract;
procedure LogReceivedData(const AText, AData: string); virtual; abstract;
procedure LogSentData(const AText, AData: string); virtual; abstract;
procedure SetActive(AValue: Boolean); virtual;
procedure Loaded; override;
function ReplaceCR(const AString : String) : String;
public
procedure Open; virtual;
procedure Close; virtual;
procedure Connect(AConnection: TComponent); override;
destructor Destroy; override;
procedure Disconnect; override;
procedure Receive(var ABuffer: TIdBytes); override;
procedure Send(var ABuffer: TIdBytes); override;
published
property Active: Boolean read FActive write SetActive default False;
property LogTime: Boolean read FLogTime write FLogTime default True;
property ReplaceCRLF: Boolean read FReplaceCRLF write FReplaceCRLF default true;
end;
implementation
uses
IdResourceStringsCore, SysUtils;
const
LOldStr : array [0..2] of string =
( EOL, CR, LF );
LNewStr : array [0..2] of string =
( RSLogEOL, RSLogCR, RSLogLF );
{ TIdLogBase }
procedure TIdLogBase.Close;
begin
end;
procedure TIdLogBase.Connect(AConnection: TComponent);
begin
inherited Connect(AConnection);
if FActive then begin
LogStatus(RSLogConnected);
end;
end;
destructor TIdLogBase.Destroy;
begin
Active := False;
inherited Destroy;
end;
procedure TIdLogBase.Disconnect;
begin
if FActive then begin
LogStatus(RSLogDisconnected);
end;
inherited Disconnect;
end;
procedure TIdLogBase.InitComponent;
begin
inherited InitComponent;
FLogTime := True;
ReplaceCRLF := True;
end;
procedure TIdLogBase.Loaded;
var
b: Boolean;
begin
inherited Loaded;
b := FActive;
FActive := False;
Active := b;
end;
procedure TIdLogBase.Open;
begin
end;
procedure TIdLogBase.Receive(var ABuffer: TIdBytes);
var
s: string;
LMsg: string;
begin
// let the next Intercept in the chain decode its data first
inherited Receive(ABuffer);
if FActive then begin
LMsg := '';
if LogTime then begin
LMsg := DateTimeToStr(Now);
end;
s := BytesToStringRaw(ABuffer);
if FReplaceCRLF then begin
s := ReplaceCR(S);
end;
LogReceivedData(LMsg, s);
end;
end;
function TIdLogBase.ReplaceCR(const AString: String): String;
begin
Result := StringsReplace(AString, LOldStr, LNewStr);
end;
procedure TIdLogBase.Send(var ABuffer: TIdBytes);
var
s: string;
LMsg: string;
begin
if FActive then begin
LMsg := '';
if LogTime then begin
LMsg := DateTimeToStr(Now);
end;
s := BytesToStringRaw(ABuffer);
if FReplaceCRLF then begin
s := ReplaceCR(S);
end;
LogSentData(LMsg, s);
end;
// let the next Intercept in the chain encode its data next
inherited Send(ABuffer);
end;
procedure TIdLogBase.SetActive(AValue: Boolean);
begin
if IsDesignTime or IsLoading then begin
FActive := AValue;
end
else if FActive <> AValue then
begin
if AValue then begin
Open;
end else begin
Close;
end;
FActive := AValue;
end;
end;
end.