00001
00002
00011 #ifndef _DEBUG_FUNCTIONS_
00012 #define _DEBUG_FUNCTIONS_
00013
00014 #include <iosfwd>
00015 #include <sstream>
00016
00017 class Log {
00018 public:
00019
00020 typedef Log& (*manip_t)(Log&);
00021 typedef std::ostream& (*std_manip_t)(std::ostream&);
00022
00023
00024 enum Level {
00025
00026
00027 OFF = 0,
00028 DEBUG,
00029 INFO,
00030 WARN,
00031 ERROR,
00032 FATAL
00033 };
00034
00036 Log();
00037
00039 Log(const char* logger);
00040
00041
00042 ~Log();
00043
00044
00045 void setLevel(Level l) { m_level = l; }
00046
00048 void writeLog();
00049
00051 Log& operator()(Level l) { setLevel(l); return *this; }
00052
00059 template<typename T> Log& operator<<(const T &rhs)
00060 {
00061 if(enabled())
00062 m_oss << rhs;
00063
00064 return *this;
00065 }
00066
00067
00068 Log& operator<<(manip_t manip) { return manip(*this); }
00069
00070
00071
00072
00073 Log& operator<<(std_manip_t manip)
00074 {
00075 std_manip_t endlPtr = std::endl<std::ostream::char_type,
00076 std::ostream::traits_type>;
00077
00078 if(manip == endlPtr) {
00079 writeLog();
00080 }
00081
00082 return *this;
00083 }
00084
00086 static Log& write(Log& l) { l.writeLog(); return l; }
00087
00088 private:
00089 bool enabled();
00090
00091 void* m_logger;
00092 Level m_level;
00093 std::ostringstream m_oss;
00094 };
00095
00096 #ifdef NDEBUG
00097 struct DummyLog {
00098 typedef std::ostream& (*std_manip_t)(std::ostream&);
00099 typedef Log& (*manip_t)(Log&);
00100
00101 template<typename T> DummyLog& operator<<(const T &rhs)
00102 { return *this; }
00103
00104 DummyLog& operator<<(std_manip_t manip)
00105 { return *this; }
00106
00107 DummyLog& operator<<(manip_t manip)
00108 { return *this; }
00109 };
00110 #define LOG_DEBUG(l) if(false) DummyLog()
00111 #else
00112 #define LOG_DEBUG(l) l(Log::DEBUG)
00113 #endif
00114
00115 #define LOG_INFO(l) l(Log::INFO)
00116 #define LOG_WARN(l) l(Log::WARN)
00117 #define LOG_ERROR(l) l(Log::ERROR)
00118 #define LOG_FATAL(l) l(Log::FATAL)
00119 #define LOG_WRITE Log::write
00120 #define UNIT_LOGGER g_untlgr
00121 #define SET_UNIT_LOGGER(lnam) namespace { Log UNIT_LOGGER(#lnam); }
00122
00123 #define LOG LOG_INFO(UNIT_LOGGER)
00124 #define DEBUG(l) LOG_DEBUG(UNIT_LOGGER)
00125
00126 void InitLogging(const char* configfile=0);
00127
00128 #endif // _DEBUG_FUNCTIONS_