2
Copyright (c) 2003-2005 Max Howell <max.howell@methylblue.com>
3
Copyright (c) 2007-2009 Mark Kretschmann <kretschmann@kde.org>
4
Copyright (c) 2010 Kevin Funk <krf@electrostorm.net>
5
Copyright (c) 2011 Harald Sitter <sitter@kde.org>
7
This library is free software; you can redistribute it and/or
8
modify it under the terms of the GNU Lesser General Public
9
License as published by the Free Software Foundation; either
10
version 2.1 of the License, or (at your option) any later version.
12
This library 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 GNU
15
Lesser General Public License for more details.
17
You should have received a copy of the GNU Lesser General Public
18
License along with this library. If not, see <http://www.gnu.org/licenses/>.
24
#include <QtCore/QMutex>
25
#include <QtCore/QObject>
26
#include <QtGui/QApplication>
31
// Define Application wide prefix
33
#define APP_PREFIX QLatin1String( "PHONON-GST" )
36
#define DEBUG_INDENT_OBJECTNAME QLatin1String("Debug_Indent_object")
38
QMutex Debug::mutex( QMutex::Recursive );
40
using namespace Debug;
42
static bool s_debugEnabled = true;
43
static bool s_debugColorsEnabled = true;
45
IndentPrivate::IndentPrivate(QObject* parent)
48
setObjectName( DEBUG_INDENT_OBJECTNAME );
52
* We can't use a statically instantiated QString for the indent, because
53
* static namespaces are unique to each dlopened library. So we piggy back
54
* the QString on the KApplication instance
56
IndentPrivate* IndentPrivate::instance()
58
QObject* qOApp = reinterpret_cast<QObject*>(qApp);
59
QObject* obj = qOApp ? qOApp->findChild<QObject*>( DEBUG_INDENT_OBJECTNAME ) : 0;
60
return (obj ? static_cast<IndentPrivate*>( obj ) : new IndentPrivate( qApp ));
64
Text color codes (use last digit here)
65
30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
67
static int s_colors[] = { 1, 2, 4, 5, 6 }; // no yellow and white for sanity
68
static int s_colorIndex = 0;
70
static QString toString( DebugLevel level )
85
static int toColor( DebugLevel level )
94
return 0; // default: black
98
static QString colorize( const QString &text, int color = s_colorIndex )
100
if( !debugColorEnabled() )
103
return QString( "\x1b[00;3%1m%2\x1b[00;39m" ).arg( QString::number(s_colors[color]), text );
106
static QString reverseColorize( const QString &text, int color )
108
if( !debugColorEnabled() )
111
return QString( "\x1b[07;3%1m%2\x1b[00;39m" ).arg( QString::number(color), text );
114
QString Debug::indent()
116
return IndentPrivate::instance()->m_string;
119
bool Debug::debugEnabled()
121
return s_debugEnabled;
124
bool Debug::debugColorEnabled()
126
return s_debugColorsEnabled;
129
void Debug::setDebugEnabled( bool enable )
131
s_debugEnabled = enable;
134
void Debug::setColoredDebug( bool enable )
136
s_debugColorsEnabled = enable;
139
QDebug Debug::dbgstream( DebugLevel level )
144
// if( !debugEnabled() )
145
// return kDebugDevNull();
148
const QString currentIndent = indent();
151
QString text = QString("%1%2").arg( APP_PREFIX ).arg( currentIndent );
152
if ( level > KDEBUG_INFO )
153
text.append( ' ' + reverseColorize( toString(level), toColor( level ) ) );
155
return QDebug( QtDebugMsg ) << qPrintable( text );
158
void Debug::perfLog( const QString &message, const QString &func )
161
if( !debugEnabled() )
164
QString str = QString( "MARK: %1: %2 %3" ).arg( qApp->applicationName(), func, message );
165
access( str.toLocal8Bit().data(), F_OK );
169
Block::Block( const char *label )
171
, m_color( s_colorIndex )
173
if( !debugEnabled() )
176
#if QT_VERSION >= 0x040700
179
m_startTime = QTime::currentTime();
183
s_colorIndex = (s_colorIndex + 1) % 5;
185
<< qPrintable( colorize( QLatin1String( "BEGIN:" ), m_color ) )
187
IndentPrivate::instance()->m_string += QLatin1String(" ");
193
if( !debugEnabled() )
196
#if QT_VERSION >= 0x040700
197
const double duration = m_startTime.elapsed() / 1000.0;
199
const double duration = (double)m_startTime.msecsTo( QTime::currentTime() ) / 1000.0;
203
IndentPrivate::instance()->m_string.truncate( Debug::indent().length() - 2 );
206
// Print timing information, and a special message (DELAY) if the method took longer than 5s
210
<< qPrintable( colorize( QLatin1String( "END__:" ), m_color ) )
212
<< qPrintable( colorize( QString( "[Took: %3s]")
213
.arg( QString::number(duration, 'g', 2) ), m_color ) );
218
<< qPrintable( colorize( QString( "END__:" ), m_color ) )
220
<< qPrintable( reverseColorize( QString( "[DELAY Took (quite long) %3s]")
221
.arg( QString::number(duration, 'g', 2) ), toColor( KDEBUG_WARN ) ) );
228
debug() << "| Stamp: " << ++n << endl;