-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogBase.h
355 lines (308 loc) · 12.8 KB
/
LogBase.h
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//---------------------------------------------------------------------------
#ifndef LogBaseH
#define LogBaseH
#include <tchar.h>
#include <System.hpp>
#include <System.Classes.hpp>
#include <System.SysUtils.hpp>
#include <System.SyncObjs.hpp>
#include <stdint.h>
#include <vector>
#include <iterator>
#include <functional>
#include <memory>
#include "LogCodes.h"
//#include "ToRawFn.h"
//---------------------------------------------------------------------------
namespace SvcApp {
//---------------------------------------------------------------------------
namespace Log {
//---------------------------------------------------------------------------
class LogParams {
private:
using ParamsCont = std::vector<Variant>;
ParamsCont params_;
void AppendLogParams( LPCTSTR Par ) {
params_.push_back( String( Par ) );
}
template<typename T>
void AppendLogParams( T const & Par ) {
params_.push_back( Par );
}
template<typename T, typename... A>
void AppendLogParams( T const & Par, A&&... Args ) {
params_.push_back( Par );
AppendLogParams( std::forward<A...>( Args )... );
}
public:
LogParams() {}
template<typename T, typename... A>
LogParams( T const & Par, A&&... Args ) {
params_.reserve( 1 + sizeof...( Args ) );
AppendLogParams( Par, std::forward<A...>( Args )... );
}
bool IsEmpty() const { return params_.empty(); }
ParamsCont::size_type Count() const { return params_.size(); }
template<typename OutputIt>
void CopyTo( OutputIt Out ) const {
std::copy( params_.begin(), params_.end(), Out );
}
};
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
template<
typename E=int,
typename M=int,
typename D=TDateTime
>
class LogItemBase {
public:
using EntityType = E;
using MessageType = M;
using DateTimeType = D;
using LogParamsType = LogParams;
LogItemBase() {}
virtual ~LogItemBase() {}
explicit LogItemBase( EntityType const & Entity, MessageType const & Message,
DateTimeType const & DateTime )
: entity_( Entity ) , message_( Message ), dateTime_( DateTime ) {}
explicit LogItemBase( EntityType const & Entity, MessageType const & Message,
LogParamsType const & Params, DateTimeType const & DateTime )
: entity_( Entity ), message_( Message ), params_( Params )
, dateTime_( DateTime ) {}
EntityType GetEntity() const { return entity_; }
MessageType GetMessage() const { return message_; }
DateTimeType GetDateTime() const { return dateTime_; }
LogParams const & GetParams() const { return params_; }
protected:
private:
EntityType entity_; // aka Category in Event Log
MessageType message_; // aka Facility Status Code in Event Log
DateTimeType dateTime_; // as generated
LogParams params_;
};
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
template<
typename E=int,
typename M=int,
typename P=DWORD,
typename TH=DWORD,
typename D=TDateTime
>
class LogItemExecData : public LogItemBase<E,M,D> {
public:
using EntityType = typename LogItemBase<E,M,D>::EntityType;
using MessageType = typename LogItemBase<E,M,D>::MessageType;
using DateTimeType = typename LogItemBase<E,M,D>::DateTimeType;
using LogParamsType = LogParams;
using ProcessIdType = P;
using ThreadIdType = TH;
LogItemExecData() {}
explicit LogItemExecData( EntityType Entity,
MessageType Message,
DateTimeType DateTime,
ProcessIdType ProcessId = ::GetCurrentProcessId(),
ThreadIdType ThreadId = ::GetCurrentThreadId() )
: LogItemBase<E,M,D>( Entity, Message, DateTime )
, processId_( ProcessId )
, threadId_( ThreadId ) {}
explicit LogItemExecData( EntityType Entity,
MessageType Message,
LogParamsType const & Params,
DateTimeType DateTime,
ProcessIdType ProcessId = ::GetCurrentProcessId(),
ThreadIdType ThreadId = ::GetCurrentThreadId() )
: LogItemBase<E,M,D>( Entity, Message, Params, DateTime )
, processId_( ProcessId )
, threadId_( ThreadId ) {}
ProcessIdType GetProcessId() const { return processId_; }
ThreadIdType GetThreadId() const { return threadId_; }
private:
ProcessIdType processId_;
ThreadIdType threadId_;
};
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
enum class DefSeverityType {
Emergency, // system is unusable // EVENTLOG_ERROR_TYPE
Alert, // action must be taken immediately // EVENTLOG_ERROR_TYPE
Critical, // critical conditions // EVENTLOG_ERROR_TYPE
Error, // error conditions // EVENTLOG_ERROR_TYPE
Warning, // warning conditions // EVENTLOG_WARNING_TYPE
Notice, // normal but significant condition // EVENTLOG_SUCCESS
Informational, // informational messages // EVENTLOG_INFORMATION_TYPE
Debug, // debug-level messages // EVENTLOG_INFORMATION_TYPE
};
//---------------------------------------------------------------------------
enum class DefFacilityType {
Kernel, // 0 Kernel
UserLevel, // 1 UserLevel
MailSystem, // 2 MailSystem
SystemDaemon, // 3 SystemDaemon
SecurityOne, // 4 SecurityOne
SysLogInternal, // 5 SysLogInternal
LPR, // 6 LPR
NNTP, // 7 NNTP
UUCP, // 8 UUCP
ClockDaemonOne, // 9 ClockDaemonOne
SecurityTwo, // 10 SecurityTwo
FTPDaemon, // 11 FTPDaemon
NTP, // 12 NTP
LogAudit, // 13 LogAudit
LogAlert, // 14 LogAlert
ClockDaemonTwo, // 15 ClockDaemonTwo
LocalUse0, // 16 LocalUse0
LocalUse1, // 17 LocalUse1
LocalUse2, // 18 LocalUse2
LocalUse3, // 19 LocalUse3
LocalUse4, // 20 LocalUse4
LocalUse5, // 21 LocalUse5
LocalUse6, // 22 LocalUse6
LocalUse7 // 23 LocalUse7
};
//---------------------------------------------------------------------------
template<
typename E = int
, typename M = int
, typename P = DWORD
, typename TH = DWORD
, typename D = TDateTime
, typename S = DefSeverityType
, typename F = DefFacilityType
>
class ClassifiedLogItem : public LogItemExecData<E,M,P,TH,D> {
public:
using EntityType = typename LogItemExecData<E,M,P,TH,D>::EntityType;
using MessageType = typename LogItemExecData<E,M,P,TH,D>::MessageType;
using DateTimeType = typename LogItemExecData<E,M,P,TH,D>::DateTimeType;
using LogParamsType = typename LogItemExecData<E,M,P,TH,D>::LogParamsType;
using ProcessIdType = typename LogItemExecData<E,M,P,TH,D>::ProcessIdType;
using ThreadIdType = typename LogItemExecData<E,M,P,TH,D>::ThreadIdType;
using SeverityType = S;
using FacilityType = F;
ClassifiedLogItem() {}
ClassifiedLogItem( EntityType Entity,
MessageType Message,
DateTimeType DateTime,
SeverityType Severity = S::Informational,
FacilityType Facility = F::UserLevel,
ProcessIdType const & ProcessId = ::GetCurrentProcessId(),
ThreadIdType const & ThreadId = ::GetCurrentThreadId() )
: LogItemExecData<E,M,P,TH,D>( Entity, Message, DateTime, ProcessId,
ThreadId )
, severity_( Severity )
, facility_( Facility ) {}
ClassifiedLogItem( EntityType Entity,
MessageType Message,
LogParamsType&& Params,
DateTimeType DateTime,
SeverityType Severity = S::Informational,
FacilityType Facility = F::UserLevel,
ProcessIdType ProcessId = ::GetCurrentProcessId(),
ThreadIdType const & ThreadId = ::GetCurrentThreadId() )
: LogItemExecData<E,M,P,TH,D>( Entity, Message, Params, DateTime,
ProcessId, ThreadId )
, severity_( Severity )
, facility_( Facility ) {}
SeverityType GetSeverity() const { return severity_; }
FacilityType GetFacility() const { return facility_; }
private:
SeverityType severity_;
FacilityType facility_;
};
//---------------------------------------------------------------------------
template<typename T>
String GetMessageText( T const & Item )
{
if ( Item.GetParams().IsEmpty() ) {
return LoadStr( Item.GetMessage() );
}
else {
LogParams const & Params = Item.GetParams();
std::vector<TVarRec> Array;
Array.reserve( Params.Count() );
Params.CopyTo( std::back_inserter( Array ) );
return FmtLoadStr( Item.GetMessage(), &Array[0], Array.size() - 1 );
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
using LogItem = ClassifiedLogItem<>;
template<typename T>
class LogBase {
public:
using ItemType = T;
void Append( T const & Item, bool HaveToFlush = true );
virtual ~LogBase() { Flush(); }
void Clear() { DoClear(); }
void Flush() { DoFlush(); }
private:
/*
using AppendCallbackRawType =
typename ToRawFnType<decltype(&LogBase<T>::Append)>::RawFnType;
*/
public:
// using AppendCallbackType = std::function<AppendCallbackRawType>;
using AppendCallbackType = std::function<void( T const &, bool )>;
void SetAppendCallback( AppendCallbackType Callback ) {
appendCallback_ = Callback;
}
protected:
virtual void DoAppend( T const & Item ) = 0;
virtual void DoClear() {}
virtual void DoFlush() {}
virtual bool DoHaveToLog( T const & Item ) const = 0;
private:
AppendCallbackType appendCallback_;
// Privato, perché l'abilitazione dei log è differenziata per destinazioni
bool HaveToLog( T const & Item ) const { return DoHaveToLog( Item ); }
};
//---------------------------------------------------------------------------
template<typename T>
void LogBase<T>::Append( T const & Item, bool HaveToFlush )
{
if ( HaveToLog( Item ) ) {
if ( appendCallback_ ) {
appendCallback_( Item, HaveToFlush );
}
DoAppend( Item );
if ( HaveToFlush ) {
DoFlush();
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
template<typename T>
class FilteredLog : public LogBase<T> {
public:
using FilterType = std::function<bool(T const &)>;
explicit FilteredLog( FilterType Filter = FilterType{} ) : filter_( Filter ) {}
protected:
virtual bool DoHaveToLog( T const & Item ) const override;
private:
FilterType filter_;
};
//---------------------------------------------------------------------------
template<typename T>
bool FilteredLog<T>::DoHaveToLog( T const & Item ) const
{
return filter_ ? filter_( Item ) : true;
}
//---------------------------------------------------------------------------
}; // End of namespace Log
//---------------------------------------------------------------------------
}; // End of namespace SvcApp
//---------------------------------------------------------------------------
#endif