BALL  1.5.0
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 
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 
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 
151  {
152  std::ostream* stream;
153  string prefix;
157  bool disabled;
158 
160  : stream(0),
161  min_level(MIN_LEVEL),
162  max_level(MAX_LEVEL),
163  target(0)
164  {
165  }
166 
167  // Delete the notification target.
169  {
170  }
171  };
172 
173 
174  protected:
175 
177  {
178  int level;
179  string text;
181 
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 
202 
203  list<StreamStruct> stream_list_;
204 
206  };
207 
208 
211  {
212  public:
213 
216 
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 
235  };
236 
237 
238 
245  : public std::ostream
246  {
247  public:
248 
249 
253 
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 
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 
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
Definition: constants.h:13
BALL_EXPORT LogStream Log
time_t Time
BALL_INDEX_TYPE Index
BALL_EXTERN_VARIABLE const double c
Definition: constants.h:149
list< StreamStruct > stream_list_
Definition: logStream.h:203
static const int MAX_LEVEL
Definition: logStream.h:91
string expandPrefix_(const string &prefix, int level, Time time) const
int sync(bool force_flush)
string incomplete_line_
Definition: logStream.h:205
static const Time MAX_TIME
Definition: logStream.h:93
vector< Logline > loglines_
Definition: logStream.h:197
virtual ~LogStreamBuf()
virtual void dump(std::ostream &s)
virtual int sync()
static const int MIN_LEVEL
Definition: logStream.h:92
LogStreamNotifier * target
Definition: logStream.h:156
LogStream * registered_at_
Definition: logStream.h:234
std::stringstream stream_
Definition: logStream.h:232
void registerAt(LogStream &log_stream, int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL)
virtual void logNotify()
void insert(std::ostream &s, int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL)
void setMinLevel(const std::ostream &s, int min_level)
void remove(std::ostream &s)
void setMaxLevel(const std::ostream &s, int max_level)
void disableOutput()
Disable all output.
LogStream & level(int level)
void insertNotification(std::ostream &s, LogStreamNotifier &target, int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL)
LogStream & error(int n=0)
string getLineText(const Index &index) const
LogStream & warn(int n=0)
Size getNumberOfLines(int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL) const
bool outputEnabled() const
Is Output enabled?
void setLevel(int level)
list< int > filterLines(int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL, Time earliest=0, Time latest=LogStreamBuf::MAX_TIME, const string &s="") const
LogStream(LogStreamBuf *buf=0, bool delete_buf=true, bool associate_stdio=false)
std::ostream & flush()
Time getLineTime(const Index &index) const
void enableOutput()
Enable all output.
virtual ~LogStream()
LogStreamBuf * rdbuf()
void setPrefix(const std::ostream &s, const string &prefix)
int getLineLevel(const Index &index) const
LogStream & info(int n=0)
#define BALL_EXPORT
Definition: COMMON/global.h:50