forked from cinecert/asdcplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KM_log.h
executable file
·328 lines (262 loc) · 9.57 KB
/
KM_log.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
/*
Copyright (c) 2004-2009, John Hurst
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*! \file KM_log.h
\version $Id$
\brief message logging API
*/
#ifndef _KM_LOG_H_
#define _KM_LOG_H_
#include <KM_platform.h>
#include <KM_mutex.h>
#include <KM_util.h>
#include <stdarg.h>
#include <errno.h>
#include <iosfwd>
#include <set>
#define LOG_MSG_IMPL(t) \
va_list args; \
va_start(args, fmt); \
vLogf((t), fmt, &args); \
va_end(args)
// Returns RESULT_PTR if the given argument is NULL.
# define KM_TEST_NULL_L(p) \
if ( (p) == 0 ) { \
DefaultLogSink().Error("NULL pointer in file %s, line %d\n", __FILE__, __LINE__); \
return Kumu::RESULT_PTR; \
}
// Returns RESULT_PTR if the given argument is NULL. It then
// assumes that the argument is a pointer to a string and returns
// RESULT_NULL_STR if the first character is '\0'.
//
# define KM_TEST_NULL_STR_L(p) \
KM_TEST_NULL_L(p); \
if ( (p)[0] == '\0' ) { \
DefaultLogSink().Error("Empty string in file %s, line %d\n", __FILE__, __LINE__); \
return Kumu::RESULT_NULL_STR; \
}
namespace Kumu
{
// no log message will exceed this length
const ui32_t MaxLogLength = 512;
//---------------------------------------------------------------------------------
// message logging
// Log messages are recorded by objects which implement the interface given
// in the class ILogSink below. The library maintains a pointer to a default
// log sink which is used by the library to report messages.
//
// types of log messages
enum LogType_t {
LOG_DEBUG, // detailed developer info
LOG_INFO, // developer info
LOG_WARN, // library non-fatal or near-miss error
LOG_ERROR, // library fatal error
LOG_NOTICE, // application user info
LOG_ALERT, // application non-fatal or near-miss error
LOG_CRIT, // application fatal error
LOG_MAX
};
// OR these values together to come up with sink filter flags.
// The default mask is LOG_ALLOW_ALL (all messages).
const i32_t LOG_ALLOW_DEBUG = 0x00000001;
const i32_t LOG_ALLOW_INFO = 0x00000002;
const i32_t LOG_ALLOW_WARN = 0x00000004;
const i32_t LOG_ALLOW_ERROR = 0x00000008;
const i32_t LOG_ALLOW_NOTICE = 0x00000010;
const i32_t LOG_ALLOW_ALERT = 0x00000020;
const i32_t LOG_ALLOW_CRIT = 0x00000040;
const i32_t LOG_ALLOW_NONE = 0x00000000;
const i32_t LOG_ALLOW_ALL = 0x000fffff;
// options are used to control display format default is 0.
const i32_t LOG_OPTION_TYPE = 0x01000000;
const i32_t LOG_OPTION_TIMESTAMP = 0x02000000;
const i32_t LOG_OPTION_PID = 0x04000000;
const i32_t LOG_OPTION_NONE = 0x00000000;
const i32_t LOG_OPTION_ALL = 0xfff00000;
// A log message with environmental metadata
class LogEntry : public IArchive
{
public:
ui32_t PID;
Timestamp EventTime;
LogType_t Type;
std::string Msg;
LogEntry() {}
LogEntry(ui32_t pid, LogType_t t, const char* m) : PID(pid), Type(t), Msg(m) { assert(m); }
virtual ~LogEntry() {}
// returns true if the message Type is present in the mask
bool TestFilter(i32_t mask_value) const;
// renders the message into outstr using the given dispaly options
// returns outstr&
std::string& CreateStringWithOptions(std::string& outstr, i32_t mask_value) const;
// IArchive
bool HasValue() const { return ! Msg.empty(); }
ui32_t ArchiveLength() const;
bool Archive(MemIOWriter* Writer) const;
bool Unarchive(MemIOReader* Reader);
};
//
std::basic_ostream<char, std::char_traits<char> >&
operator<<(std::basic_ostream<char, std::char_traits<char> >& strm, LogEntry const& Entry);
typedef ArchivableList<LogEntry> LogEntryList;
//
class ILogSink
{
protected:
i32_t m_filter;
i32_t m_options;
Mutex m_lock;
std::set<ILogSink*> m_listeners;
// you must obtain m_lock BEFORE calling this from your own WriteEntry
void WriteEntryToListeners(const LogEntry& entry)
{
std::set<ILogSink*>::iterator i;
for ( i = m_listeners.begin(); i != m_listeners.end(); ++i )
(*i)->WriteEntry(entry);
}
KM_NO_COPY_CONSTRUCT(ILogSink);
public:
ILogSink() : m_filter(LOG_ALLOW_ALL), m_options(LOG_OPTION_NONE) {}
virtual ~ILogSink() {}
void SetFilterFlag(i32_t f) { m_filter |= f; }
void UnsetFilterFlag(i32_t f) { m_filter &= ~f; }
bool TestFilterFlag(i32_t f) const { return ((m_filter & f) == f); }
void SetOptionFlag(i32_t o) { m_options |= o; }
void UnsetOptionFlag(i32_t o) { m_options &= ~o; }
bool TestOptionFlag(i32_t o) const { return ((m_options & o) == o); }
void AddListener(ILogSink& s) {
if ( &s != this )
{
AutoMutex l(m_lock);
m_listeners.insert(&s);
}
}
void DelListener(ILogSink& s) {
AutoMutex l(m_lock);
m_listeners.erase(&s);
}
// library messages
void Error(const char* fmt, ...) { LOG_MSG_IMPL(LOG_ERROR); }
void Warn(const char* fmt, ...) { LOG_MSG_IMPL(LOG_WARN); }
void Info(const char* fmt, ...) { LOG_MSG_IMPL(LOG_INFO); }
void Debug(const char* fmt, ...) { LOG_MSG_IMPL(LOG_DEBUG); }
// application messages
void Critical(const char* fmt, ...) { LOG_MSG_IMPL(LOG_CRIT); }
void Alert(const char* fmt, ...) { LOG_MSG_IMPL(LOG_ALERT); }
void Notice(const char* fmt, ...) { LOG_MSG_IMPL(LOG_NOTICE); }
// message with type
void Logf(LogType_t type, const char* fmt, ...) { LOG_MSG_IMPL(type); }
// actual log sink input
virtual void vLogf(LogType_t, const char*, va_list*);
virtual void WriteEntry(const LogEntry&) = 0;
};
// Sets the internal default sink to the given receiver. If the given value
// is zero, sets the default sink to the internally allocated stderr sink.
void SetDefaultLogSink(ILogSink* = 0);
// Returns the internal default sink.
ILogSink& DefaultLogSink();
// attach a log sink as a listener until deleted
class LogSinkListenContext
{
ILogSink* m_log_source;
ILogSink* m_sink;
KM_NO_COPY_CONSTRUCT(LogSinkListenContext);
LogSinkListenContext();
public:
LogSinkListenContext(ILogSink& source, ILogSink& sink)
{
m_log_source = &source;
m_sink = &sink;
m_log_source->AddListener(*m_sink);
}
~LogSinkListenContext()
{
m_log_source->DelListener(*m_sink);
}
};
//------------------------------------------------------------------------------------------
//
// collect log messages into the given list, does not test filter
class EntryListLogSink : public ILogSink
{
LogEntryList& m_Target;
KM_NO_COPY_CONSTRUCT(EntryListLogSink);
EntryListLogSink();
public:
EntryListLogSink(LogEntryList& target) : m_Target(target) {}
virtual ~EntryListLogSink() {}
void WriteEntry(const LogEntry& Entry);
};
// write messages to a POSIX stdio stream
class StdioLogSink : public ILogSink
{
FILE* m_stream;
KM_NO_COPY_CONSTRUCT(StdioLogSink);
public:
StdioLogSink() : m_stream(stderr) {}
StdioLogSink(FILE* stream) : m_stream(stream) {}
virtual ~StdioLogSink() {}
void WriteEntry(const LogEntry&);
};
#ifdef KM_WIN32
// write messages to the Win32 debug stream
class WinDbgLogSink : public ILogSink
{
KM_NO_COPY_CONSTRUCT(WinDbgLogSink);
public:
WinDbgLogSink() {}
virtual ~WinDbgLogSink() {}
void WriteEntry(const LogEntry&);
};
#endif
#ifndef KM_WIN32
// write messages to a POSIX file descriptor
class StreamLogSink : public ILogSink
{
int m_fd;
KM_NO_COPY_CONSTRUCT(StreamLogSink);
StreamLogSink();
public:
StreamLogSink(int fd) : m_fd(fd) {}
virtual ~StreamLogSink() {}
void WriteEntry(const LogEntry&);
};
// write messages to the syslog facility
class SyslogLogSink : public ILogSink
{
KM_NO_COPY_CONSTRUCT(SyslogLogSink);
SyslogLogSink();
public:
SyslogLogSink(const std::string& source_name, int facility);
virtual ~SyslogLogSink();
void WriteEntry(const LogEntry&);
};
// convert a string into the appropriate syslog facility id
int SyslogNameToFacility(const std::string& facility_name);
#endif
} // namespace Kumu
#endif // _KM_LOG_H_
//
// end KM_log.h
//