log.h

00001 #ifndef GM_LOG_H 00002 #define GM_LOG_H 00003 00004 #include <iostream> 00005 00006 namespace GNUMessenger { 00007 using namespace std; 00008 00009 #define LOG(x) { Log << x << endl; } 00010 #define LOG_DEBUG(x) { Log.Debug() << x << " ["__FILE__"(" << __LINE__ << ")]" << endl; } 00011 00016 class LogBase 00017 { 00018 public: 00019 00021 LogBase(); 00022 00024 virtual LogBase& operator <<(ostream& ( *pf )(ostream&)){ 00025 pf(GetStream()); 00026 return *this; 00027 } 00028 00030 virtual LogBase& operator <<(const wchar_t * wstr){ 00031 GetStreamW() << wstr; 00032 return *this; 00033 } 00034 00036 virtual LogBase& operator <<(const char * str){ 00037 GetStream() << str; 00038 return *this; 00039 } 00040 00042 template <typename T> 00043 LogBase& operator << (const T str) { 00044 GetStream() << str; 00045 return *this; 00046 } 00047 00049 enum Mode { Debug, Error, Normal }; 00050 00052 const char * GetMode(); 00053 00055 virtual void SetMode(const Mode mode); 00056 00057 protected: 00058 00060 virtual void Break() { m_lastWasWide = true; } 00062 virtual void BreakW() { m_lastWasWide = false; } 00063 00065 virtual ostream& GetStream() = 0; 00067 virtual wostream& GetStreamW()= 0; 00068 00069 Mode m_mode; 00070 00071 private: 00073 bool m_lastWasWide; 00074 }; 00075 00077 class StdLog: public LogBase 00078 { 00079 public: 00080 00081 virtual void SetMode(const Mode mode) { 00082 LogBase::SetMode(mode); 00083 (*this) << GetMode(); 00084 } 00085 00086 protected: 00087 00088 virtual ostream& GetStream() { return cout; } 00089 virtual wostream& GetStreamW() { return wcout; } 00090 00091 }; 00092 00094 class LogNull: public LogBase 00095 { 00096 public: 00097 protected: 00098 00099 virtual ostream& GetStream() { return cout; } 00100 virtual wostream& GetStreamW() { return wcout; } 00101 }; 00102 00104 class LogManager { 00105 public: 00106 00108 LogManager(); 00109 00111 ~LogManager(); 00112 00116 void SetLog(LogBase * logger); 00117 00119 LogBase * GetLog() { return m_logger; } 00120 00122 LogBase& operator << (const char * str); 00123 00125 LogBase& operator << (const wchar_t* wstr); 00126 00128 LogBase& Debug(); 00129 00131 LogBase& Error(); 00132 00134 LogBase& Normal(); 00135 00136 private: 00137 LogBase * m_logger; 00138 00139 }; 00140 00142 static LogManager Log = LogManager(); 00143 00144 } 00145 00146 #endif

Generated on Tue Oct 5 14:41:47 2004 for GNU Messenger by doxygen 1.3.8