1 // Author: Max Howell <max.howell@methylblue.com>, (C) 2003-5
2 // Author: Mark Kretschmann <kretschmann@kde.org>, (C) 2007
3 // Copyright: See COPYING file that comes with this distribution
9 // We always want debug output available at runtime
10 #undef QT_NO_DEBUG_OUTPUT
11 #undef KDE_NO_DEBUG_OUTPUT
15 #include <QApplication>
22 #include "amarok_export.h"
25 #define __PRETTY_FUNCTION__ __FUNCTION__
30 * @short kdebug with indentation functionality and convenience macros
31 * @author Max Howell <max.howell@methylblue.com>
35 * #define DEBUG_PREFIX "Blah"
40 * Debug::Block myBlock( __PRETTY_FUNCTION__ );
42 * debug() << "output1" << endl;
43 * debug() << "output2" << endl;
48 * app: BEGIN: void function()
51 * app: END: void function(): Took 0.1s
60 extern AMAROK_EXPORT QMutex mutex
; // defined in app.cpp
62 // we can't use a statically instantiated QCString for the indent, because
63 // static namespaces are unique to each dlopened library. So we piggy back
64 // the QCString on the KApplication instance
66 #define qOApp reinterpret_cast<QObject*>(qApp)
67 class Indent
: QObject
69 friend QString
&modifieableIndent();
70 Indent() : QObject( qOApp
) { setObjectName( "DEBUG_indent" ); }
74 inline QString
&modifieableIndent()
76 QObject
* o
= qOApp
? qOApp
->findChild
<QObject
*>( "DEBUG_indent" ) : 0;
77 QString
&ret
= (o
? static_cast<Indent
*>( o
) : new Indent
)->m_string
;
81 inline QString
indent()
83 return modifieableIndent();
86 inline bool debugEnabled()
88 KConfigGroup config
= KGlobal::config()->group( "General" );
89 const bool debug
= config
.readEntry( "Debug Enabled", false );
94 inline kdbgstream
dbgstream()
96 return debugEnabled() ? kdbgstream( QtDebugMsg
) : kDebugDevNull();
102 #define AMK_PREFIX ""
104 #define AMK_PREFIX "[" DEBUG_PREFIX "]"
116 static inline kdbgstream
debug() { mutex
.lock(); QString ind
= indent(); mutex
.unlock(); return dbgstream() << qPrintable( ind
+ AMK_PREFIX
); }
117 static inline kdbgstream
warning() { mutex
.lock(); QString ind
= indent(); mutex
.unlock(); return dbgstream() << qPrintable( ind
+ AMK_PREFIX
+ " [WARNING!]" ); }
118 static inline kdbgstream
error() { mutex
.lock(); QString ind
= indent(); mutex
.unlock(); return dbgstream() << qPrintable( ind
+ AMK_PREFIX
+ " [ERROR!]" ); }
119 static inline kdbgstream
fatal() { mutex
.lock(); QString ind
= indent(); mutex
.unlock(); return dbgstream() << qPrintable( ind
+ AMK_PREFIX
); }
121 typedef kdbgstream DebugStream
;
125 typedef kndbgstream NoDebugStream
;
129 using Debug::warning
;
132 using Debug::DebugStream
;
134 /// Standard function announcer
135 #define DEBUG_FUNC_INFO { Debug::mutex.lock(); kDebug() << Debug::indent() ; Debug::mutex.unlock(); }
138 #define DEBUG_LINE_INFO { Debug::mutex.lock(); kDebug() << Debug::indent() << "Line: " << __LINE__; Debug::mutex.unlock(); }
140 /// Convenience macro for making a standard Debug::Block
141 #define DEBUG_BLOCK Debug::Block uniquelyNamedStackAllocatedStandardBlock( __PRETTY_FUNCTION__ );
143 /// Use this to remind yourself to finish the implementation of a function
144 #define AMAROK_NOTIMPLEMENTED warning() << "NOT-IMPLEMENTED: " << __PRETTY_FUNCTION__ << endl;
146 /// Use this to alert other developers to stop using a function
147 #define AMAROK_DEPRECATED warning() << "DEPRECATED: " << __PRETTY_FUNCTION__ << endl;
153 * @class Debug::Block
154 * @short Use this to label sections of your code
160 * Debug::Block myBlock( "section" );
162 * debug() << "output1" << endl;
163 * debug() << "output2" << endl;
168 * app: BEGIN: section
169 * app: [prefix] output1
170 * app: [prefix] output2
171 * app: END: section - Took 0.1s
181 Block( const char *label
)
184 if( !debugEnabled() ) return;
187 gettimeofday( &m_start
, 0 );
189 kDebug() << "BEGIN:" << label
;
190 Debug::modifieableIndent() += " ";
196 if( !debugEnabled() ) return;
200 gettimeofday( &end
, 0 );
202 end
.tv_sec
-= m_start
.tv_sec
;
203 if( end
.tv_usec
< m_start
.tv_usec
) {
204 // Manually carry a one from the seconds field.
205 end
.tv_usec
+= 1000000;
208 end
.tv_usec
-= m_start
.tv_usec
;
210 double duration
= double(end
.tv_sec
) + (double(end
.tv_usec
) / 1000000.0);
212 Debug::modifieableIndent().truncate( Debug::indent().length() - 2 );
213 kDebug() << "END__:" << m_label
214 << "- Took" << qPrintable( QString::number( duration
, 'g', 2 ) + "s" );
221 * @name Debug::stamp()
222 * @short To facilitate crash/freeze bugs, by making it easy to mark code that has been processed
234 * Will output (assuming the crash occurs in function2()
244 debug() << "| Stamp: " << ++n
<< endl
;
255 * @short You can pass anything to this and it will output it as a list
257 * debug() << (Debug::List() << anInt << aString << aQStringList << aDouble) << endl;
260 typedef QList
<QVariant
> List
;