00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef LOGGING_H
00023 #define LOGGING_H
00024
00025 #include <QIODevice>
00026 #include <QString>
00027 #include <QStringList>
00028 #include <QTextStream>
00029 #include <QBuffer>
00030 #include <QDebug>
00031
00032 #define CONTEXT_LOG_MSG_TYPE_TEST 1
00033 #define CONTEXT_LOG_MSG_TYPE_DEBUG 2
00034 #define CONTEXT_LOG_MSG_TYPE_WARNING 3
00035 #define CONTEXT_LOG_MSG_TYPE_CRITICAL 4
00036
00037 #ifndef CONTEXT_LOG_MODULE_NAME
00038 #define CONTEXT_LOG_MODULE_NAME "unknown"
00039 #endif
00040
00041 class ContextFeature
00042 {
00043 public:
00044 ContextFeature(QString name);
00045 QString getName() const;
00046
00047 private:
00048 QString featureName;
00049 };
00050
00051 class ContextRealLogger : public QTextStream
00052 {
00053 public:
00054 ContextRealLogger(int msgType, const char *module, const char *func, const char *file, int line);
00055 ~ContextRealLogger();
00056
00057 static bool showTest;
00058 static bool showDebug;
00059 static bool showWarning;
00060 static bool showCritical;
00061 static bool initialized;
00062 static bool hideTimestamps;
00063 static bool useColor;
00064 static char *showModule;
00065 static char *hideModule;
00066 static bool vanilla;
00067
00068 static void initialize();
00069
00070 ContextRealLogger &operator<< (const ContextFeature&);
00071
00072
00073
00074 template <typename T> ContextRealLogger &operator<< (const T& qSomething)
00075 {
00076 QString out;
00077 QDebug(&out) << qSomething;
00078 QTextStream::operator<<(out);
00079 return *this;
00080 }
00081
00082 private:
00083
00084 bool shouldPrint();
00085 void appendFeatures();
00086
00087 int msgType;
00088 const char* moduleName;
00089 QString data;
00090 QStringList features;
00091 };
00092
00104 class ContextZeroLogger
00105 {
00106 public:
00108 inline ContextZeroLogger() {}
00109
00110
00111 template <typename T> inline ContextZeroLogger &operator<< (const T&) { return *this;}
00112 };
00113
00114
00115
00116 #define contextFeature(name) (ContextFeature(name))
00117
00118 #ifdef CONTEXT_LOG_HIDE_TEST
00119 #define contextTest() (ContextZeroLogger())
00120 #else
00121 #define contextTest() (ContextRealLogger(CONTEXT_LOG_MSG_TYPE_TEST, CONTEXT_LOG_MODULE_NAME, __PRETTY_FUNCTION__, __FILE__, __LINE__))
00122 #endif
00123
00124 #ifdef CONTEXT_LOG_HIDE_DEBUG
00125 #define contextDebug() (ContextZeroLogger())
00126 #else
00127 #define contextDebug() (ContextRealLogger(CONTEXT_LOG_MSG_TYPE_DEBUG, CONTEXT_LOG_MODULE_NAME, __PRETTY_FUNCTION__, __FILE__, __LINE__))
00128 #endif
00129
00130 #ifdef CONTEXT_LOG_HIDE_WARNING
00131 #define contextWarning() (ContextZeroLogger())
00132 #else
00133 #define contextWarning() (ContextRealLogger(CONTEXT_LOG_MSG_TYPE_WARNING, CONTEXT_LOG_MODULE_NAME, __PRETTY_FUNCTION__, __FILE__, __LINE__))
00134 #endif
00135
00136 #ifdef CONTEXT_LOG_HIDE_CRITICAL
00137 #define contextCritical() (ContextZeroLogger())
00138 #else
00139 #define contextCritical() (ContextRealLogger(CONTEXT_LOG_MSG_TYPE_CRITICAL, CONTEXT_LOG_MODULE_NAME, __PRETTY_FUNCTION__, __FILE__, __LINE__))
00140 #endif
00141
00142 #endif // LOGGING_H