-
Notifications
You must be signed in to change notification settings - Fork 20
/
LogViewer.Subscribers.Base.pas
299 lines (243 loc) · 6.78 KB
/
LogViewer.Subscribers.Base.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
{
Copyright (C) 2013-2022 Tim Sinaeve tim.sinaeve@gmail.com
Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
}
unit LogViewer.Subscribers.Base;
{ Base class for subscribers implementing ISubscriber. }
interface
uses
System.Classes,
Spring,
DDuce.Logger.Interfaces,
LogViewer.Interfaces;
type
TSubscriber = class(TInterfacedObject, ISubscriber)
private
FReceiver : Weak<IChannelReceiver>; // weak reference
FOnReceiveMessage : Event<TReceiveMessageEvent>;
FEnabled : Boolean;
FMessageCount : Int64;
FSourceId : UInt32;
FSourceName : string;
FKey : string;
FOnChange : Event<TNotifyEvent>;
FLogMessageLevels : TLogMessageLevels;
FLogMessageTypes : TLogMessageTypes;
FTimeStampFirst : TDateTime;
FTimeStampLast : TDateTime;
FBytesReceived : Int64;
protected
{$REGION 'property access methods'}
function GetOnReceiveMessage: IEvent<TReceiveMessageEvent>;
function GetKey: string; virtual;
function GetMessageCount: Int64; virtual;
function GetReceiver: IChannelReceiver;
function GetEnabled: Boolean; virtual;
procedure SetEnabled(const Value: Boolean); virtual;
function GetSourceId: UInt32; virtual;
function GetSourceName: string;
function GetOnChange: IEvent<TNotifyEvent>;
function GetLogMessageLevels: TLogMessageLevels;
function GetLogMessageTypes: TLogMessageTypes;
procedure SetLogMessageLevels(const Value: TLogMessageLevels); virtual;
procedure SetLogMessageTypes(const Value: TLogMessageTypes); virtual;
function GetTimeStampFirst: TDateTime;
function GetTimeStampLast: TDateTime;
function GetBytesReceived: Int64;
function GetIsSourceActive: Boolean; virtual;
{$ENDREGION}
procedure Poll; virtual;
procedure Reset; virtual;
procedure Close; virtual;
procedure DoReceiveMessage(AStream: TStream); virtual;
procedure DoChange; virtual;
property Receiver: IChannelReceiver
read GetReceiver;
property BytesReceived: Int64
read GetBytesReceived;
property Enabled: Boolean
read GetEnabled write SetEnabled;
property MessageCount: Int64
read GetMessageCount;
property IsSourceActive: Boolean
read GetIsSourceActive;
property SourceId: UInt32
read GetSourceId;
property SourceName: string
read GetSourceName;
property TimeStampFirst: TDateTime
read GetTimeStampFirst;
property TimeStampLast: TDateTime
read GetTimeStampLast;
property LogMessageTypes: TLogMessageTypes
read GetLogMessageTypes write SetLogMessageTypes;
property LogMessageLevels: TLogMessageLevels
read GetLogMessageLevels write SetLogMessageLevels;
property OnReceiveMessage: IEvent<TReceiveMessageEvent>
read GetOnReceiveMessage;
property OnChange: IEvent<TNotifyEvent>
read GetOnChange;
public
constructor Create(
const AReceiver : IChannelReceiver;
ASourceId : Integer;
const AKey : string;
const ASourceName : string;
AEnabled : Boolean
);
destructor Destroy; override;
end;
implementation
uses
System.SysUtils,
DDuce.Logger;
{$REGION 'construction and destruction'}
constructor TSubscriber.Create(const AReceiver: IChannelReceiver;
ASourceId: Integer; const AKey, ASourceName: string; AEnabled: Boolean);
begin
inherited Create;
Guard.CheckNotNull(AReceiver, 'AReceiver');
FLogMessageTypes := AllMessages;
FLogMessageLevels := AllLevels;
FReceiver := AReceiver;
FSourceId := ASourceId;
FKey := AKey;
FSourceName := ASourceName;
Enabled := AEnabled;
end;
destructor TSubscriber.Destroy;
begin
FOnReceiveMessage.Clear;
FReceiver := nil;
inherited Destroy;
end;
{$ENDREGION}
{$REGION 'property access methods'}
function TSubscriber.GetBytesReceived: Int64;
begin
Result := FBytesReceived;
end;
function TSubscriber.GetEnabled: Boolean;
begin
Result := FEnabled;
end;
procedure TSubscriber.SetEnabled(const Value: Boolean);
begin
if Value <> Enabled then
begin
FEnabled := Value;
DoChange;
end;
end;
function TSubscriber.GetIsSourceActive: Boolean;
begin
Result := FEnabled;
end;
function TSubscriber.GetLogMessageLevels: TLogMessageLevels;
begin
Result := FLogMessageLevels;
end;
procedure TSubscriber.SetLogMessageLevels(const Value: TLogMessageLevels);
begin
if Value <> LogMessageLevels then
begin
FLogMessageLevels := Value;
DoChange;
end;
end;
function TSubscriber.GetLogMessageTypes: TLogMessageTypes;
begin
Result := FLogMessageTypes;
end;
procedure TSubscriber.SetLogMessageTypes(const Value: TLogMessageTypes);
begin
if Value <> LogMessageTypes then
begin
FLogMessageTypes := Value;
DoChange;
end;
end;
function TSubscriber.GetKey: string;
begin
Result := FKey;
end;
function TSubscriber.GetMessageCount: Int64;
begin
Result := FMessageCount;
end;
function TSubscriber.GetOnChange: IEvent<TNotifyEvent>;
begin
Result := FOnChange;
end;
function TSubscriber.GetOnReceiveMessage: IEvent<TReceiveMessageEvent>;
begin
Result := FOnReceiveMessage;
end;
function TSubscriber.GetReceiver: IChannelReceiver;
begin
Exit(FReceiver);
end;
function TSubscriber.GetSourceId: UInt32;
begin
Result := FSourceId;
end;
function TSubscriber.GetSourceName: string;
begin
Result := FSourceName;
end;
function TSubscriber.GetTimeStampFirst: TDateTime;
begin
Result := FTimeStampFirst;
end;
function TSubscriber.GetTimeStampLast: TDateTime;
begin
Result := FTimeStampLast;
end;
{$ENDREGION}
{$REGION 'event dispatch methods'}
procedure TSubscriber.DoChange;
begin
FOnChange.Invoke(Self);
end;
procedure TSubscriber.DoReceiveMessage(AStream: TStream);
begin
Guard.CheckNotNull(AStream, 'AStream');
if Enabled then
begin
if FMessageCount = 0 then
FTimeStampFirst := Now;
FTimeStampLast := Now;
Inc(FMessageCount);
Inc(FBytesReceived, AStream.Size);
FOnReceiveMessage.Invoke(Self, AStream);
DoChange;
end;
end;
{$ENDREGION}
{$REGION 'protected methods'}
procedure TSubscriber.Close;
begin
// implemented in descendants
end;
procedure TSubscriber.Poll;
begin
// implemented in descendants
end;
procedure TSubscriber.Reset;
begin
FMessageCount := 0;
FBytesReceived := 0;
FTimeStampFirst := 0;
FTimeStampLast := 0;
DoChange;
end;
{$ENDREGION}
end.