|
IBR-DTNSuite 0.6
|
00001 /* 00002 * Logger.h 00003 * 00004 * Copyright 2011 Johannes Morgenroth, IBR, TU Braunschweig 00005 * 00006 * Licensed under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 00019 #ifndef LOGGER_H_ 00020 #define LOGGER_H_ 00021 00022 //#define __IBRCOMMON_MULTITHREADED__ 00023 00024 #include <ibrcommon/thread/Queue.h> 00025 #include <ibrcommon/thread/Thread.h> 00026 #include <sys/time.h> 00027 #include <iostream> 00028 #include <sstream> 00029 #include <list> 00030 00061 #define IBRCOMMON_LOGGER_LEVEL \ 00062 ibrcommon::Logger::getVerbosity() 00063 00064 #define IBRCOMMON_LOGGER(level) \ 00065 { \ 00066 ibrcommon::Logger log = ibrcommon::Logger::level(); \ 00067 log 00068 00069 #define IBRCOMMON_LOGGER_DEBUG(verbosity) \ 00070 if (ibrcommon::Logger::getVerbosity() >= verbosity) \ 00071 { \ 00072 ibrcommon::Logger log = ibrcommon::Logger::debug(verbosity); \ 00073 log 00074 00075 #define IBRCOMMON_LOGGER_ENDL \ 00076 std::flush; \ 00077 log.print(); \ 00078 } 00079 00080 #define IBRCOMMON_LOGGER_ex(level) \ 00081 IBRCOMMON_LOGGER(level) << __PRETTY_FUNCTION__ << ": " 00082 00083 #define IBRCOMMON_LOGGER_DEBUG_ex(verbosity) \ 00084 IBRCOMMON_LOGGER_DEBUG(verbosity) << __FILE__ << ":" << __LINE__ << " in " << __PRETTY_FUNCTION__ << ": " 00085 00086 namespace ibrcommon 00087 { 00093 class Logger : public std::stringstream 00094 { 00095 public: 00096 enum LogOptions 00097 { 00098 LOG_NONE = 0, 00099 LOG_DATETIME = 1 << 0, /* print date/time on log messages */ 00100 LOG_HOSTNAME = 1 << 1, /* print hostname on log messages */ 00101 LOG_LEVEL = 1 << 2, /* print the log level on log messages */ 00102 LOG_TIMESTAMP = 1 << 3 /* print timestamp on log messages */ 00103 }; 00104 00105 enum LogLevel 00106 { 00107 LOGGER_EMERG = 1 << 0, /* system is unusable */ 00108 LOGGER_ALERT = 1 << 1, /* action must be taken immediately */ 00109 LOGGER_CRIT = 1 << 2, /* critical conditions */ 00110 LOGGER_ERR = 1 << 3, /* error conditions */ 00111 LOGGER_WARNING = 1 << 4, /* warning conditions */ 00112 LOGGER_NOTICE = 1 << 5, /* normal but significant condition */ 00113 LOGGER_INFO = 1 << 6, /* informational */ 00114 LOGGER_DEBUG = 1 << 7, /* debug-level messages */ 00115 LOGGER_ALL = 0xff 00116 }; 00117 00118 Logger(const Logger&); 00119 virtual ~Logger(); 00120 00121 static Logger emergency(); 00122 static Logger alert(); 00123 static Logger critical(); 00124 static Logger error(); 00125 static Logger warning(); 00126 static Logger notice(); 00127 static Logger info(); 00128 static Logger debug(int verbosity); 00129 00134 static void setVerbosity(const int verbosity); 00135 00140 static int getVerbosity(); 00141 00148 static void addStream(std::ostream &stream, const unsigned char logmask = LOGGER_INFO, const unsigned char options = LOG_NONE); 00149 00157 static void enableSyslog(const char *name, int option, int facility, const unsigned char logmask = LOGGER_INFO); 00158 00166 static void enableAsync(); 00167 00173 static void stop(); 00174 00178 void print(); 00179 00180 private: 00181 Logger(LogLevel level, int debug_verbosity = 0); 00182 00183 class LoggerOutput 00184 { 00185 public: 00186 LoggerOutput(std::ostream &stream, const unsigned char logmask, const unsigned char options); 00187 virtual ~LoggerOutput(); 00188 00189 void log(const Logger &log); 00190 00191 std::ostream &_stream; 00192 unsigned char _level; 00193 unsigned char _options; 00194 }; 00195 00196 class LogWriter : public ibrcommon::JoinableThread 00197 { 00198 public: 00199 LogWriter(); 00200 virtual ~LogWriter(); 00201 00202 void log(Logger &logger); 00203 00208 void setVerbosity(const int verbosity); 00209 00214 int getVerbosity(); 00215 00222 void addStream(std::ostream &stream, const unsigned char logmask = LOGGER_INFO, const unsigned char options = LOG_NONE); 00223 00231 void enableSyslog(const char *name, int option, int facility, const unsigned char logmask = LOGGER_INFO); 00232 00240 void enableAsync(); 00241 00242 protected: 00243 void flush(); 00244 void run(); 00245 bool __cancellation(); 00246 00247 private: 00251 void flush(const Logger &logger); 00252 00253 int _verbosity; 00254 bool _syslog; 00255 unsigned char _syslog_mask; 00256 00257 ibrcommon::Queue<Logger> _queue; 00258 bool _use_queue; 00259 std::list<LoggerOutput> _logger; 00260 }; 00261 00262 LogLevel _level; 00263 int _debug_verbosity; 00264 struct timeval _logtime; 00265 00266 static LogWriter _logwriter; 00267 }; 00268 } 00269 00270 #endif /* LOGGER_H_ */