Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef _MBILOG_H
00016 #define _MBILOG_H
00017
00018 #include <string>
00019 #include <iostream>
00020 #include <sstream>
00021
00022 #include <mbilogConfig.h>
00023
00024 #ifndef MBILOG_MODULENAME
00025 #if defined(_CMAKE_MODULENAME)
00026 #define MBILOG_MODULENAME _CMAKE_MODULENAME
00027 #else
00028 #define MBILOG_MODULENAME "n/a"
00029 #endif
00030 #endif
00031
00032 #if defined(_WIN32)
00033 #ifdef mbilog_EXPORTS
00034 #define MBILOG_DLL_API __declspec(dllexport)
00035 #else
00036 #define MBILOG_DLL_API __declspec(dllimport)
00037 #endif
00038 #else
00039 #define MBILOG_DLL_API
00040 #endif
00041
00042 namespace mbilog {
00043
00044 enum {
00045 Info,
00046 Warn,
00047 Error,
00048 Fatal,
00049 Debug
00050 };
00051
00052 class MBILOG_DLL_API LogMessage {
00053
00054 public:
00055
00056 const int level;
00057 const char* filePath;
00058 const int lineNumber;
00059 const char* functionName;
00060
00061 const char* moduleName;
00062 std::string category;
00063 std::string message;
00064
00065 LogMessage(
00066 const int _level,
00067 const char* _filePath,
00068 const int _lineNumber,
00069 const char* _functionName
00070 )
00071 : level(_level)
00072 , filePath(_filePath)
00073 , lineNumber(_lineNumber)
00074 , functionName(_functionName)
00075 {
00076 }
00077 };
00078
00079 struct MBILOG_DLL_API AbstractBackend
00080 {
00081 virtual ~AbstractBackend(){}
00082 virtual void ProcessMessage(const mbilog::LogMessage& )=0;
00083 };
00084
00085 class MBILOG_DLL_API BackendCout : public AbstractBackend
00086 {
00087 public:
00088
00089 BackendCout();
00090 ~BackendCout();
00091 virtual void ProcessMessage(const mbilog::LogMessage &l );
00092
00093 void SetFull(bool full);
00094
00095 static void FormatSmart(const LogMessage &l,int threadID=0);
00096 static void FormatFull(const LogMessage &l,int threadID=0);
00097
00098 static void FormatSmart(std::ostream &out, const LogMessage &l,int threadID=0);
00099 static void FormatFull(std::ostream &out, const LogMessage &l,int threadID=0);
00100
00101 private:
00102
00103 static void AppendTimeStamp(std::ostream& out);
00104 static void FormatSmartWindows(const mbilog::LogMessage &l,int );
00105
00106 bool useFullOutput;
00107
00108 };
00109
00110 void MBILOG_DLL_API RegisterBackend(AbstractBackend* backend);
00111 void MBILOG_DLL_API UnregisterBackend(AbstractBackend* backend);
00112 void MBILOG_DLL_API DistributeToBackends(LogMessage &l);
00113
00114 class MBILOG_DLL_API PseudoStream {
00115
00116 protected:
00117
00118 bool disabled;
00119 LogMessage msg;
00120 std::stringstream ss;
00121
00122 public:
00123
00124 inline PseudoStream( int level,
00125 const char* filePath,
00126 int lineNumber,
00127 const char* functionName)
00128 : disabled(false)
00129 , msg(LogMessage(level,filePath,lineNumber,functionName))
00130 , ss(std::stringstream::out)
00131 {
00132 }
00133
00134 inline ~PseudoStream()
00135 {
00136 if(!disabled)
00137 {
00138 msg.message = ss.str();
00139 msg.moduleName = MBILOG_MODULENAME;
00140 DistributeToBackends(msg);
00141 }
00142 }
00143
00144 template <class T> inline PseudoStream& operator<<(const T& data)
00145 {
00146 if(!disabled)
00147 ss << data;
00148 return *this;
00149 }
00150
00151 template <class T> inline PseudoStream& operator<<(T& data)
00152 {
00153 if(!disabled)
00154 ss << data;
00155 return *this;
00156 }
00157
00158 inline PseudoStream& operator<<(std::ostream& (*func)(std::ostream&))
00159 {
00160 if(!disabled)
00161 ss << func;
00162 return *this;
00163 }
00164
00165 inline PseudoStream& operator()(const char *category)
00166 {
00167 if(!disabled)
00168 {
00169 if(msg.category.length())
00170 msg.category+=".";
00171 msg.category+=category;
00172 }
00173 return *this;
00174 }
00175
00176 inline PseudoStream& operator()(bool enabled)
00177 {
00178 disabled|=!enabled;
00179 return *this;
00180 }
00181 };
00182
00183 class MBILOG_DLL_API NullStream {
00184
00185 public:
00186
00187 template <class T> inline NullStream& operator<<(const T& )
00188 {
00189 return *this;
00190 }
00191 template <class T> inline NullStream& operator<<(T& )
00192 {
00193 return *this;
00194 }
00195 inline NullStream& operator<<(std::ostream& (*)(std::ostream&))
00196 {
00197 return *this;
00198 }
00199 inline NullStream& operator()(const char *)
00200 {
00201 return *this;
00202 }
00203 inline NullStream& operator()(bool)
00204 {
00205 return *this;
00206 }
00207 };
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 }
00233
00234
00235 #define MBI_INFO mbilog::PseudoStream(mbilog::Info,__FILE__,__LINE__,__FUNCTION__)
00236 #define MBI_WARN mbilog::PseudoStream(mbilog::Warn,__FILE__,__LINE__,__FUNCTION__)
00237 #define MBI_ERROR mbilog::PseudoStream(mbilog::Error,__FILE__,__LINE__,__FUNCTION__)
00238 #define MBI_FATAL mbilog::PseudoStream(mbilog::Fatal,__FILE__,__LINE__,__FUNCTION__)
00239
00240 #ifdef MBILOG_ENABLE_DEBUG
00241 #define MBI_DEBUG mbilog::PseudoStream(mbilog::Debug,__FILE__,__LINE__,__FUNCTION__)
00242 #else
00243 #define MBI_DEBUG true ? mbilog::NullStream() : mbilog::NullStream()
00244 #endif
00245
00246
00247 #endif