00001
00002
00003
00004
00005
00006
00007
00008 #ifndef LOGGER_H_
00009 #define LOGGER_H_
00010
00011 #include <iostream>
00012 #include <sstream>
00013 #include <list>
00014
00045 #define IBRCOMMON_LOGGER(level) { ibrcommon::Logger log = ibrcommon::Logger::level(); log
00046 #define IBRCOMMON_LOGGER_DEBUG(verbosity) { ibrcommon::Logger log = ibrcommon::Logger::debug(verbosity); log
00047 #define IBRCOMMON_LOGGER_ENDL std::flush; log << ibrcommon::Logger::endl; }
00048 #define IBRCOMMON_LOGGER_LEVEL ibrcommon::Logger::getVerbosity()
00049
00050 namespace ibrcommon
00051 {
00057 class Logger : public std::stringstream
00058 {
00059 public:
00060 enum LogOptions
00061 {
00062 LOG_NONE = 0,
00063 LOG_TIMESTAMP = 1 << 0,
00064 LOG_HOSTNAME = 1 << 1,
00065 LOG_LEVEL = 1 << 2
00066 };
00067
00068 enum LogLevel
00069 {
00070 LOGGER_EMERG = 1 << 0,
00071 LOGGER_ALERT = 1 << 1,
00072 LOGGER_CRIT = 1 << 2,
00073 LOGGER_ERR = 1 << 3,
00074 LOGGER_WARNING = 1 << 4,
00075 LOGGER_NOTICE = 1 << 5,
00076 LOGGER_INFO = 1 << 6,
00077 LOGGER_DEBUG = 1 << 7,
00078 LOGGER_ALL = 0xff
00079 };
00080
00081 Logger(const Logger&);
00082 virtual ~Logger();
00083
00084 static Logger emergency();
00085 static Logger alert();
00086 static Logger critical();
00087 static Logger error();
00088 static Logger warning();
00089 static Logger notice();
00090 static Logger info();
00091 static Logger debug(int verbosity);
00092
00097 static void setVerbosity(const int verbosity);
00098
00103 static int getVerbosity();
00104
00111 static void addStream(std::ostream &stream, const unsigned char logmask = LOGGER_INFO, const unsigned char options = LOG_NONE);
00112
00120 static void enableSyslog(const char *name, int option, int facility, const unsigned char logmask = LOGGER_INFO);
00121
00125 void flush();
00126
00132 class endl_flag
00133 {
00134 public:
00135 endl_flag() {};
00136 ~endl_flag() {};
00137 };
00138
00139 static endl_flag endl;
00140
00141 friend
00142 Logger& operator<<(Logger &logger, endl_flag&)
00143 {
00144 logger.flush();
00145 return logger;
00146 }
00147
00148 private:
00149 Logger(LogLevel level, int debug_verbosity = 0);
00150
00151 class LoggerOutput
00152 {
00153 public:
00154 LoggerOutput(std::ostream &stream, const unsigned char logmask, const unsigned char options);
00155 ~LoggerOutput();
00156
00157 void log(const Logger &log);
00158
00159 std::ostream &_stream;
00160 unsigned char _level;
00161 unsigned char _options;
00162 };
00163
00164 LogLevel _level;
00165 int _debug_verbosity;
00166
00167 static std::list<LoggerOutput> _logger;
00168 static int _verbosity;
00169 static bool _syslog;
00170 static unsigned char _syslog_mask;
00171 };
00172 }
00173
00174 #endif