text: #ifndef KLFDEBUG_H
  #define KLFDEBUG_H
  
  #include <QDebug>
  #include <qmap.h>
  
  #include "klfdefs.h"
  
  namespace latex_lib_impl {
  
  	QString klfTimeOfDay(bool shortFmt = true);
  	QByteArray klfShortFuncSignature(const QByteArray& fullFuncName);
  	inline QByteArray klfShortFuncSignature(const char *fullFuncName) { return klfShortFuncSignature(QByteArray(fullFuncName)); }
  
  #define KLF_SHORT_TIME qPrintable(klfTimeOfDay())
  
  	class KLFDebugBlock {
  
  	public:
  		KLFDebugBlock(const QString& blockName);
  		KLFDebugBlock(bool printmsg, const QString& blockName);
  
  		virtual ~KLFDebugBlock();
  
  	protected:
  		QString pBlockName;
  
  	private:
  		bool pPrintMsg;
  	};
  
  	class KLFDebugBlockTimer : public KLFDebugBlock {
  
  	public:
  		KLFDebugBlockTimer(const QString& blockName);
  		virtual ~KLFDebugBlockTimer();
  	};
  
  	class KLFDebugObjectWatcherPrivate;
  
  	class KLFDebugObjectWatcher : public QObject {
  		Q_OBJECT
  
  	public:
  		static KLFDebugObjectWatcher *getWatcher();
  
  		void registerObjectRefInfo(QObject *object, const QString& refInfo);
  	public slots:
  		void debugObjectDestroyedFromSender() { debugObjectDestroyed(const_cast<QObject*>(sender())); }
  		void debugObjectDestroyed(QObject *object);
  
  	private:
  		KLFDebugObjectWatcher();
  		virtual ~KLFDebugObjectWatcher();
  		static KLFDebugObjectWatcher *instance;
  
  		KLFDebugObjectWatcherPrivate *p;
  	};
  
  #ifdef KLF_DEBUG
  
  	template<class T>
  	inline const T& __klf_debug_tee(const T& expr) { qDebug() << "TEE VALUE: " << expr; return expr; }
  
  	QDebug
  		__klf_dbg_hdr(QDebug dbg, const char * funcname, const char *refinstance, const char * shorttime);
  
  	inline QString __klf_debug_ref_instance() { return QString(); }
  #define KLF_DEBUG_DECLARE_REF_INSTANCE( expr )			\
    protected: inline QString __klf_debug_ref_instance() const { return QString("[")+ (expr) + "]" ; }
  
  #define KLF_DEBUG_DECLARE_ASSIGNABLE_REF_INSTANCE()			\
    public: QString __klf_debug_this_ref_instance;			\
    protected: inline QString __klf_debug_ref_instance() const { return __klf_debug_this_ref_instance; }
  #define KLF_DEBUG_ASSIGN_REF_INSTANCE(object, ref_instance)	\
    (object)->__klf_debug_this_ref_instance = QString("[%1]").arg((ref_instance))
  #define KLF_DEBUG_ASSIGN_SAME_REF_INSTANCE(object)			\
    (object)->__klf_debug_this_ref_instance = __klf_debug_ref_instance();
  
  #define KLF_DEBUG_TIME_BLOCK(msg) KLFDebugBlockTimer __klf_debug_timer_block(QString("")+msg)
  #define KLF_DEBUG_BLOCK(msg) KLFDebugBlock __klf_debug_block(QString("")+msg)
  #define KLF_DEBUG_TEE(expr) __klf_debug_tee(expr)
  #define klfDbg( streamableItems )				\
    __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, qPrintable(__klf_debug_ref_instance()), NULL) << streamableItems
  #define klfDbgT( streamableItems )					\
    __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, qPrintable(__klf_debug_ref_instance()), KLF_SHORT_TIME) << streamableItems
  #define klfDbgSt( streamableItems )				\
    __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, NULL, NULL) << streamableItems
  #define klfDbgStT( streamableItems )					\
    __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, NULL, KLF_SHORT_TIME) << streamableItems
  
  
  #define KLF_DEBUG_WATCH_OBJECT( qobj )				\
    { KLFDebugObjectWatcher::getWatcher()->registerObjectRefInfo((qobj), #qobj) ; \
      connect((qobj), SIGNAL(destroyed()),				\
  	    KLFDebugObjectWatcher::getWatcher(), SLOT(debugObjectDestroyedFromSender())); \
    }
  
  
  #else
  
  #define KLF_DEBUG_DECLARE_REF_INSTANCE( expr )
  #define KLF_DEBUG_DECLARE_ASSIGNABLE_REF_INSTANCE()
  #define KLF_DEBUG_ASSIGN_REF_INSTANCE(object, ref_instance)
  #define KLF_DEBUG_ASSIGN_SAME_REF_INSTANCE(object)
  
  
  #define KLF_DEBUG_TIME_BLOCK(msg)
  #define KLF_DEBUG_BLOCK(msg)
  
  #define KLF_DEBUG_TEE(expr) (expr)
  
  #define klfDbg( streamableItems )
  #define klfDbgT( streamableItems )
  #define klfDbgSt( streamableItems )
  #define klfDbgStT( streamableItems )
  
  #define KLF_DEBUG_WATCH_OBJECT( qobj )
  
  #endif
  
  
  #if defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L
  #  if defined(__GNUC__) && __GNUC__ >= 2
  #    define __func__ __FUNCTION__
  #  else
  #    ifdef KLFBACKEND_QT4
  #      define __func__ (qPrintable(QString("<in %2 line %1>").arg(__LINE__).arg(__FILE__)))
  #    else
  #      define __func__ (QString("<in %2 line %1>").arg(__LINE__).arg(__FILE__).ascii().data())
  #    endif
  #  endif
  #endif
  
  #ifndef KLF_FUNC_NAME
  #  if (defined(Q_CC_GNU) && !defined(Q_OS_SOLARIS)) || defined(Q_CC_HPACC) || defined(Q_CC_DIAB)
  #    define KLF_FUNC_NAME  (klfShortFuncSignature(__PRETTY_FUNCTION__).data())
  #  elif defined(_MSC_VER)
  #    if _MSC_VER <= 1300
  #        define KLF_FUNC_NAME  __func__
  #    else
  #        define KLF_FUNC_NAME  (klfShortFuncSignature(__FUNCSIG__).data())
  #    endif
  #  else
  #    define KLF_FUNC_NAME __func__
  #  endif
  #endif
  
  #define KLF_ASSERT_CONDITION(expr, msg, failaction)		       \
    if ( !(expr) ) {						       \
      qWarning().nospace()<<"In function "<<KLF_FUNC_NAME<<":\n\t"<<msg; \
      failaction;							       \
    }
  
  #define KLF_ASSERT_CONDITION_ELSE(expr, msg, failaction)	\
    KLF_ASSERT_CONDITION(expr, msg, failaction)			\
    else
  #define KLF_ASSERT_NOT_NULL(ptr, msg, failaction)	\
    KLF_ASSERT_CONDITION((ptr) != NULL, msg, failaction)
  
  #if defined(KLFBACKEND_QT4) && defined(QT_NO_DEBUG_OUTPUT)
  	inline QDebug& operator<<(QDebug& str, const QVariant& v) { return str; }
  #endif
  
  }
  
  #endif
  