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
00012
00013 enum Level {
00014 TRACE = 0, DEBUG = 10, INFO = 20, WARN = 30, WARNING = 30, ERROR = 40
00015 };
00016
00017
00018 typedef std::map<std::string, Log*> LogMap;
00019
00020
00021 typedef std::map<std::string, int> LevelMap;
00022
00023
00024 typedef std::map<int, std::string> ColorCodes;
00025
00026 private:
00027
00028 static LogMap existingLogs;
00029
00030
00031 static LevelMap defaultLevels;
00032
00033
00034 static ColorCodes colorCodes;
00035
00036
00037 static std::string endColorCode;
00038
00039
00040 static bool showTimestamp;
00041
00042
00043 static bool showLogLevel;
00044
00045
00046 static bool showLoggerName;
00047
00048
00049 static bool useShellColors;
00050
00051 public:
00052
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
00074
00075
00076
00077
00078 Log(const std::string& name);
00079
00080
00081 Log(const std::string& name, int level);
00082
00083
00084
00085 static std::string getColorCode(int level);
00086
00087 public:
00088
00089
00090
00091 static Log& getLog(const std::string& name);
00092
00093 public:
00094
00095 int getLevel() const {
00096 return _level;
00097 }
00098
00099
00100 Log& setLevel(int level) {
00101 _level = level;
00102 return *this;
00103 }
00104
00105
00106 static Level getLevelFromName(const std::string& level);
00107
00108
00109 static std::string getLevelName(int level);
00110
00111
00112 std::string getName() const {
00113 return _name;
00114 }
00115
00116
00117 Log& setName(const std::string& name) {
00118 _name = name;
00119 return *this;
00120 }
00121
00122
00123 bool isActive(int level) const {
00124 return (level >= _level);
00125 }
00126
00127
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); }
00138
00139
00140 private:
00141
00142 std::string _name;
00143
00144
00145 int _level;
00146
00147 protected:
00148
00149 void log(int level, const std::string& message);
00150
00151
00152 std::string formatMessage(int level, const std::string& message);
00153
00154 public:
00155
00156
00157
00158 std::ostream* const _nostream;
00159
00160
00161 friend std::ostream& operator<<(Log& log, int level);
00162
00163 };
00164
00165
00166 std::ostream& operator<<(Log& log, int level);
00167
00168 }
00169
00170
00171 #endif