[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
klfdebug.h
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfdebug.h
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfdebug.h 862 2013-11-23 11:10:54Z phfaist $ */
23 
24 #ifndef KLFDEBUG_H
25 #define KLFDEBUG_H
26 
27 #include <qmap.h>
28 
29 #include <klfdefs.h>
30 
31 // Note: function definitions are in klfdefs.cpp
32 
33 
34 // DEBUG UTILITIES (SOME WITH TIME PRINTING FOR TIMING OPERATIONS)
35 
36 
37 KLF_EXPORT QByteArray klfShortFuncSignature(const QByteArray& fullFuncName);
38 inline QByteArray klfShortFuncSignature(const char *fullFuncName)
39 #ifdef KLFBACKEND_QT4
40 { return klfShortFuncSignature(QByteArray(fullFuncName)); }
41 #else
42 { return klfShortFuncSignature(QByteArray().duplicate(fullFuncName, strlen(fullFuncName))); }
43 #endif
44 
45 KLF_EXPORT QString klfTimeOfDay(bool shortFmt = true);
46 
47 #ifdef KLFBACKEND_QT4
48 # define KLF_SHORT_TIME qPrintable(klfTimeOfDay())
49 #else
50 # define KLF_SHORT_TIME (klfTimeOfDay().ascii())
51 #endif
52 
53 
54 class KLF_EXPORT KLFDebugBlock
55 {
56 public:
57  KLFDebugBlock(const QString& blockName);
58  KLFDebugBlock(bool printmsg, const QString& blockName);
59 
60  virtual ~KLFDebugBlock();
61 
62 protected:
63  QString pBlockName;
64 private:
65  bool pPrintMsg;
66 };
67 
68 class KLF_EXPORT KLFDebugBlockTimer : public KLFDebugBlock
69 {
70 public:
71  KLFDebugBlockTimer(const QString& blockName);
72  virtual ~KLFDebugBlockTimer();
73 };
74 
75 #ifndef KLFBACKEND_QT4
76 class KLF_EXPORT __klf_dbg_string_obj {
77  QString hdr;
78 public:
79  __klf_dbg_string_obj(const QString& h) : hdr(h) { }
80  __klf_dbg_string_obj(const __klf_dbg_string_obj& other) : hdr(other.hdr) { }
81  int operator=(const QString& msg);
82 };
83 #endif
84 
85 class KLFDebugObjectWatcherPrivate;
86 
87 class KLF_EXPORT KLFDebugObjectWatcher : public QObject
88 {
89  Q_OBJECT
90 public:
91  static KLFDebugObjectWatcher *getWatcher();
92 
93  void registerObjectRefInfo(QObject *object, const QString& refInfo);
94 public slots:
95  void debugObjectDestroyedFromSender() { debugObjectDestroyed(const_cast<QObject*>(sender())); }
96  void debugObjectDestroyed(QObject *object);
97 private:
98 
99  KLFDebugObjectWatcher();
100  virtual ~KLFDebugObjectWatcher();
101  static KLFDebugObjectWatcher *instance;
102 
103  KLFDebugObjectWatcherPrivate *p;
104 };
105 
106 
107 #ifdef KLFBACKEND_QT4
108 # include <QDebug>
109 #endif
110 
111 #ifdef KLF_DEBUG
112 
113 template<class T>
114 inline const T& __klf_debug_tee(const T& expr)
115 # ifdef KLFBACKEND_QT4
116 { qDebug()<<"TEE VALUE: "<<expr; return expr; }
117 # else
118 { return expr; } // sorry, no qDebug()<<(anything you want) in Qt 3 ...
119 # endif
120 
121 
122 # ifdef KLFBACKEND_QT4
123 KLF_EXPORT QDebug
124 /* */ __klf_dbg_hdr(QDebug dbg, const char * funcname, const char *refinstance, const char * shorttime);
125 # else
126 KLF_EXPORT __klf_dbg_string_obj
127 /* */ __klf_dbg_hdr_qt3(const char *funcname, const char *refinstance, const char *shorttime) ;
128 # endif
129 
130 inline QString __klf_debug_ref_instance() { return QString(); }
131 # define KLF_DEBUG_DECLARE_REF_INSTANCE( expr ) \
132  protected: inline QString __klf_debug_ref_instance() const { return QString("[")+ (expr) + "]" ; }
133 
134 # define KLF_DEBUG_DECLARE_ASSIGNABLE_REF_INSTANCE() \
135  public: QString __klf_debug_this_ref_instance; \
136  protected: inline QString __klf_debug_ref_instance() const { return __klf_debug_this_ref_instance; }
137 # define KLF_DEBUG_ASSIGN_REF_INSTANCE(object, ref_instance) \
138  (object)->__klf_debug_this_ref_instance = QString("[%1]").arg((ref_instance))
139 # define KLF_DEBUG_ASSIGN_SAME_REF_INSTANCE(object) \
140  (object)->__klf_debug_this_ref_instance = __klf_debug_ref_instance();
141 
142 # define KLF_DEBUG_TIME_BLOCK(msg) KLFDebugBlockTimer __klf_debug_timer_block(QString("")+msg)
143 # define KLF_DEBUG_BLOCK(msg) KLFDebugBlock __klf_debug_block(QString("")+msg)
144 # define KLF_DEBUG_TEE(expr) __klf_debug_tee(expr)
145 # ifdef KLFBACKEND_QT4
146 # define klfDbg( streamableItems ) \
147  __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, qPrintable(__klf_debug_ref_instance()), NULL) << streamableItems
148 # define klfDbgT( streamableItems ) \
149  __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, qPrintable(__klf_debug_ref_instance()), KLF_SHORT_TIME) << streamableItems
150 # define klfDbgSt( streamableItems ) \
151  __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, NULL, NULL) << streamableItems
152 # define klfDbgStT( streamableItems ) \
153  __klf_dbg_hdr(qDebug(), KLF_FUNC_NAME, NULL, KLF_SHORT_TIME) << streamableItems
154 # else
155 # define klfDbg( string ) \
156  __klf_dbg_hdr_qt3(KLF_FUNC_NAME, __klf_debug_ref_instance().local8Bit(), NULL) = QString("") + (string)
157 # define klfDbgT( string ) \
158  __klf_dbg_hdr_qt3(KLF_FUNC_NAME, __klf_debug_ref_instance().local8Bit(), KLF_SHORT_TIME) = QString("") + (string)
159 # define klfDbgSt( string ) \
160  __klf_dbg_hdr_qt3(KLF_FUNC_NAME, NULL, NULL) = QString("") + (string)
161 # define klfDbgStT( string ) \
162  __klf_dbg_hdr_qt3(KLF_FUNC_NAME, NULL, KLF_SHORT_TIME) = QString("") + (string)
163 # endif
164 
165 # define KLF_DEBUG_WATCH_OBJECT( qobj ) \
166  { KLFDebugObjectWatcher::getWatcher()->registerObjectRefInfo((qobj), #qobj) ; \
167  connect((qobj), SIGNAL(destroyed()), \
168  KLFDebugObjectWatcher::getWatcher(), SLOT(debugObjectDestroyedFromSender())); \
169  }
170 
171 
172 #else // KLF_DEBUG
173 
174 
175 
176 # define KLF_DEBUG_DECLARE_REF_INSTANCE( expr )
177 # define KLF_DEBUG_DECLARE_ASSIGNABLE_REF_INSTANCE()
178 # define KLF_DEBUG_ASSIGN_REF_INSTANCE(object, ref_instance)
179 # define KLF_DEBUG_ASSIGN_SAME_REF_INSTANCE(object)
180 
181 
182 # define KLF_DEBUG_TIME_BLOCK(msg)
183 # define KLF_DEBUG_BLOCK(msg)
184 
185 # define KLF_DEBUG_TEE(expr) (expr)
186 
187 # define klfDbg( streamableItems )
188 # define klfDbgT( streamableItems )
189 # define klfDbgSt( streamableItems )
190 # define klfDbgStT( streamableItems )
191 
192 # define KLF_DEBUG_WATCH_OBJECT( qobj )
193 
194 #endif // KLF_DEBUG
195 
196 
197 
198 /* Ensure a usable __func__ symbol */
199 #if defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L
200 # if defined(__GNUC__) && __GNUC__ >= 2
201 # define __func__ __FUNCTION__
202 # else
203 # ifdef KLFBACKEND_QT4
204 # define __func__ (qPrintable(QString("<in %2 line %1>").arg(__LINE__).arg(__FILE__)))
205 # else
206 # define __func__ (QString("<in %2 line %1>").arg(__LINE__).arg(__FILE__).ascii().data())
207 # endif
208 # endif
209 #endif
210 /* The following declaration tests are inspired from "qglobal.h" in Qt 4.6.2 source code */
211 #ifndef KLF_FUNC_NAME
212 # if (defined(Q_CC_GNU) && !defined(Q_OS_SOLARIS)) || defined(Q_CC_HPACC) || defined(Q_CC_DIAB)
213 # define KLF_FUNC_NAME (klfShortFuncSignature(__PRETTY_FUNCTION__).data())
214 # elif defined(_MSC_VER)
215  /* MSVC 2002 doesn't have __FUNCSIG__ */
216 # if _MSC_VER <= 1300
217 # define KLF_FUNC_NAME __func__
218 # else
219 # define KLF_FUNC_NAME (klfShortFuncSignature(__FUNCSIG__).data())
220 # endif
221 # else
222 # define KLF_FUNC_NAME __func__
223 # endif
224 #endif
225 
226 
227 
228 
229 #ifdef KLFBACKEND_QT4
230 #define KLF_ASSERT_CONDITION(expr, msg, failaction) \
231  if ( !(expr) ) { \
232  qWarning().nospace()<<"In function "<<KLF_FUNC_NAME<<":\n\t"<<msg; \
233  failaction; \
234  }
235 #else
236 #define KLF_ASSERT_CONDITION(expr, msg, failaction) \
237  if ( !(expr) ) { \
238  qWarning("In function %s:\n\t%s", KLF_FUNC_NAME, (QString("")+msg).local8Bit().data()); \
239  failaction; \
240  }
241 #endif
242 #define KLF_ASSERT_CONDITION_ELSE(expr, msg, failaction) \
243  KLF_ASSERT_CONDITION(expr, msg, failaction) \
244  else
245 #define KLF_ASSERT_NOT_NULL(ptr, msg, failaction) \
246  KLF_ASSERT_CONDITION((ptr) != NULL, msg, failaction)
247 
248 
249 
250 
251 #if defined(KLFBACKEND_QT4) && defined(QT_NO_DEBUG_OUTPUT)
252 // Qt fix: this line is needed in non-debug output mode (?)
253 inline QDebug& operator<<(QDebug& str, const QVariant& v) { return str; }
254 #endif
255 
256 
257 
258 
259 
260 #endif
Base declarations for klatexformula and some utilities.
Utility to time the execution of a block.
Definition: klfdebug.h:54
KLF_EXPORT QString klfTimeOfDay(bool shortFmt=true)
Definition: klfdefs.cpp:909
QByteArray klfShortFuncSignature(const char *fullFuncName)
Definition: klfdebug.h:38
An extension of KLFDebugBlock with millisecond-time display.
Definition: klfdebug.h:68

Generated by doxygen 1.8.8