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
BALL::LogStreamBuf::StreamStruct::min_level
int min_level
Definition: logStream.h:154
BALL::LogStream::clear
void clear()
BALL_EXPORT
#define BALL_EXPORT
Definition: COMMON/global.h:50
global.h
BALL::LogStreamBuf::LogStreamBuf
LogStreamBuf()
BALL::LogStreamNotifier
Definition: logStream.h:211
BALL::LogStream::insert
void insert(std::ostream &s, int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL)
BALL::LogStreamBuf::tmp_level_
int tmp_level_
Definition: logStream.h:201
BALL::LogStreamNotifier::stream_
std::stringstream stream_
Definition: logStream.h:232
BALL::LogStreamBuf::StreamStruct
Definition: logStream.h:151
BALL::LogStream::getLevel
int getLevel()
BALL::LogStreamBuf::incomplete_line_
string incomplete_line_
Definition: logStream.h:205
BALL::LogStreamNotifier::unregister
void unregister()
BALL::LogStream::info
LogStream & info(int n=0)
BALL::Log
BALL_EXPORT LogStream Log
BALL::LogStream::setMinLevel
void setMinLevel(const std::ostream &s, int min_level)
BALL::LogStream::setLevel
void setLevel(int level)
BALL::LogStreamBuf::MAX_LEVEL
static const int MAX_LEVEL
Definition: logStream.h:91
BALL::LogStream::error
LogStream & error(int n=0)
BALL::LogStream::insertNotification
void insertNotification(std::ostream &s, LogStreamNotifier &target, int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL)
BALL::LogStreamBuf::MIN_LEVEL
static const int MIN_LEVEL
Definition: logStream.h:92
BALL::LogStreamBuf::dump
virtual void dump(std::ostream &s)
BALL::LogStreamBuf::LoglineStruct::time
Time time
Definition: logStream.h:180
BALL::LogStream::getNumberOfLines
Size getNumberOfLines(int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL) const
BALL::LogStreamBuf::StreamStruct::~StreamStruct
~StreamStruct()
Definition: logStream.h:168
BALL::LogStreamBuf::sync
int sync(bool force_flush)
BALL::LogStreamNotifier::registered_at_
LogStream * registered_at_
Definition: logStream.h:234
BALL::LogStream::LogStreamLevel
LogStreamLevel
Definition: logStream.h:262
BALL::LogStreamNotifier::LogStreamNotifier
LogStreamNotifier()
BALL::LogStreamBuf::StreamStruct::StreamStruct
StreamStruct()
Definition: logStream.h:159
BALL::LogStream::disableOutput
void disableOutput()
Disable all output.
BALL::LogStreamNotifier::logNotify
virtual void logNotify()
BALL
Definition: constants.h:13
BALL::LogStream::rdbuf
LogStreamBuf * rdbuf()
BALL::LogStream::~LogStream
virtual ~LogStream()
BALL::LogStream::remove
void remove(std::ostream &s)
BALL::LogStreamBuf::loglines_
vector< Logline > loglines_
Definition: logStream.h:197
BALL::LogStreamBuf::sync
virtual int sync()
BALL::LogStream::warn
LogStream & warn(int n=0)
BALL::LogStream::outputEnabled
bool outputEnabled() const
Is Output enabled?
BALL::Constants::c
BALL_EXTERN_VARIABLE const double c
Definition: constants.h:149
BALL::LogStreamBuf::LoglineStruct::text
string text
Definition: logStream.h:179
BALL::LogStreamBuf::StreamStruct::target
LogStreamNotifier * target
Definition: logStream.h:156
BALL_INDEX_TYPE
BALL_SIZE_TYPE
BALL::LogStream::level
LogStream & level(int level)
BALL::LogStreamNotifier::registerAt
void registerAt(LogStream &log_stream, int min_level=LogStreamBuf::MIN_LEVEL, int max_level=LogStreamBuf::MAX_LEVEL)
BALL::LogStream::enableOutput
void enableOutput()
Enable all output.
BALL::LogStream::setMaxLevel
void setMaxLevel(const std::ostream &s, int max_level)
BALL::LogStreamBuf::MAX_TIME
static const Time MAX_TIME
Definition: logStream.h:93
BALL::LogStreamBuf::StreamStruct::prefix
string prefix
Definition: logStream.h:153
BALL::LogStreamBuf::LoglineStruct
Definition: logStream.h:177
BALL::LogStream::filterLines
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
BALL::LogStreamBuf
Definition: logStream.h:82
debug.h
BALL::Time
time_t Time
Definition: COMMON/global.h:123
BALL::LogStream::getLineTime
Time getLineTime(const Index &index) const
BALL::LogStreamBuf::StreamStruct::stream
std::ostream * stream
Definition: logStream.h:152
BALL::LogStreamBuf::stream_list_
list< StreamStruct > stream_list_
Definition: logStream.h:203
BALL::LogStream::getLineText
string getLineText(const Index &index) const
BALL::LogStream::getLineLevel
int getLineLevel(const Index &index) const
BALL::LogStreamBuf::level_
int level_
Definition: logStream.h:199
BALL::LogStreamNotifier::~LogStreamNotifier
virtual ~LogStreamNotifier()
BALL::LogStreamBuf::StreamStruct::max_level
int max_level
Definition: logStream.h:155
BALL::LogStreamBuf::overflow
virtual int overflow(int c=-1)
BALL::LogStreamBuf::LoglineStruct::level
int level
Definition: logStream.h:178
BALL::LogStream::flush
std::ostream & flush()
BALL::LogStreamBuf::~LogStreamBuf
virtual ~LogStreamBuf()
BALL::LogStream::LogStream
LogStream(LogStreamBuf *buf=0, bool delete_buf=true, bool associate_stdio=false)
BALL::LogStream
Definition: logStream.h:246
BALL::LogStream::setPrefix
void setPrefix(const std::ostream &s, const string &prefix)
BALL::LogStreamBuf::StreamStruct::disabled
bool disabled
Definition: logStream.h:157
BALL::LogStreamBuf::pbuf_
char * pbuf_
Definition: logStream.h:195
BALL::LogStreamBuf::expandPrefix_
string expandPrefix_(const string &prefix, int level, Time time) const
BALL::LogStreamBuf::LoglineStruct::LoglineStruct
LoglineStruct()
Definition: logStream.h:182