Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef OPENMS_CONCEPT_LOGSTREAM_H
00036 #define OPENMS_CONCEPT_LOGSTREAM_H
00037
00038 #include <OpenMS/DATASTRUCTURES/String.h>
00039
00040
00041 #include <sstream>
00042 #include <iostream>
00043 #include <list>
00044 #include <vector>
00045 #include <ctime>
00046 #include <map>
00047
00048 namespace OpenMS
00049 {
00076 namespace Logger
00077 {
00078
00079 class LogStream;
00080 class LogStreamNotifier;
00081
00103 class OPENMS_DLLAPI LogStreamBuf :
00104 public std::streambuf
00105 {
00106
00107 friend class LogStream;
00108
00109 public:
00110
00112
00113 static const time_t MAX_TIME;
00114 static const std::string UNKNOWN_LOG_LEVEL;
00116
00118
00119
00124 explicit LogStreamBuf(std::string log_level = UNKNOWN_LOG_LEVEL);
00125
00129 virtual ~LogStreamBuf();
00130
00132
00134
00135
00146 virtual int sync();
00147
00152 virtual int overflow(int c = -1);
00154
00155
00157
00158
00163 void setLevel(std::string level);
00164
00165
00169 std::string getLevel();
00171
00177 OPENMS_DLLAPI struct StreamStruct
00178 {
00179 std::ostream * stream;
00180 std::string prefix;
00181 LogStreamNotifier * target;
00182
00183 StreamStruct() :
00184 stream(0),
00185 target(0)
00186 {}
00187
00189 ~StreamStruct()
00190 {}
00191
00192 };
00193
00199 void clearCache();
00200
00201 protected:
00202
00204 void distribute_(std::string outstring);
00205
00207 std::string expandPrefix_(const std::string & prefix, time_t time) const;
00208
00209 char * pbuf_;
00210 std::string level_;
00211 std::list<StreamStruct> stream_list_;
00212 std::string incomplete_line_;
00213
00215
00216
00220 struct LogCacheStruct
00221 {
00222 Size timestamp;
00223 int counter;
00224 };
00225
00230 Size log_cache_counter_;
00231
00233 std::map<std::string, LogCacheStruct> log_cache_;
00235 std::map<Size, std::string> log_time_cache_;
00236
00238 bool isInCache_(std::string const & line);
00239
00248 std::string addToCache_(std::string const & line);
00249
00251 Size getNextLogCounter_();
00252
00254 };
00255
00257 class OPENMS_DLLAPI LogStreamNotifier
00258 {
00259 public:
00260
00262 LogStreamNotifier();
00263
00265 virtual ~LogStreamNotifier();
00266
00268 virtual void logNotify();
00269
00271 void registerAt(LogStream & log_stream);
00272
00274 void unregister();
00275
00276 protected:
00277 std::stringstream stream_;
00278
00279 LogStream * registered_at_;
00280 };
00281
00282
00303 class OPENMS_DLLAPI LogStream :
00304 public std::ostream
00305 {
00306 public:
00307
00309
00310
00320 LogStream(LogStreamBuf * buf = 0, bool delete_buf = true, std::ostream * stream = 0);
00321
00323 virtual ~LogStream();
00325
00327
00328
00335 LogStreamBuf * rdbuf();
00336
00338 LogStreamBuf * operator->();
00340
00341
00343
00344
00350 void setLevel(std::string level);
00351
00352
00356 std::string getLevel();
00358
00360
00361
00376 void insert(std::ostream & s);
00377
00388 void remove(std::ostream & s);
00389
00391 void insertNotification(std::ostream & s,
00392 LogStreamNotifier & target);
00393
00414 void setPrefix(const std::ostream & s, const std::string & prefix);
00415
00416
00418 void setPrefix(const std::string & prefix);
00419
00421 void flush();
00423 private:
00424
00425 typedef std::list<LogStreamBuf::StreamStruct>::iterator StreamIterator;
00426
00427 StreamIterator findStream_(const std::ostream & stream);
00428 bool hasStream_(std::ostream & stream);
00429 bool bound_() const;
00430
00435 bool delete_buffer_;
00436
00437 };
00438
00439 }
00440
00441
00443 #define LOG_FATAL_ERROR \
00444 Log_fatal << __FILE__ << "(" << __LINE__ << "): "
00445
00447 #define LOG_ERROR \
00448 Log_error
00449
00451 #define LOG_WARN \
00452 Log_warn
00453
00455 #define LOG_INFO \
00456 Log_info
00457
00459 #define LOG_DEBUG \
00460 Log_debug << __FILE__ << "(" << __LINE__ << "): "
00461
00462 OPENMS_DLLAPI extern Logger::LogStream Log_fatal;
00463 OPENMS_DLLAPI extern Logger::LogStream Log_error;
00464 OPENMS_DLLAPI extern Logger::LogStream Log_warn;
00465 OPENMS_DLLAPI extern Logger::LogStream Log_info;
00466 OPENMS_DLLAPI extern Logger::LogStream Log_debug;
00467
00468 }
00469
00470 #endif // OPENMS_CONCEPT_LOGSTREAM_H