BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 #ifndef BALL_CONFIG_CONFIG_H
9 # include <BALL/CONFIG/config.h>
10 #endif
11 
12 #ifndef BALL_COMMON_GLOBAL_H
13 # include <BALL/COMMON/global.h>
14 #endif
15 
16 #ifndef BALL_COMMON_DEBUG_H
17 # include <BALL/COMMON/debug.h>
18 #endif
19 
20 #ifdef BALL_HAS_SYS_TIME_H
21 # include <sys/time.h>
22 #endif
23 
24 #ifdef BALL_HAS_TIME_H
25 # include <time.h>
26 #endif
27 
28 #ifdef BALL_HAS_SSTREAM
29 # include <sstream>
30 #else
31 # include <strstream>
32 #endif
33 
34 #include <iostream>
35 #include <list>
36 #include <vector>
37 #include <string>
38 
39 
40 using std::list;
41 using std::vector;
42 using std::string;
43 
44 namespace BALL
45 {
46 
71 
72  // forward declarations
73  class LogStream;
74  class LogStreamNotifier;
75 
96  : public std::streambuf
97  {
98 
99  friend class LogStream;
100 
101  public:
102 
106  static const int MAX_LEVEL;
107  static const int MIN_LEVEL;
108  static const Time MAX_TIME;
110 
114 
118  LogStreamBuf();
119 
123  virtual ~LogStreamBuf();
124 
126 
127 
131 
136  virtual void dump(std::ostream& s);
137 
139 
143 
154  virtual int sync();
155 
156  int sync(bool force_flush);
157 
162  virtual int overflow(int c = -1);
164 
166  {
167  std::ostream* stream;
168  string prefix;
172  bool disabled;
173 
175  : stream(0),
176  min_level(MIN_LEVEL),
177  max_level(MAX_LEVEL),
178  target(0)
179  {
180  }
181 
182  // Delete the notification target.
184  {
185  }
186  };
187 
188 
189  protected:
190 
192  {
193  int level;
194  string text;
196 
198  : level(0),
199  text(""),
200  time(0)
201  {}
202  };
203 
204  typedef struct LoglineStruct Logline;
205 
206 
207  // interpret the prefix format string and return the expanded prefix
208  string expandPrefix_(const string& prefix, int level, Time time) const;
209 
210  char* pbuf_;
211 
212  vector<Logline> loglines_;
213 
214  int level_;
215 
217 
218  list<StreamStruct> stream_list_;
219 
221  };
222 
223 
226  {
227  public:
228 
231 
233  virtual ~LogStreamNotifier();
234 
236  virtual void logNotify();
237 
239  void registerAt(LogStream& log_stream,
240  int min_level = LogStreamBuf::MIN_LEVEL,
241  int max_level = LogStreamBuf::MAX_LEVEL);
243  void unregister();
244 
245  protected:
246 
247  std::stringstream stream_;
248 
250  };
251 
252 
253 
260  : public std::ostream
261  {
262  public:
263 
264 
268 
277  {
281  ERROR_LEVEL = 2000 ,
282 
285  WARNING_LEVEL = 1000,
288  INFORMATION_LEVEL = 0
289  };
290 
292 
296 
307  LogStream(LogStreamBuf* buf = 0, bool delete_buf = true, bool associate_stdio = false);
308 
312  virtual ~LogStream();
313 
315 
319 
323  LogStreamBuf* rdbuf();
324 
327  LogStreamBuf* operator -> ();
329 
333 
340  void setLevel(int level);
341 
349  int getLevel();
350 
364  LogStream& level(int level);
365 
370  LogStream& info(int n = 0);
371 
376  LogStream& error(int n = 0);
377 
382  LogStream& warn(int n = 0);
383 
385 
389 
404  void insert
405  (std::ostream& s, int min_level = LogStreamBuf::MIN_LEVEL,
406  int max_level = LogStreamBuf::MAX_LEVEL);
407 
415  void remove(std::ostream& s);
416 
419  void insertNotification(std::ostream& s,
420  LogStreamNotifier& target,
421  int min_level = LogStreamBuf::MIN_LEVEL,
422  int max_level = LogStreamBuf::MAX_LEVEL);
423 
431  void setMinLevel(const std::ostream& s, int min_level);
432 
440  void setMaxLevel(const std::ostream& s, int max_level);
441 
460  void setPrefix(const std::ostream& s, const string& prefix);
461 
463  void disableOutput() ;
464 
466  void enableOutput() ;
467 
469  bool outputEnabled() const
470  ;
471 
474  std::ostream& flush();
475 
477 
480 
485  void clear();
486 
496  Size getNumberOfLines
497  (int min_level = LogStreamBuf::MIN_LEVEL,
498  int max_level = LogStreamBuf::MAX_LEVEL) const;
499 
506  string getLineText(const Index& index) const;
507 
512  Time getLineTime(const Index& index) const;
513 
519  int getLineLevel(const Index& index) const;
520 
529  list<int> filterLines
530  (int min_level = LogStreamBuf::MIN_LEVEL, int max_level = LogStreamBuf::MAX_LEVEL,
531  Time earliest = 0, Time latest = LogStreamBuf::MAX_TIME,
532  const string& s = "") const;
534 
535  private:
536 
537  typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
538 
539  StreamIterator findStream_(const std::ostream& stream);
540  bool hasStream_(std::ostream& stream);
541  bool bound_() const;
542 
543  // flag needed by the destructor to decide whether the streambuf
544  // has to be deleted. If the default ctor is used to create
545  // the LogStreamBuf, delete_buffer_ is set to true and the ctor
546  // also deletes the buffer.
547  bool delete_buffer_;
548  bool disable_output_;
549  };
550 
551 
556  BALL_EXPORT extern LogStream Log;
557 
559 
560 # ifndef BALL_NO_INLINE_FUNCTIONS
561 # include <BALL/COMMON/logStream.iC>
562 # endif
563 
564 } // namespace BALL
565 
566 #endif // BALL_COMMON_LOGSTREAM_H
std::stringstream stream_
Definition: logStream.h:247
static const Time MAX_TIME
Definition: logStream.h:108
BALL_EXPORT LogStream Log
static const int MIN_LEVEL
Definition: logStream.h:107
BALL_EXTERN_VARIABLE const double c
Definition: constants.h:149
time_t Time
string incomplete_line_
Definition: logStream.h:220
static const int MAX_LEVEL
Definition: logStream.h:106
list< StreamStruct > stream_list_
Definition: logStream.h:218
LogStreamNotifier * target
Definition: logStream.h:171
LogStream * registered_at_
Definition: logStream.h:249
vector< Logline > loglines_
Definition: logStream.h:212
#define BALL_EXPORT
Definition: COMMON/global.h:50