00001
00002 #ifndef RIVET_LOGGING_HH
00003 #define RIVET_LOGGING_HH 1
00004
00005 #include "Rivet/Rivet.hh"
00006
00007
00008 namespace Rivet {
00009
00010 class Log {
00011 public:
00012
00013
00014 enum Level {
00015 TRACE = 0, DEBUG = 10, INFO = 20, WARN = 30, ERROR = 40
00016 };
00017
00018
00019 typedef map<const string, Log*> LogMap;
00020
00021
00022 typedef map<const string, Level> LevelMap;
00023
00024 private:
00025
00026 static LogMap existingLogs;
00027
00028
00029 static LevelMap defaultLevels;
00030
00031
00032 static bool showTimestamp;
00033
00034
00035 static bool showLogLevel;
00036
00037
00038 static bool showLoggerName;
00039
00040 public:
00041
00042 inline static void setDefaultLevels(const LevelMap& logLevels) {
00043 defaultLevels = logLevels;
00044 }
00045
00046 inline static void setShowTimestamp(const bool showTime=true) {
00047 showTimestamp = showTime;
00048 }
00049
00050 inline static void setShowLevel(const bool showLevel=true) {
00051 showLogLevel = showLevel;
00052 }
00053
00054 inline static void setShowLoggerName(const bool showName=true) {
00055 showLoggerName = showName;
00056 }
00057
00058
00059 protected:
00060
00061
00062
00063 Log(const string& name);
00064
00065
00066 Log(const string& name, const Level& level);
00067
00068
00069
00070
00071
00072
00073
00074
00075 public:
00076
00077
00078 static Log& getLog(const string& name);
00079
00080
00081 static Log& getLog(const string& name, const Level& level);
00082
00083 public:
00084
00085 inline Level getLevel() const {
00086 return _level;
00087 }
00088
00089
00090 inline Log& setLevel(const Level& level) {
00091 _level = level;
00092 return *this;
00093 }
00094
00095
00096 static Level getLevelFromName(const string& level);
00097
00098
00099 static string getLevelName(const Level& level);
00100
00101
00102 inline string getName() const {
00103 return _name;
00104 }
00105
00106
00107 inline Log& setName(const string& name) {
00108 _name = name;
00109 return *this;
00110 }
00111
00112
00113 inline bool isActive(const Level& level) const {
00114 return (level >= _level);
00115 }
00116
00117
00118
00119 inline void trace(const string& message) { log(TRACE, message); }
00120
00121 inline void debug(const string& message) { log(DEBUG, message); }
00122
00123 inline void info(const string& message) { log(INFO, message); }
00124
00125 inline void warn(const string& message) { log(WARN, message); }
00126
00127 inline void error(const string& message) { log(ERROR, message); }
00128
00129
00130 private:
00131
00132 string _name;
00133
00134
00135 Level _level;
00136
00137 protected:
00138
00139 void log(const Level& level, const string& message);
00140
00141
00142 string formatMessage(const Level& level, const string& message);
00143
00144 public:
00145
00146
00147
00148 ostream* const _nostream;
00149
00150
00151 friend ostream& operator<<(Log& log, const Log::Level& level);
00152
00153 };
00154
00155
00156 ostream& operator<<(Log& log, const Log::Level& level);
00157
00158 }
00159
00160
00161 #endif