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 <sys/time.h>
00016 #include <iostream>
00017 #include <sstream>
00018 #include <list>
00019
00050 #define IBRCOMMON_LOGGER_LEVEL \
00051 ibrcommon::Logger::getVerbosity()
00052
00053 #define IBRCOMMON_LOGGER(level) \
00054 { \
00055 ibrcommon::Logger log = ibrcommon::Logger::level(); \
00056 log
00057
00058 #define IBRCOMMON_LOGGER_DEBUG(verbosity) \
00059 if (ibrcommon::Logger::getVerbosity() >= verbosity) \
00060 { \
00061 ibrcommon::Logger log = ibrcommon::Logger::debug(verbosity); \
00062 log
00063
00064 #define IBRCOMMON_LOGGER_ENDL \
00065 std::flush; \
00066 log.print(); \
00067 }
00068
00069 #define IBRCOMMON_LOGGER_ex(level) \
00070 IBRCOMMON_LOGGER(level) << __PRETTY_FUNCTION__ << ": "
00071
00072 #define IBRCOMMON_LOGGER_DEBUG_ex(verbosity) \
00073 IBRCOMMON_LOGGER_DEBUG(verbosity) << __FILE__ << ":" << __LINE__ << " in " << __PRETTY_FUNCTION__ << ": "
00074
00075 namespace ibrcommon
00076 {
00082 class Logger : public std::stringstream
00083 {
00084 public:
00085 enum LogOptions
00086 {
00087 LOG_NONE = 0,
00088 LOG_DATETIME = 1 << 0,
00089 LOG_HOSTNAME = 1 << 1,
00090 LOG_LEVEL = 1 << 2,
00091 LOG_TIMESTAMP = 1 << 3
00092 };
00093
00094 enum LogLevel
00095 {
00096 LOGGER_EMERG = 1 << 0,
00097 LOGGER_ALERT = 1 << 1,
00098 LOGGER_CRIT = 1 << 2,
00099 LOGGER_ERR = 1 << 3,
00100 LOGGER_WARNING = 1 << 4,
00101 LOGGER_NOTICE = 1 << 5,
00102 LOGGER_INFO = 1 << 6,
00103 LOGGER_DEBUG = 1 << 7,
00104 LOGGER_ALL = 0xff
00105 };
00106
00107 Logger(const Logger&);
00108 virtual ~Logger();
00109
00110 static Logger emergency();
00111 static Logger alert();
00112 static Logger critical();
00113 static Logger error();
00114 static Logger warning();
00115 static Logger notice();
00116 static Logger info();
00117 static Logger debug(int verbosity);
00118
00123 static void setVerbosity(const int verbosity);
00124
00129 static int getVerbosity();
00130
00137 static void addStream(std::ostream &stream, const unsigned char logmask = LOGGER_INFO, const unsigned char options = LOG_NONE);
00138
00146 static void enableSyslog(const char *name, int option, int facility, const unsigned char logmask = LOGGER_INFO);
00147
00155 static void enableAsync();
00156
00162 static void stop();
00163
00167 void print();
00168
00169 private:
00170 Logger(LogLevel level, int debug_verbosity = 0);
00171
00172 class LoggerOutput
00173 {
00174 public:
00175 LoggerOutput(std::ostream &stream, const unsigned char logmask, const unsigned char options);
00176 virtual ~LoggerOutput();
00177
00178 void log(const Logger &log);
00179
00180 std::ostream &_stream;
00181 unsigned char _level;
00182 unsigned char _options;
00183 };
00184
00185 class LogWriter : public ibrcommon::JoinableThread
00186 {
00187 public:
00188 LogWriter();
00189 virtual ~LogWriter();
00190
00191 void log(Logger &logger);
00192
00197 void setVerbosity(const int verbosity);
00198
00203 int getVerbosity();
00204
00211 void addStream(std::ostream &stream, const unsigned char logmask = LOGGER_INFO, const unsigned char options = LOG_NONE);
00212
00220 void enableSyslog(const char *name, int option, int facility, const unsigned char logmask = LOGGER_INFO);
00221
00229 void enableAsync();
00230
00231 protected:
00232 void flush();
00233 void run();
00234 bool __cancellation();
00235
00236 private:
00240 void flush(const Logger &logger);
00241
00242 int _verbosity;
00243 bool _syslog;
00244 unsigned char _syslog_mask;
00245
00246 ibrcommon::Queue<Logger> _queue;
00247 bool _use_queue;
00248 std::list<LoggerOutput> _logger;
00249 };
00250
00251 LogLevel _level;
00252 int _debug_verbosity;
00253 struct timeval _logtime;
00254
00255 static LogWriter _logwriter;
00256 };
00257 }
00258
00259 #endif