BALL
1.5.0
include
BALL
COMMON
logStream.h
Go to the documentation of this file.
1
// -*- Mode: C++; tab-width: 2; -*-
2
// vi: set ts=2:
3
//
4
5
#ifndef BALL_COMMON_LOGSTREAM_H
6
#define BALL_COMMON_LOGSTREAM_H
7
8
#include <
BALL/COMMON/global.h
>
9
#include <
BALL/COMMON/debug.h
>
10
#include <BALL/CONFIG/config.h>
11
12
#ifdef BALL_HAS_SYS_TIME_H
13
# include <sys/time.h>
14
#endif
15
16
#ifdef BALL_HAS_TIME_H
17
# include <time.h>
18
#endif
19
20
#include <iostream>
21
#include <list>
22
#include <sstream>
23
#include <vector>
24
25
using
std::list;
26
using
std::vector;
27
using
std::string;
28
29
namespace
BALL
30
{
31
56
57
// forward declarations
58
class
LogStream;
59
class
LogStreamNotifier;
60
80
class
BALL_EXPORT
LogStreamBuf
81
:
public
std::streambuf
82
{
83
84
friend
class
LogStream
;
85
86
public
:
87
91
static
const
int
MAX_LEVEL
;
92
static
const
int
MIN_LEVEL
;
93
static
const
Time
MAX_TIME
;
95
99
103
LogStreamBuf
();
104
108
virtual
~
LogStreamBuf
();
109
111
112
116
121
virtual
void
dump(std::ostream& s);
122
124
128
139
virtual
int
sync();
140
141
int
sync(
bool
force_flush);
142
147
virtual
int
overflow(
int
c
= -1);
149
150
struct
BALL_EXPORT
StreamStruct
151
{
152
std::ostream*
stream
;
153
string
prefix
;
154
int
min_level
;
155
int
max_level
;
156
LogStreamNotifier
*
target
;
157
bool
disabled
;
158
159
StreamStruct
()
160
: stream(0),
161
min_level(MIN_LEVEL),
162
max_level(MAX_LEVEL),
163
target(0)
164
{
165
}
166
167
// Delete the notification target.
168
~StreamStruct
()
169
{
170
}
171
};
172
173
174
protected
:
175
176
struct
LoglineStruct
177
{
178
int
level
;
179
string
text
;
180
Time
time
;
181
182
LoglineStruct
()
183
: level(0),
184
text(
""
),
185
time(0)
186
{}
187
};
188
189
typedef
struct
LoglineStruct
Logline
;
190
191
192
// interpret the prefix format string and return the expanded prefix
193
string
expandPrefix_(
const
string
& prefix,
int
level,
Time
time)
const
;
194
195
char
*
pbuf_
;
196
197
vector<Logline>
loglines_
;
198
199
int
level_
;
200
201
int
tmp_level_
;
202
203
list<StreamStruct>
stream_list_
;
204
205
string
incomplete_line_
;
206
};
207
208
210
class
BALL_EXPORT
LogStreamNotifier
211
{
212
public
:
213
215
LogStreamNotifier
();
216
218
virtual
~
LogStreamNotifier
();
219
221
virtual
void
logNotify();
222
224
void
registerAt(
LogStream
& log_stream,
225
int
min_level =
LogStreamBuf::MIN_LEVEL
,
226
int
max_level =
LogStreamBuf::MAX_LEVEL
);
228
void
unregister();
229
230
protected
:
231
232
std::stringstream
stream_
;
233
234
LogStream
*
registered_at_
;
235
};
236
237
238
244
class
BALL_EXPORT
LogStream
245
:
public
std::ostream
246
{
247
public
:
248
249
253
261
enum
LogStreamLevel
262
{
266
ERROR_LEVEL = 2000 ,
267
270
WARNING_LEVEL = 1000,
273
INFORMATION_LEVEL = 0
274
};
275
277
281
292
LogStream
(
LogStreamBuf
* buf = 0,
bool
delete_buf =
true
,
bool
associate_stdio =
false
);
293
297
virtual
~
LogStream
();
298
300
304
308
LogStreamBuf
* rdbuf();
309
312
LogStreamBuf
* operator -> ();
314
318
325
void
setLevel(
int
level);
326
334
int
getLevel();
335
349
LogStream
& level(
int
level);
350
355
LogStream
& info(
int
n = 0);
356
361
LogStream
& error(
int
n = 0);
362
367
LogStream
& warn(
int
n = 0);
368
370
374
389
void
insert
390
(std::ostream& s,
int
min_level =
LogStreamBuf::MIN_LEVEL
,
391
int
max_level =
LogStreamBuf::MAX_LEVEL
);
392
400
void
remove(std::ostream& s);
401
404
void
insertNotification(std::ostream& s,
405
LogStreamNotifier
& target,
406
int
min_level =
LogStreamBuf::MIN_LEVEL
,
407
int
max_level =
LogStreamBuf::MAX_LEVEL
);
408
416
void
setMinLevel(
const
std::ostream& s,
int
min_level);
417
425
void
setMaxLevel(
const
std::ostream& s,
int
max_level);
426
445
void
setPrefix(
const
std::ostream& s,
const
string
& prefix);
446
448
void
disableOutput() ;
449
451
void
enableOutput() ;
452
454
bool
outputEnabled()
const
455
;
456
459
std::ostream& flush();
460
462
465
470
void
clear();
471
481
Size
getNumberOfLines
482
(
int
min_level =
LogStreamBuf::MIN_LEVEL
,
483
int
max_level =
LogStreamBuf::MAX_LEVEL
)
const
;
484
491
string
getLineText(
const
Index
& index)
const
;
492
497
Time
getLineTime(
const
Index
& index)
const
;
498
504
int
getLineLevel(
const
Index
& index)
const
;
505
514
list<int> filterLines
515
(
int
min_level =
LogStreamBuf::MIN_LEVEL
,
int
max_level =
LogStreamBuf::MAX_LEVEL
,
516
Time
earliest = 0,
Time
latest =
LogStreamBuf::MAX_TIME
,
517
const
string
& s =
""
)
const
;
519
520
private
:
521
522
typedef
std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
523
524
StreamIterator findStream_(
const
std::ostream& stream);
525
bool
hasStream_(std::ostream& stream);
526
bool
bound_()
const
;
527
528
// flag needed by the destructor to decide whether the streambuf
529
// has to be deleted. If the default ctor is used to create
530
// the LogStreamBuf, delete_buffer_ is set to true and the ctor
531
// also deletes the buffer.
532
bool
delete_buffer_;
533
bool
disable_output_;
534
};
535
536
541
BALL_EXPORT
extern
LogStream
Log
;
542
544
545
# ifndef BALL_NO_INLINE_FUNCTIONS
546
# include <BALL/COMMON/logStream.iC>
547
# endif
548
549
}
// namespace BALL
550
551
#endif // BALL_COMMON_LOGSTREAM_H
BALL_INDEX_TYPE
BALL::LogStreamBuf::MIN_LEVEL
static const int MIN_LEVEL
Definition:
logStream.h:92
BALL_EXPORT
#define BALL_EXPORT
Definition:
COMMON/global.h:50
BALL::LogStreamBuf::LoglineStruct::text
string text
Definition:
logStream.h:179
BALL::LogStreamBuf::level_
int level_
Definition:
logStream.h:199
BALL::LogStreamBuf::pbuf_
char * pbuf_
Definition:
logStream.h:195
BALL::LogStreamBuf::StreamStruct::disabled
bool disabled
Definition:
logStream.h:157
BALL::Log
BALL_EXPORT LogStream Log
BALL::LogStreamBuf::StreamStruct::target
LogStreamNotifier * target
Definition:
logStream.h:156
BALL::LogStreamBuf::LoglineStruct::time
Time time
Definition:
logStream.h:180
BALL::LogStreamNotifier
Definition:
logStream.h:210
BALL_SIZE_TYPE
BALL::LogStreamBuf::StreamStruct
Definition:
logStream.h:150
BALL::LogStreamBuf::LoglineStruct::level
int level
Definition:
logStream.h:178
BALL::LogStreamBuf::StreamStruct::StreamStruct
StreamStruct()
Definition:
logStream.h:159
BALL::LogStreamBuf::StreamStruct::prefix
string prefix
Definition:
logStream.h:153
BALL
Definition:
constants.h:12
BALL::LogStreamBuf::StreamStruct::min_level
int min_level
Definition:
logStream.h:154
BALL::LogStreamBuf::loglines_
vector< Logline > loglines_
Definition:
logStream.h:197
BALL::LogStreamBuf::StreamStruct::~StreamStruct
~StreamStruct()
Definition:
logStream.h:168
BALL::LogStreamBuf::MAX_LEVEL
static const int MAX_LEVEL
Definition:
logStream.h:91
BALL::LogStreamBuf::StreamStruct::stream
std::ostream * stream
Definition:
logStream.h:152
BALL::LogStreamBuf::StreamStruct::max_level
int max_level
Definition:
logStream.h:155
BALL::LogStreamBuf::tmp_level_
int tmp_level_
Definition:
logStream.h:201
BALL::LogStreamBuf::incomplete_line_
string incomplete_line_
Definition:
logStream.h:205
BALL::Constants::c
const BALL_EXTERN_VARIABLE double c
Definition:
constants.h:149
BALL::LogStreamNotifier::registered_at_
LogStream * registered_at_
Definition:
logStream.h:234
BALL::LogStreamBuf
Definition:
logStream.h:80
BALL::LogStream
Definition:
logStream.h:244
BALL::LogStream::LogStreamLevel
LogStreamLevel
Definition:
logStream.h:261
debug.h
BALL::LogStreamBuf::stream_list_
list< StreamStruct > stream_list_
Definition:
logStream.h:203
BALL::Time
time_t Time
Definition:
COMMON/global.h:123
BALL::LogStreamBuf::LoglineStruct
Definition:
logStream.h:176
global.h
BALL::LogStreamBuf::LoglineStruct::LoglineStruct
LoglineStruct()
Definition:
logStream.h:182
BALL::LogStreamNotifier::stream_
std::stringstream stream_
Definition:
logStream.h:232
BALL::LogStreamBuf::MAX_TIME
static const Time MAX_TIME
Definition:
logStream.h:93
Generated by
1.8.16