Rivet  1.8.0
Logging.hh
00001 #ifndef RIVET_LOGGING_HH
00002 #define RIVET_LOGGING_HH
00003 
00004 #include "Rivet/Rivet.hh"
00005 
00006 namespace Rivet {
00007 
00008 
00009   class Log {
00010   public:
00011 
00013     enum Level {
00014       TRACE = 0, DEBUG = 10, INFO = 20, WARN = 30, WARNING = 30, ERROR = 40, CRITICAL = 50, ALWAYS = 50
00015     };
00016 
00018     typedef std::map<std::string, Log*> LogMap;
00019 
00021     typedef std::map<std::string, int> LevelMap;
00022 
00024     typedef std::map<int, std::string> ColorCodes;
00025 
00026   private:
00028     static LogMap existingLogs;
00029 
00031     static LevelMap defaultLevels;
00032 
00034     static ColorCodes colorCodes;
00035 
00037     static std::string endColorCode;
00038 
00040     static bool showTimestamp;
00041 
00043     static bool showLogLevel;
00044 
00046     static bool showLoggerName;
00047 
00049     static bool useShellColors;
00050 
00051   public:
00053     static void setLevel(const std::string& name, int level);
00054     static void setLevels(const LevelMap& logLevels);
00055 
00056     static void setShowTimestamp(bool showTime=true) {
00057       showTimestamp = showTime;
00058     }
00059 
00060     static void setShowLevel(bool showLevel=true) {
00061       showLogLevel = showLevel;
00062     }
00063 
00064     static void setShowLoggerName(bool showName=true) {
00065       showLoggerName = showName;
00066     }
00067 
00068     static void setUseColors(bool useColors=true) {
00069       useShellColors = useColors;
00070     }
00071 
00072   protected:
00073 
00075 
00076 
00078     Log(const std::string& name);
00079 
00081     Log(const std::string& name, int level);
00082 
00084 
00085     static std::string getColorCode(int level);
00086 
00087   public:
00088 
00091     static Log& getLog(const std::string& name);
00092 
00093   public:
00095     int getLevel() const {
00096       return _level;
00097     }
00098 
00100     Log& setLevel(int level) {
00101       _level = level;
00102       return *this;
00103     }
00104 
00106     static Level getLevelFromName(const std::string& level);
00107 
00109     static std::string getLevelName(int level);
00110 
00112     std::string getName() const {
00113       return _name;
00114     }
00115 
00117     Log& setName(const std::string& name) {
00118       _name = name;
00119       return *this;
00120     }
00121 
00123     bool isActive(int level) const {
00124       return (level >= _level);
00125     }
00126 
00128 
00129     void trace(const std::string& message) { log(TRACE, message); }
00130 
00131     void debug(const std::string& message) { log(DEBUG, message); }
00132 
00133     void info(const std::string& message) { log(INFO, message); }
00134 
00135     void warn(const std::string& message) { log(WARN, message); }
00136 
00137     void error(const std::string& message) { log(ERROR, message); }
00139 
00140   private:
00142     std::string _name;
00143 
00145     int _level;
00146 
00147   protected:
00149     void log(int level, const std::string& message);
00150 
00152     std::string formatMessage(int level, const std::string& message);
00153 
00154   public:
00155 
00158     std::ostream* const _nostream;
00159 
00161     friend std::ostream& operator<<(Log& log, int level);
00162 
00163   };
00164 
00166   std::ostream& operator<<(Log& log, int level);
00167 
00168 }
00169 
00170 
00171 // Neat CPU-conserving logging macros. Use by preference!
00172 // NB. Only usable in classes where a getLog() method is provided
00173 #define MSG_LVL(lvl, x) \
00174   do { \
00175     if (getLog().isActive(lvl)) { \
00176       getLog() << lvl << x << endl;   \
00177     } \
00178   } while (0)
00179 
00180 #define MSG_TRACE(x)   MSG_LVL(Log::TRACE, x)
00181 #define MSG_DEBUG(x)   MSG_LVL(Log::DEBUG, x)
00182 #define MSG_INFO(x)    MSG_LVL(Log::INFO, x)
00183 #define MSG_WARNING(x) MSG_LVL(Log::WARNING, x)
00184 #define MSG_ERROR(x)   MSG_LVL(Log::ERROR, x)
00185 
00186 
00187 #endif