00001
00002
00003
00004
00005
00006
00007 #ifndef BALL_COMMON_LOGSTREAM_H
00008 #define BALL_COMMON_LOGSTREAM_H
00009
00010 #ifndef BALL_CONFIG_CONFIG_H
00011 # include <BALL/CONFIG/config.h>
00012 #endif
00013
00014 #ifndef BALL_COMMON_GLOBAL_H
00015 # include <BALL/COMMON/global.h>
00016 #endif
00017
00018 #ifndef BALL_COMMON_DEBUG_H
00019 # include <BALL/COMMON/debug.h>
00020 #endif
00021
00022 #ifdef BALL_HAS_SYS_TIME_H
00023 # include <sys/time.h>
00024 #endif
00025
00026 #ifdef BALL_HAS_TIME_H
00027 # include <time.h>
00028 #endif
00029
00030 #ifdef BALL_HAS_SSTREAM
00031 # include <sstream>
00032 #else
00033 # include <strstream>
00034 #endif
00035
00036 #include <iostream>
00037 #include <list>
00038 #include <vector>
00039 #include <string>
00040
00041
00042 using std::list;
00043 using std::vector;
00044 using std::string;
00045
00046 namespace BALL
00047 {
00048
00073
00074
00075 class LogStream;
00076 class LogStreamNotifier;
00077
00097 class BALL_EXPORT LogStreamBuf
00098 : public std::streambuf
00099 {
00100
00101 friend class LogStream;
00102
00103 public:
00104
00108 static const int MAX_LEVEL;
00109 static const int MIN_LEVEL;
00110 static const Time MAX_TIME;
00112
00116
00120 LogStreamBuf();
00121
00125 virtual ~LogStreamBuf();
00126
00128
00129
00133
00138 virtual void dump(std::ostream& s);
00139
00141
00145
00156 virtual int sync();
00157
00162 virtual int overflow(int c = -1);
00164
00165 BALL_EXPORT struct StreamStruct
00166 {
00167 std::ostream* stream;
00168 string prefix;
00169 int min_level;
00170 int max_level;
00171 LogStreamNotifier* target;
00172
00173 StreamStruct()
00174 : stream(0),
00175 min_level(MIN_LEVEL),
00176 max_level(MAX_LEVEL),
00177 target(0)
00178 {
00179 }
00180
00181
00182 ~StreamStruct()
00183 {
00184 }
00185 };
00186
00187
00188 protected:
00189
00190 struct LoglineStruct
00191 {
00192 int level;
00193 string text;
00194 Time time;
00195
00196 LoglineStruct()
00197 : level(0),
00198 text(""),
00199 time(0)
00200 {}
00201 };
00202
00203 typedef struct LoglineStruct Logline;
00204
00205
00206
00207 string expandPrefix_(const string& prefix, int level, Time time) const;
00208
00209 char* pbuf_;
00210
00211 vector<Logline> loglines_;
00212
00213 int level_;
00214
00215 int tmp_level_;
00216
00217 list<StreamStruct> stream_list_;
00218
00219 string incomplete_line_;
00220 };
00221
00222
00224 class BALL_EXPORT LogStreamNotifier
00225 {
00226 public:
00227
00229 LogStreamNotifier();
00230
00232 virtual ~LogStreamNotifier();
00233
00235 virtual void logNotify();
00236
00238 void registerAt(LogStream& log_stream,
00239 int min_level = LogStreamBuf::MIN_LEVEL,
00240 int max_level = LogStreamBuf::MAX_LEVEL);
00242 void unregister();
00243
00244 protected:
00245
00246 std::stringstream stream_;
00247
00248 LogStream* registered_at_;
00249 };
00250
00251
00252
00258 class BALL_EXPORT LogStream
00259 : public std::ostream
00260 {
00261 public:
00262
00263
00267
00275 enum LogStreamLevel
00276 {
00280 ERROR_LEVEL = 2000 ,
00281
00284 WARNING_LEVEL = 1000,
00287 INFORMATION_LEVEL = 0
00288 };
00289
00291
00295
00306 LogStream(LogStreamBuf* buf = 0, bool delete_buf = true, bool associate_stdio = false);
00307
00311 virtual ~LogStream();
00312
00314
00318
00322 LogStreamBuf* rdbuf();
00323
00326 LogStreamBuf* operator -> ();
00328
00332
00339 void setLevel(int level);
00340
00348 int getLevel();
00349
00363 LogStream& level(int level);
00364
00369 LogStream& info(int n = 0);
00370
00375 LogStream& error(int n = 0);
00376
00381 LogStream& warn(int n = 0);
00382
00384
00388
00403 void insert
00404 (std::ostream& s, int min_level = LogStreamBuf::MIN_LEVEL,
00405 int max_level = LogStreamBuf::MAX_LEVEL);
00406
00414 void remove(std::ostream& s);
00415
00418 void insertNotification(std::ostream& s,
00419 LogStreamNotifier& target,
00420 int min_level = LogStreamBuf::MIN_LEVEL,
00421 int max_level = LogStreamBuf::MAX_LEVEL);
00422
00430 void setMinLevel(const std::ostream& s, int min_level);
00431
00439 void setMaxLevel(const std::ostream& s, int max_level);
00440
00459 void setPrefix(const std::ostream& s, const string& prefix);
00460
00462 void disableOutput() ;
00463
00465 void enableOutput() ;
00466
00468 bool outputEnabled() const
00469 ;
00470
00472 void flush()
00473 ;
00475
00478
00483 void clear();
00484
00494 Size getNumberOfLines
00495 (int min_level = LogStreamBuf::MIN_LEVEL,
00496 int max_level = LogStreamBuf::MAX_LEVEL) const;
00497
00504 string getLineText(const Index& index) const;
00505
00510 Time getLineTime(const Index& index) const;
00511
00517 int getLineLevel(const Index& index) const;
00518
00527 list<int> filterLines
00528 (int min_level = LogStreamBuf::MIN_LEVEL, int max_level = LogStreamBuf::MAX_LEVEL,
00529 Time earliest = 0, Time latest = LogStreamBuf::MAX_TIME,
00530 const string& s = "") const;
00532
00533 private:
00534
00535 typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
00536
00537 StreamIterator findStream_(const std::ostream& stream);
00538 bool hasStream_(std::ostream& stream);
00539 bool bound_() const;
00540
00541
00542
00543
00544
00545 bool delete_buffer_;
00546 bool disable_output_;
00547 };
00548
00549
00554 BALL_EXPORT extern LogStream Log;
00555
00557
00558 # ifndef BALL_NO_INLINE_FUNCTIONS
00559 # include <BALL/COMMON/logStream.iC>
00560 # endif
00561
00562 }
00563
00564 #endif // BALL_COMMON_LOGSTREAM_H