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

Generated by doxygen 1.8.7