00001 #ifndef AGILE_LOGGING_HH 00002 #define AGILE_LOGGING_HH 00003 00004 #include "AGILe/AGILe.hh" 00005 00006 namespace AGILe { 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<const std::string, Log*> LogMap; 00019 00021 typedef std::map<const 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(LevelMap& logLevels); 00055 00056 static void setShowTimestamp(const bool showTime=true) { 00057 showTimestamp = showTime; 00058 } 00059 00060 static void setShowLevel(const bool showLevel=true) { 00061 showLogLevel = showLevel; 00062 } 00063 00064 static void setShowLoggerName(const bool showName=true) { 00065 showLoggerName = showName; 00066 } 00067 00068 static void setUseColors(const bool useColors=true) { 00069 useShellColors = useColors; 00070 } 00071 00072 protected: 00074 00075 00076 Log(const std::string& name); 00077 00079 Log(const std::string& name, int level); 00080 00082 //Log(const Log&); 00083 00085 //Log& operator=(const Log&); 00087 00088 static std::string getColorCode(int level); 00089 00090 public: 00093 static Log& getLog(const std::string& name); 00094 00095 public: 00097 int getLevel() const { 00098 return _level; 00099 } 00100 00102 Log& setLevel(int level) { 00103 _level = level; 00104 return *this; 00105 } 00106 00108 static Level getLevelFromName(const std::string& level); 00109 00111 static std::string getLevelName(int level); 00112 00114 std::string getName() const { 00115 return _name; 00116 } 00117 00119 Log& setName(const std::string& name) { 00120 _name = name; 00121 return *this; 00122 } 00123 00125 bool isActive(int level) const { 00126 return (level >= _level); 00127 } 00128 00130 00131 void trace(const std::string& message) { log(TRACE, message); } 00132 00133 void debug(const std::string& message) { log(DEBUG, message); } 00134 00135 void info(const std::string& message) { log(INFO, message); } 00136 00137 void warn(const std::string& message) { log(WARN, message); } 00138 00139 void error(const std::string& message) { log(ERROR, message); } 00140 00141 void always(const std::string& message) { log(ALWAYS, message); } 00143 00144 private: 00146 std::string _name; 00147 00149 int _level; 00150 00151 protected: 00153 void log(int level, const std::string& message); 00154 00156 std::string formatMessage(int level, const std::string& message); 00157 00158 public: 00159 00162 std::ostream* const _nostream; 00163 00165 friend std::ostream& operator<<(Log& log, int level); 00166 00167 }; 00168 00170 std::ostream& operator<<(Log& log, int level); 00171 00172 } 00173 00174 00175 #endif