File indexing completed on 2025-01-12 04:19:23
0001 /* 0002 Copyright (c) 2003-2005 Max Howell <max.howell@methylblue.com> 0003 Copyright (c) 2007-2009 Mark Kretschmann <kretschmann@kde.org> 0004 Copyright (c) 2010 Kevin Funk <krf@electrostorm.net> 0005 Copyright (c) 2011 Harald Sitter <sitter@kde.org> 0006 0007 This library is free software; you can redistribute it and/or 0008 modify it under the terms of the GNU Lesser General Public 0009 License as published by the Free Software Foundation; either 0010 version 2.1 of the License, or (at your option) any later version. 0011 0012 This library is distributed in the hope that it will be useful, 0013 but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 Lesser General Public License for more details. 0016 0017 You should have received a copy of the GNU Lesser General Public 0018 License along with this library. If not, see <http://www.gnu.org/licenses/>. 0019 */ 0020 0021 #ifndef PHONON_DEBUG_H 0022 #define PHONON_DEBUG_H 0023 0024 // We always want debug output available at runtime 0025 #undef QT_NO_DEBUG_OUTPUT 0026 #undef KDE_NO_DEBUG_OUTPUT 0027 0028 #include <QtCore/QDebug> 0029 #include <QtCore/QMutex> 0030 0031 #if QT_VERSION >= 0x040700 0032 # include <QtCore/QElapsedTimer> 0033 #else 0034 # include <QtCore/QTime> 0035 #endif 0036 0037 // Platform specific macros 0038 #ifdef _WIN32 // krazy:exclude=cpp we really want to check a compiler feature here :P 0039 #define __PRETTY_FUNCTION__ __FUNCTION__ 0040 #endif 0041 #ifdef __SUNPRO_CC 0042 #define __PRETTY_FUNCTION__ __FILE__ 0043 #endif 0044 0045 /** 0046 * @namespace Debug 0047 * @short kdebug with indentation functionality and convenience macros 0048 * @author Max Howell <max.howell@methylblue.com> 0049 * 0050 * Usage: 0051 * 0052 * #define DEBUG_PREFIX "Blah" 0053 * #include "debug.h" 0054 * 0055 * void function() 0056 * { 0057 * Debug::Block myBlock( __PRETTY_FUNCTION__ ); 0058 * 0059 * debug() << "output1" << endl; 0060 * debug() << "output2" << endl; 0061 * } 0062 * 0063 * Will output: 0064 * 0065 * app: BEGIN: void function() 0066 * app: [Blah] output1 0067 * app: [Blah] output2 0068 * app: END: void function(): Took 0.1s 0069 * 0070 * @see Block 0071 * @see CrashHelper 0072 * @see ListStream 0073 */ 0074 namespace Debug 0075 { 0076 extern QRecursiveMutex mutex; 0077 0078 enum DebugLevel { 0079 DEBUG_INFO = 0, 0080 DEBUG_WARN = 1, 0081 DEBUG_ERROR = 2, 0082 DEBUG_FATAL = 3, 0083 DEBUG_NONE = 4 0084 }; 0085 0086 QDebug dbgstream( DebugLevel level = DEBUG_INFO ); 0087 bool debugEnabled(); 0088 bool debugColorEnabled(); 0089 DebugLevel minimumDebugLevel(); 0090 void setColoredDebug( bool enable ); 0091 void setMinimumDebugLevel( DebugLevel level ); 0092 QString indent(); 0093 0094 static inline QDebug dbgstreamwrapper( DebugLevel level ) { return dbgstream( level ); } 0095 0096 static inline QDebug debug() { return dbgstreamwrapper( DEBUG_INFO ); } 0097 static inline QDebug warning() { return dbgstreamwrapper( DEBUG_WARN ); } 0098 static inline QDebug error() { return dbgstreamwrapper( DEBUG_ERROR ); } 0099 static inline QDebug fatal() { return dbgstreamwrapper( DEBUG_FATAL ); } 0100 0101 void perfLog( const QString &message, const QString &func ); 0102 } 0103 0104 using Debug::debug; 0105 using Debug::warning; 0106 using Debug::error; 0107 using Debug::fatal; 0108 0109 /// Standard function announcer 0110 #define DEBUG_FUNC_INFO { Debug::mutex.lock(); qDebug() << Debug::indent() ; Debug::mutex.unlock(); } 0111 0112 /// Announce a line 0113 #define DEBUG_LINE_INFO { Debug::mutex.lock(); qDebug() << Debug::indent() << "Line: " << __LINE__; Debug::mutex.unlock(); } 0114 0115 /// Convenience macro for making a standard Debug::Block 0116 #define DEBUG_BLOCK Debug::Block uniquelyNamedStackAllocatedStandardBlock( __PRETTY_FUNCTION__ ); 0117 0118 /// Performance logging 0119 #define PERF_LOG( msg ) { Debug::perfLog( msg, __PRETTY_FUNCTION__ ); } 0120 0121 class BlockPrivate; 0122 0123 namespace Debug 0124 { 0125 /** 0126 * @class Debug::Block 0127 * @short Use this to label sections of your code 0128 * 0129 * Usage: 0130 * 0131 * void function() 0132 * { 0133 * Debug::Block myBlock( "section" ); 0134 * 0135 * debug() << "output1" << endl; 0136 * debug() << "output2" << endl; 0137 * } 0138 * 0139 * Will output: 0140 * 0141 * app: BEGIN: section 0142 * app: [prefix] output1 0143 * app: [prefix] output2 0144 * app: END: section - Took 0.1s 0145 * 0146 */ 0147 class Block 0148 { 0149 public: 0150 explicit Block( const char *name ); 0151 ~Block(); 0152 0153 private: 0154 #if QT_VERSION >= 0x040700 0155 QElapsedTimer m_startTime; 0156 #else 0157 QTime m_startTime; 0158 #endif 0159 const char *m_label; 0160 int m_color; 0161 }; 0162 0163 /** 0164 * @name Debug::stamp() 0165 * @short To facilitate crash/freeze bugs, by making it easy to mark code that has been processed 0166 * 0167 * Usage: 0168 * 0169 * { 0170 * Debug::stamp(); 0171 * function1(); 0172 * Debug::stamp(); 0173 * function2(); 0174 * Debug::stamp(); 0175 * } 0176 * 0177 * Will output (assuming the crash occurs in function2() 0178 * 0179 * app: Stamp: 1 0180 * app: Stamp: 2 0181 * 0182 */ 0183 void stamp(); 0184 } 0185 0186 #include <QtCore/QVariant> 0187 0188 namespace Debug 0189 { 0190 /** 0191 * @class Debug::List 0192 * @short You can pass anything to this and it will output it as a list 0193 * 0194 * debug() << (Debug::List() << anInt << aString << aQStringList << aDouble) << endl; 0195 */ 0196 0197 typedef QList<QVariant> List; 0198 } 0199 0200 #endif