00001
00002
00003
00004
00005 #ifndef BALL_COMMON_LOGSTREAM_H
00006 #define BALL_COMMON_LOGSTREAM_H
00007
00008 #ifndef BALL_CONFIG_CONFIG_H
00009 # include <BALL/CONFIG/config.h>
00010 #endif
00011
00012 #ifndef BALL_COMMON_GLOBAL_H
00013 # include <BALL/COMMON/global.h>
00014 #endif
00015
00016 #ifndef BALL_COMMON_DEBUG_H
00017 # include <BALL/COMMON/debug.h>
00018 #endif
00019
00020 #ifdef BALL_HAS_SYS_TIME_H
00021 # include <sys/time.h>
00022 #endif
00023
00024 #ifdef BALL_HAS_TIME_H
00025 # include <time.h>
00026 #endif
00027
00028 #ifdef BALL_HAS_SSTREAM
00029 # include <sstream>
00030 #else
00031 # include <strstream>
00032 #endif
00033
00034 #include <iostream>
00035 #include <list>
00036 #include <vector>
00037 #include <string>
00038
00039
00040 using std::list;
00041 using std::vector;
00042 using std::string;
00043
00044 namespace BALL
00045 {
00046
00071
00072
00073 class LogStream;
00074 class LogStreamNotifier;
00075
00095 class BALL_EXPORT LogStreamBuf
00096 : public std::streambuf
00097 {
00098
00099 friend class LogStream;
00100
00101 public:
00102
00106 static const int MAX_LEVEL;
00107 static const int MIN_LEVEL;
00108 static const Time MAX_TIME;
00110
00114
00118 LogStreamBuf();
00119
00123 virtual ~LogStreamBuf();
00124
00126
00127
00131
00136 virtual void dump(std::ostream& s);
00137
00139
00143
00154 virtual int sync();
00155
00160 virtual int overflow(int c = -1);
00162
00163 BALL_EXPORT struct StreamStruct
00164 {
00165 std::ostream* stream;
00166 string prefix;
00167 int min_level;
00168 int max_level;
00169 LogStreamNotifier* target;
00170
00171 StreamStruct()
00172 : stream(0),
00173 min_level(MIN_LEVEL),
00174 max_level(MAX_LEVEL),
00175 target(0)
00176 {
00177 }
00178
00179
00180 ~StreamStruct()
00181 {
00182 }
00183 };
00184
00185
00186 protected:
00187
00188 struct LoglineStruct
00189 {
00190 int level;
00191 string text;
00192 Time time;
00193
00194 LoglineStruct()
00195 : level(0),
00196 text(""),
00197 time(0)
00198 {}
00199 };
00200
00201 typedef struct LoglineStruct Logline;
00202
00203
00204
00205 string expandPrefix_(const string& prefix, int level, Time time) const;
00206
00207 char* pbuf_;
00208
00209 vector<Logline> loglines_;
00210
00211 int level_;
00212
00213 int tmp_level_;
00214
00215 list<StreamStruct> stream_list_;
00216
00217 string incomplete_line_;
00218 };
00219
00220
00222 class BALL_EXPORT LogStreamNotifier
00223 {
00224 public:
00225
00227 LogStreamNotifier();
00228
00230 virtual ~LogStreamNotifier();
00231
00233 virtual void logNotify();
00234
00236 void registerAt(LogStream& log_stream,
00237 int min_level = LogStreamBuf::MIN_LEVEL,
00238 int max_level = LogStreamBuf::MAX_LEVEL);
00240 void unregister();
00241
00242 protected:
00243
00244 std::stringstream stream_;
00245
00246 LogStream* registered_at_;
00247 };
00248
00249
00250
00256 class BALL_EXPORT LogStream
00257 : public std::ostream
00258 {
00259 public:
00260
00261
00265
00273 enum LogStreamLevel
00274 {
00278 ERROR_LEVEL = 2000 ,
00279
00282 WARNING_LEVEL = 1000,
00285 INFORMATION_LEVEL = 0
00286 };
00287
00289
00293
00304 LogStream(LogStreamBuf* buf = 0, bool delete_buf = true, bool associate_stdio = false);
00305
00309 virtual ~LogStream();
00310
00312
00316
00320 LogStreamBuf* rdbuf();
00321
00324 LogStreamBuf* operator -> ();
00326
00330
00337 void setLevel(int level);
00338
00346 int getLevel();
00347
00361 LogStream& level(int level);
00362
00367 LogStream& info(int n = 0);
00368
00373 LogStream& error(int n = 0);
00374
00379 LogStream& warn(int n = 0);
00380
00382
00386
00401 void insert
00402 (std::ostream& s, int min_level = LogStreamBuf::MIN_LEVEL,
00403 int max_level = LogStreamBuf::MAX_LEVEL);
00404
00412 void remove(std::ostream& s);
00413
00416 void insertNotification(std::ostream& s,
00417 LogStreamNotifier& target,
00418 int min_level = LogStreamBuf::MIN_LEVEL,
00419 int max_level = LogStreamBuf::MAX_LEVEL);
00420
00428 void setMinLevel(const std::ostream& s, int min_level);
00429
00437 void setMaxLevel(const std::ostream& s, int max_level);
00438
00457 void setPrefix(const std::ostream& s, const string& prefix);
00458
00460 void disableOutput() ;
00461
00463 void enableOutput() ;
00464
00466 bool outputEnabled() const
00467 ;
00468
00470 void flush()
00471 ;
00473
00476
00481 void clear();
00482
00492 Size getNumberOfLines
00493 (int min_level = LogStreamBuf::MIN_LEVEL,
00494 int max_level = LogStreamBuf::MAX_LEVEL) const;
00495
00502 string getLineText(const Index& index) const;
00503
00508 Time getLineTime(const Index& index) const;
00509
00515 int getLineLevel(const Index& index) const;
00516
00525 list<int> filterLines
00526 (int min_level = LogStreamBuf::MIN_LEVEL, int max_level = LogStreamBuf::MAX_LEVEL,
00527 Time earliest = 0, Time latest = LogStreamBuf::MAX_TIME,
00528 const string& s = "") const;
00530
00531 private:
00532
00533 typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
00534
00535 StreamIterator findStream_(const std::ostream& stream);
00536 bool hasStream_(std::ostream& stream);
00537 bool bound_() const;
00538
00539
00540
00541
00542
00543 bool delete_buffer_;
00544 bool disable_output_;
00545 };
00546
00547
00552 BALL_EXPORT extern LogStream Log;
00553
00555
00556 # ifndef BALL_NO_INLINE_FUNCTIONS
00557 # include <BALL/COMMON/logStream.iC>
00558 # endif
00559
00560 }
00561
00562 #endif // BALL_COMMON_LOGSTREAM_H