[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project

src/klfbackend/klfdebug.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfdebug.h
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2010 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id: klfdebug.h 504 2010-09-21 10:50:49Z philippe $ */
00023 
00024 #ifndef KLFDEBUG_H
00025 #define KLFDEBUG_H
00026 
00027 #include <qmap.h>
00028 
00029 #include <klfdefs.h>
00030 
00031 // Note: function definitions are in klfdefs.cpp
00032 
00033 
00034 // DEBUG UTILITIES (SOME WITH TIME PRINTING FOR TIMING OPERATIONS)
00035 
00036 
00037 KLF_EXPORT QByteArray klfShortFuncSignature(const QByteArray& fullFuncName);
00038 inline QByteArray klfShortFuncSignature(const char *fullFuncName)
00039 #ifdef KLFBACKEND_QT4
00040 { return klfShortFuncSignature(QByteArray(fullFuncName)); }
00041 #else
00042 { return klfShortFuncSignature(QByteArray().duplicate(fullFuncName, strlen(fullFuncName))); }
00043 #endif
00044 
00045 KLF_EXPORT QString klfTimeOfDay(bool shortFmt = true);
00046 
00047 #ifdef KLFBACKEND_QT4
00048 #  define KLF_SHORT_TIME qPrintable(klfTimeOfDay())
00049 #else
00050 #  define KLF_SHORT_TIME (klfTimeOfDay().ascii())
00051 #endif
00052 
00053 
00054 class KLF_EXPORT KLFDebugBlock
00055 {
00056 public:
00057   KLFDebugBlock(const QString& blockName);
00058   KLFDebugBlock(bool printmsg, const QString& blockName);
00059 
00060   virtual ~KLFDebugBlock();
00061 
00062 protected:
00063   QString pBlockName;
00064 private:
00065   bool pPrintMsg;
00066 };
00067 
00068 class KLF_EXPORT KLFDebugBlockTimer : public KLFDebugBlock
00069 {
00070 public:
00071   KLFDebugBlockTimer(const QString& blockName);
00072   virtual ~KLFDebugBlockTimer();
00073 };
00074 
00075 #ifndef KLFBACKEND_QT4
00076 class KLF_EXPORT __klf_dbg_string_obj  {
00077   QString hdr;
00078 public:
00079   __klf_dbg_string_obj(const QString& h) : hdr(h) { }
00080   __klf_dbg_string_obj(const __klf_dbg_string_obj& other) : hdr(other.hdr) { }
00081   int operator=(const QString& msg);
00082 };
00083 #endif
00084 
00085 class KLFDebugObjectWatcherPrivate;
00086 
00087 class KLF_EXPORT KLFDebugObjectWatcher : public QObject
00088 {
00089   Q_OBJECT
00090 public:
00091   static KLFDebugObjectWatcher *getWatcher();
00092 
00093   void registerObjectRefInfo(QObject *object, const QString& refInfo);
00094 public slots:
00095   void debugObjectDestroyedFromSender() { debugObjectDestroyed(const_cast<QObject*>(sender())); }
00096   void debugObjectDestroyed(QObject *object);
00097 private:
00098 
00099   KLFDebugObjectWatcher();
00100   virtual ~KLFDebugObjectWatcher();
00101   static KLFDebugObjectWatcher *instance;
00102 
00103   KLFDebugObjectWatcherPrivate *p;
00104 };
00105 
00106 
00107 
00108 #ifdef KLF_DEBUG
00109 
00110 #  ifdef KLFBACKEND_QT4
00111 #    include <QDebug>
00112 #  endif
00113 
00114 template<class T>
00115 inline const T& __klf_debug_tee(const T& expr)
00116 #  ifdef KLFBACKEND_QT4
00117 { qDebug()<<"TEE VALUE: "<<expr; return expr; }
00118 #  else
00119 { return expr; } // sorry, no  qDebug()<<(anything you want)  in Qt 3 ...
00120 #  endif
00121 
00122 
00123 #  ifdef KLFBACKEND_QT4
00124 KLF_EXPORT  QDebug
00125 /* */ __klf_dbg_hdr(QDebug dbg, const char * funcname, const char *refinstance, const char * shorttime);
00126 #  else
00127 KLF_EXPORT  __klf_dbg_string_obj
00128 /* */ __klf_dbg_hdr_qt3(const char *funcname, const char *refinstance, const char *shorttime) ;
00129 #  endif
00130 
00131 inline QString __klf_debug_ref_instance() { return QString(); }
00132 #  define KLF_DEBUG_DECLARE_REF_INSTANCE( expr )                        \
00133   protected: inline QString __klf_debug_ref_instance() const { return QString("[")+ (expr) + "]" ; }
00134 
00135 #  define KLF_DEBUG_DECLARE_ASSIGNABLE_REF_INSTANCE()                   \
00136   public: QString __klf_debug_this_ref_instance;                        \
00137   protected: inline QString __klf_debug_ref_instance() const { return __klf_debug_this_ref_instance; }
00138 #  define KLF_DEBUG_ASSIGN_REF_INSTANCE(object, ref_instance)   \
00139   (object)->__klf_debug_this_ref_instance = QString("[%1]").arg((ref_instance))
00140 #  define KLF_DEBUG_ASSIGN_SAME_REF_INSTANCE(object)                    \
00141   (object)->__klf_debug_this_ref_instance = __klf_debug_ref_instance();
00142 
00143 #  define KLF_DEBUG_TIME_BLOCK(msg) KLFDebugBlockTimer __klf_debug_timer_block(QString("")+msg)
00144 #  define KLF_DEBUG_BLOCK(msg) KLFDebugBlock __klf_debug_block(QString("")+msg)
00145 #  define KLF_DEBUG_TEE(expr) __klf_debug_tee(expr)
00146 #  ifdef KLFBACKEND_QT4
00147 #    define klfDbg( streamableItems )                           \
00148   __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, qPrintable(__klf_debug_ref_instance()), NULL) << streamableItems
00149 #    define klfDbgT( streamableItems )                                  \
00150   __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, qPrintable(__klf_debug_ref_instance()), KLF_SHORT_TIME) << streamableItems
00151 #    define klfDbgSt( streamableItems )                         \
00152   __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, NULL, NULL) << streamableItems
00153 #    define klfDbgStT( streamableItems )                                        \
00154   __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, NULL, KLF_SHORT_TIME) << streamableItems
00155 #  else
00156 #    define klfDbg( string )                                            \
00157   __klf_dbg_hdr_qt3(KLF_FUNC_NAME, __klf_debug_ref_instance().local8Bit(), NULL) = QString("") + (string)
00158 #    define klfDbgT( string )                                           \
00159   __klf_dbg_hdr_qt3(KLF_FUNC_NAME, __klf_debug_ref_instance().local8Bit(), KLF_SHORT_TIME) = QString("") + (string)
00160 #    define klfDbgSt( string )                                          \
00161   __klf_dbg_hdr_qt3(KLF_FUNC_NAME, NULL, NULL) = QString("") + (string)
00162 #    define klfDbgStT( string )                                         \
00163   __klf_dbg_hdr_qt3(KLF_FUNC_NAME, NULL, KLF_SHORT_TIME) = QString("") + (string)
00164 #  endif
00165 
00166 #  define KLF_DEBUG_WATCH_OBJECT( qobj )                                \
00167   { KLFDebugObjectWatcher::getWatcher()->registerObjectRefInfo((qobj), #qobj) ; \
00168     connect((qobj), SIGNAL(destroyed()),                                \
00169             KLFDebugObjectWatcher::getWatcher(), SLOT(debugObjectDestroyedFromSender())); \
00170   }
00171 
00172 
00173 #else // KLF_DEBUG
00174 
00175 
00176 
00177 #  define KLF_DEBUG_DECLARE_REF_INSTANCE( expr )
00178 #  define KLF_DEBUG_DECLARE_ASSIGNABLE_REF_INSTANCE()
00179 #  define KLF_DEBUG_ASSIGN_REF_INSTANCE(object, ref_instance)
00180 #  define KLF_DEBUG_ASSIGN_SAME_REF_INSTANCE(object)
00181 
00182 
00183 #  define KLF_DEBUG_TIME_BLOCK(msg)
00184 #  define KLF_DEBUG_BLOCK(msg)
00185 
00186 #  define KLF_DEBUG_TEE(expr) (expr)
00187 
00188 #  define klfDbg( streamableItems )
00189 #  define klfDbgT( streamableItems )
00190 #  define klfDbgSt( streamableItems )
00191 #  define klfDbgStT( streamableItems )
00192 
00193 #  define KLF_DEBUG_WATCH_OBJECT( qobj )
00194 
00195 #endif // KLF_DEBUG
00196 
00197 
00198 
00199 /* Ensure a usable __func__ symbol */
00200 #if defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L
00201 #  if defined(__GNUC__) && __GNUC__ >= 2
00202 #    define __func__ __FUNCTION__
00203 #  else
00204 #    ifdef KLFBACKEND_QT4
00205 #      define __func__ (qPrintable(QString("<in %2 line %1>").arg(__LINE__).arg(__FILE__)))
00206 #    else
00207 #      define __func__ (QString("<in %2 line %1>").arg(__LINE__).arg(__FILE__).ascii().data())
00208 #    endif
00209 #  endif
00210 #endif
00211 /* The following declaration tests are inspired from "qglobal.h" in Qt 4.6.2 source code */
00212 #ifndef KLF_FUNC_NAME
00213 #  if (defined(Q_CC_GNU) && !defined(Q_OS_SOLARIS)) || defined(Q_CC_HPACC) || defined(Q_CC_DIAB)
00214 #    define KLF_FUNC_NAME  (klfShortFuncSignature(__PRETTY_FUNCTION__).data())
00215 #  elif defined(_MSC_VER)
00216     /* MSVC 2002 doesn't have __FUNCSIG__ */
00217 #    if _MSC_VER <= 1300
00218 #        define KLF_FUNC_NAME  __func__
00219 #    else
00220 #        define KLF_FUNC_NAME  (klfShortFuncSignature(__FUNCSIG__).data())
00221 #    endif
00222 #  else
00223 #    define KLF_FUNC_NAME __func__
00224 #  endif
00225 #endif
00226 
00227 
00228 
00229 
00230 #ifdef KLFBACKEND_QT4
00231 #define KLF_ASSERT_CONDITION(expr, msg, failaction)                    \
00232   if ( !(expr) ) {                                                     \
00233     qWarning().nospace()<<"In function "<<KLF_FUNC_NAME<<":\n\t"<<msg; \
00234     failaction;                                                        \
00235   }
00236 #else
00237 #define KLF_ASSERT_CONDITION(expr, msg, failaction)                     \
00238   if ( !(expr) ) {                                                      \
00239     qWarning("In function %s:\n\t%s", (QString("")+msg).local8Bit().data()); \
00240     failaction;                                                         \
00241   }
00242 #endif
00243 #define KLF_ASSERT_NOT_NULL(ptr, msg, failaction)       \
00244   KLF_ASSERT_CONDITION((ptr) != NULL, msg, failaction)
00245 
00246 
00247 
00248 
00249 #if defined(KLFBACKEND_QT4) && defined(QT_NO_DEBUG_OUTPUT)
00250 // Qt fix: this line is needed in non-debug output mode (?)
00251 inline QDebug& operator<<(QDebug& str, const QVariant& v) { return str; }
00252 #endif
00253 
00254 
00255 
00256 
00257 
00258 #endif

Generated by doxygen 1.7.3