00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef KLFDEBUG_H
00025 #define KLFDEBUG_H
00026
00027 #include <QMap>
00028
00029 #include <klfdefs.h>
00030
00031
00032
00033
00034
00035
00036 KLF_EXPORT QByteArray klfShortFuncSignature(const QByteArray& fullFuncName);
00037 inline QByteArray klfShortFuncSignature(const char *fullFuncName)
00038 { return klfShortFuncSignature(QByteArray(fullFuncName)); }
00039
00040 KLF_EXPORT QString klfTimeOfDay(bool shortFmt = true);
00041
00042 #define KLF_SHORT_TIME qPrintable(klfTimeOfDay())
00043
00044
00045 class KLF_EXPORT KLFDebugBlock
00046 {
00047 public:
00048 KLFDebugBlock(const QString& blockName);
00049 KLFDebugBlock(bool printmsg, const QString& blockName);
00050
00051 virtual ~KLFDebugBlock();
00052
00053 protected:
00054 QString pBlockName;
00055 private:
00056 bool pPrintMsg;
00057 };
00058
00059 class KLF_EXPORT KLFDebugBlockTimer : public KLFDebugBlock
00060 {
00061 public:
00062 KLFDebugBlockTimer(const QString& blockName);
00063 virtual ~KLFDebugBlockTimer();
00064 };
00065
00066 class KLFDebugObjectWatcherPrivate;
00067
00068 class KLF_EXPORT KLFDebugObjectWatcher : public QObject
00069 {
00070 Q_OBJECT
00071 public:
00072 static KLFDebugObjectWatcher *getWatcher();
00073
00074 void registerObjectRefInfo(QObject *object, const QString& refInfo);
00075 public slots:
00076 void debugObjectDestroyedFromSender() { debugObjectDestroyed(const_cast<QObject*>(sender())); }
00077 void debugObjectDestroyed(QObject *object);
00078 private:
00079
00080 KLFDebugObjectWatcher();
00081 ~KLFDebugObjectWatcher();
00082 static KLFDebugObjectWatcher *instance;
00083
00084 KLFDebugObjectWatcherPrivate *p;
00085 };
00086
00087
00088
00089 #include <QDebug>
00090
00091
00092 #ifdef KLF_DEBUG
00093
00094
00095 KLF_EXPORT QDebug
00096 __klf_dbg_hdr(QDebug dbg, const char * funcname, const char *refinstance, const char * shorttime);
00097
00098 template<class T>
00099 inline const T& __klf_debug_tee(const T& expr)
00100 { qDebug()<<"TEE VALUE: "<<expr; return expr; }
00101
00102 template<class T>
00103 inline const T& __klf_debug_tee2(const T& expr, QDebug dbg, const char *where, const char *what)
00104
00105
00106 { __klf_dbg_hdr(dbg, where, NULL, NULL)<<"`"<<what<<"': "<<expr; return expr; }
00107
00108
00109 inline QString __klf_debug_ref_instance() { return QString(); }
00110 # define KLF_DEBUG_DECLARE_REF_INSTANCE( expr ) \
00111 protected: inline QString __klf_debug_ref_instance() const { return QString("[")+ (expr) + "]" ; }
00112
00113 # define KLF_DEBUG_DECLARE_ASSIGNABLE_REF_INSTANCE() \
00114 public: QString __klf_debug_this_ref_instance; \
00115 protected: inline QString __klf_debug_ref_instance() const { return __klf_debug_this_ref_instance; }
00116 # define KLF_DEBUG_ASSIGN_REF_INSTANCE(object, ref_instance) \
00117 (object)->__klf_debug_this_ref_instance = QString("[%1]").arg((ref_instance))
00118 # define KLF_DEBUG_ASSIGN_SAME_REF_INSTANCE(object) \
00119 (object)->__klf_debug_this_ref_instance = __klf_debug_ref_instance();
00120
00121 # define KLF_DEBUG_TIME_BLOCK(msg) KLFDebugBlockTimer __klf_debug_timer_block(QString("")+msg)
00122 # define KLF_DEBUG_BLOCK(msg) KLFDebugBlock __klf_debug_block(QString("")+msg)
00123 # define KLF_DEBUG_TEE(expr) \
00124 __klf_debug_tee2(expr, qDebug(), KLF_FUNC_NAME, #expr)
00125 # define klfDebugf( arglist ) qDebug arglist
00126 # define klfDbg( streamableItems ) \
00127 __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, qPrintable(__klf_debug_ref_instance()), NULL) << streamableItems
00128 # define klfDbgT( streamableItems ) \
00129 __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, qPrintable(__klf_debug_ref_instance()), KLF_SHORT_TIME) << streamableItems
00130 # define klfDbgSt( streamableItems ) \
00131 __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, NULL, NULL) << streamableItems
00132 # define klfDbgStT( streamableItems ) \
00133 __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, NULL, KLF_SHORT_TIME) << streamableItems
00134
00135 # define KLF_DEBUG_WATCH_OBJECT( qobj ) \
00136 { KLFDebugObjectWatcher::getWatcher()->registerObjectRefInfo((qobj), #qobj) ; \
00137 connect((qobj), SIGNAL(destroyed()), \
00138 KLFDebugObjectWatcher::getWatcher(), SLOT(debugObjectDestroyedFromSender())); \
00139 }
00140
00141
00142 #else // KLF_DEBUG
00143
00144
00145 # define KLF_DEBUG_DECLARE_REF_INSTANCE( expr )
00146 # define KLF_DEBUG_DECLARE_ASSIGNABLE_REF_INSTANCE()
00147 # define KLF_DEBUG_ASSIGN_REF_INSTANCE(object, ref_instance)
00148 # define KLF_DEBUG_ASSIGN_SAME_REF_INSTANCE(object)
00149
00150
00151 # define KLF_DEBUG_TIME_BLOCK(msg)
00152 # define KLF_DEBUG_BLOCK(msg)
00153
00154 # define KLF_DEBUG_TEE(expr) (expr)
00155
00156 # define klfDebugf(arglist)
00157
00158 # define klfDbg( streamableItems )
00159 # define klfDbgT( streamableItems )
00160 # define klfDbgSt( streamableItems )
00161 # define klfDbgStT( streamableItems )
00162
00163 # define KLF_DEBUG_WATCH_OBJECT( qobj )
00164
00165 #endif // KLF_DEBUG
00166
00167
00168 KLF_EXPORT QDebug
00169 __klf_warning_hdr(QDebug warnstr, const char * funcname, const char * shorttime);
00170
00171 #define klfWarning(streamableItems) __klf_warning_hdr(qWarning(), KLF_FUNC_NAME, KLF_SHORT_TIME) << streamableItems
00172
00173
00174
00175 #if defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L
00176 # if defined(__GNUC__) && __GNUC__ >= 2
00177 # define __func__ __FUNCTION__
00178 # else
00179 # define __func__ (qPrintable(QString("<in %2 line %1>").arg(__LINE__).arg(__FILE__)))
00180 # endif
00181 #endif
00182
00183 #ifndef KLF_FUNC_NAME
00184 # if (defined(Q_CC_GNU) && !defined(Q_OS_SOLARIS)) || defined(Q_CC_HPACC) || defined(Q_CC_DIAB)
00185 # define KLF_FUNC_NAME (klfShortFuncSignature(__PRETTY_FUNCTION__).data())
00186 # elif defined(_MSC_VER)
00187
00188 # if _MSC_VER <= 1300
00189 # define KLF_FUNC_NAME __func__
00190 # else
00191 # define KLF_FUNC_NAME (klfShortFuncSignature(__FUNCSIG__).data())
00192 # endif
00193 # else
00194 # define KLF_FUNC_NAME __func__
00195 # endif
00196 #endif
00197
00198
00199
00200
00201 #define KLF_ASSERT_CONDITION(expr, msg, failaction) \
00202 klfDbgSt("KLF_ASSERT_CONDITION(" #expr ")") ; \
00203 if ( !(expr) ) { \
00204 klfWarning(msg); \
00205 failaction; \
00206 }
00207 #define KLF_ASSERT_CONDITION_ELSE(expr, msg, failaction) \
00208 KLF_ASSERT_CONDITION(expr, msg, failaction) \
00209 else
00210 #define KLF_ASSERT_NOT_NULL(ptr, msg, failaction) \
00211 KLF_ASSERT_CONDITION((ptr) != NULL, msg, failaction)
00212
00213
00214
00215
00216 #endif