Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef LOGGER_H_
00009 #define LOGGER_H_
00010
00011
00012
00013 #include <ibrcommon/thread/Queue.h>
00014 #include <ibrcommon/thread/Thread.h>
00015 #include <iostream>
00016 #include <sstream>
00017 #include <list>
00018
00049 #define IBRCOMMON_LOGGER_LEVEL ibrcommon::Logger::getVerbosity()
00050 #define IBRCOMMON_LOGGER(level) { ibrcommon::Logger log = ibrcommon::Logger::level(); log
00051 #define IBRCOMMON_LOGGER_DEBUG(verbosity) if (ibrcommon::Logger::getVerbosity() >= verbosity) { ibrcommon::Logger log = ibrcommon::Logger::debug(verbosity); log
00052 #define IBRCOMMON_LOGGER_ENDL std::flush; log << ibrcommon::Logger::endl; }
00053
00054 namespace ibrcommon
00055 {
00061 class Logger : public std::stringstream
00062 {
00063 public:
00064 enum LogOptions
00065 {
00066 LOG_NONE = 0,
00067 LOG_DATETIME = 1 << 0,
00068 LOG_HOSTNAME = 1 << 1,
00069 LOG_LEVEL = 1 << 2,
00070 LOG_TIMESTAMP = 1 << 3
00071 };
00072
00073 enum LogLevel
00074 {
00075 LOGGER_EMERG = 1 << 0,
00076 LOGGER_ALERT = 1 << 1,
00077 LOGGER_CRIT = 1 << 2,
00078 LOGGER_ERR = 1 << 3,
00079 LOGGER_WARNING = 1 << 4,
00080 LOGGER_NOTICE = 1 << 5,
00081 LOGGER_INFO = 1 << 6,
00082 LOGGER_DEBUG = 1 << 7,
00083 LOGGER_ALL = 0xff
00084 };
00085
00086 Logger(const Logger&);
00087 virtual ~Logger();
00088
00089 static Logger emergency();
00090 static Logger alert();
00091 static Logger critical();
00092 static Logger error();
00093 static Logger warning();
00094 static Logger notice();
00095 static Logger info();
00096 static Logger debug(int verbosity);
00097
00102 static void setVerbosity(const int verbosity);
00103
00108 static int getVerbosity();
00109
00116 static void addStream(std::ostream &stream, const unsigned char logmask = LOGGER_INFO, const unsigned char options = LOG_NONE);
00117
00125 static void enableSyslog(const char *name, int option, int facility, const unsigned char logmask = LOGGER_INFO);
00126
00130 void flush();
00131
00137 class endl_flag
00138 {
00139 public:
00140 endl_flag() {};
00141 ~endl_flag() {};
00142 };
00143
00144 static endl_flag endl;
00145
00153 static void enableAsync();
00154
00160 static void stop();
00161
00162 friend
00163 Logger& operator<<(Logger &logger, endl_flag&)
00164 {
00165 if (Logger::_use_queue)
00166 {
00167 Logger::_queue.push(logger);
00168 }
00169 else
00170 {
00171 logger.flush();
00172 }
00173 return logger;
00174 }
00175
00176 protected:
00177 static ibrcommon::Queue<Logger> _queue;
00178
00179 private:
00180 Logger(LogLevel level, int debug_verbosity = 0);
00181
00182 class LoggerOutput
00183 {
00184 public:
00185 LoggerOutput(std::ostream &stream, const unsigned char logmask, const unsigned char options);
00186 ~LoggerOutput();
00187
00188 void log(const Logger &log);
00189
00190 std::ostream &_stream;
00191 unsigned char _level;
00192 unsigned char _options;
00193 };
00194
00195 class LogWriter : public ibrcommon::JoinableThread
00196 {
00197 public:
00198 LogWriter();
00199 virtual ~LogWriter();
00200
00201 protected:
00202 void flush();
00203 void run();
00204 bool __cancellation();
00205 };
00206
00207 LogLevel _level;
00208 int _debug_verbosity;
00209
00210 static bool _use_queue;
00211 static LogWriter _logwriter;
00212 static std::list<LoggerOutput> _logger;
00213 static int _verbosity;
00214 static bool _syslog;
00215 static unsigned char _syslog_mask;
00216
00217 struct timeval _logtime;
00218 };
00219 }
00220
00221 #endif