~ubuntu-branches/ubuntu/saucy/phonon-backend-gstreamer/saucy-proposed

« back to all changes in this revision

Viewing changes to gstreamer/debug.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2011-04-15 14:43:30 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110415144330-7uif3319lxdu4ltt
Tags: 4:4.7.0really4.5.0-0ubuntu2
* New upstream release
* Add kubuntu_02_install_codec.diff to fix codec install

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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>
 
6
 
 
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.
 
11
 
 
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.
 
16
 
 
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/>.
 
19
*/
 
20
 
 
21
#include "debug.h"
 
22
#include "debug_p.h"
 
23
 
 
24
#include <QtCore/QMutex>
 
25
#include <QtCore/QObject>
 
26
#include <QtGui/QApplication>
 
27
 
 
28
#include <iostream>
 
29
#include <unistd.h>
 
30
 
 
31
// Define Application wide prefix
 
32
#ifndef APP_PREFIX
 
33
#define APP_PREFIX QLatin1String( "PHONON-GST" )
 
34
#endif
 
35
 
 
36
#define DEBUG_INDENT_OBJECTNAME QLatin1String("Debug_Indent_object")
 
37
 
 
38
QMutex Debug::mutex( QMutex::Recursive );
 
39
 
 
40
using namespace Debug;
 
41
 
 
42
static bool s_debugEnabled = true;
 
43
static bool s_debugColorsEnabled = true;
 
44
 
 
45
IndentPrivate::IndentPrivate(QObject* parent)
 
46
    : QObject(parent)
 
47
{
 
48
    setObjectName( DEBUG_INDENT_OBJECTNAME );
 
49
}
 
50
 
 
51
/**
 
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
 
55
 */
 
56
IndentPrivate* IndentPrivate::instance()
 
57
{
 
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 ));
 
61
}
 
62
 
 
63
/*
 
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
 
66
*/
 
67
static int s_colors[] = { 1, 2, 4, 5, 6 }; // no yellow and white for sanity
 
68
static int s_colorIndex = 0;
 
69
 
 
70
static QString toString( DebugLevel level )
 
71
{
 
72
    switch( level )
 
73
    {
 
74
        case KDEBUG_WARN:
 
75
            return "[WARNING]";
 
76
        case KDEBUG_ERROR:
 
77
            return "[ERROR__]";
 
78
        case KDEBUG_FATAL:
 
79
            return "[FATAL__]";
 
80
        default:
 
81
            return QString();
 
82
    }
 
83
}
 
84
 
 
85
static int toColor( DebugLevel level )
 
86
{
 
87
    switch( level ) {
 
88
        case KDEBUG_WARN:
 
89
            return 3; // red
 
90
        case KDEBUG_ERROR:
 
91
        case KDEBUG_FATAL:
 
92
            return 1; // yellow
 
93
        default:
 
94
            return 0; // default: black
 
95
    }
 
96
}
 
97
 
 
98
static QString colorize( const QString &text, int color = s_colorIndex )
 
99
{
 
100
    if( !debugColorEnabled() )
 
101
        return text;
 
102
 
 
103
    return QString( "\x1b[00;3%1m%2\x1b[00;39m" ).arg( QString::number(s_colors[color]), text );
 
104
}
 
105
 
 
106
static QString reverseColorize( const QString &text, int color )
 
107
{
 
108
    if( !debugColorEnabled() )
 
109
        return text;
 
110
 
 
111
    return QString( "\x1b[07;3%1m%2\x1b[00;39m" ).arg( QString::number(color), text );
 
112
}
 
113
 
 
114
QString Debug::indent()
 
115
{
 
116
    return IndentPrivate::instance()->m_string;
 
117
}
 
118
 
 
119
bool Debug::debugEnabled()
 
120
{
 
121
    return s_debugEnabled;
 
122
}
 
123
 
 
124
bool Debug::debugColorEnabled()
 
125
{
 
126
    return s_debugColorsEnabled;
 
127
}
 
128
 
 
129
void Debug::setDebugEnabled( bool enable )
 
130
{
 
131
    s_debugEnabled = enable;
 
132
}
 
133
 
 
134
void Debug::setColoredDebug( bool enable )
 
135
{
 
136
    s_debugColorsEnabled = enable;
 
137
}
 
138
 
 
139
QDebug Debug::dbgstream( DebugLevel level )
 
140
{
 
141
#ifdef __GNUC__
 
142
#warning FIXME
 
143
#endif
 
144
//    if( !debugEnabled() )
 
145
//        return kDebugDevNull();
 
146
 
 
147
    mutex.lock();
 
148
    const QString currentIndent = indent();
 
149
    mutex.unlock();
 
150
 
 
151
    QString text = QString("%1%2").arg( APP_PREFIX ).arg( currentIndent );
 
152
    if ( level > KDEBUG_INFO )
 
153
        text.append( ' ' + reverseColorize( toString(level), toColor( level ) ) );
 
154
 
 
155
    return QDebug( QtDebugMsg ) << qPrintable( text );
 
156
}
 
157
 
 
158
void Debug::perfLog( const QString &message, const QString &func )
 
159
{
 
160
#ifdef Q_OS_UNIX
 
161
    if( !debugEnabled() )
 
162
        return;
 
163
 
 
164
    QString str = QString( "MARK: %1: %2 %3" ).arg( qApp->applicationName(), func, message );
 
165
    access( str.toLocal8Bit().data(), F_OK );
 
166
#endif
 
167
}
 
168
 
 
169
Block::Block( const char *label )
 
170
    : m_label( label )
 
171
    , m_color( s_colorIndex )
 
172
{
 
173
    if( !debugEnabled() )
 
174
        return;
 
175
 
 
176
#if QT_VERSION >= 0x040700
 
177
    m_startTime.start();
 
178
#else
 
179
    m_startTime = QTime::currentTime();
 
180
#endif
 
181
 
 
182
    mutex.lock();
 
183
    s_colorIndex = (s_colorIndex + 1) % 5;
 
184
    dbgstream()
 
185
        << qPrintable( colorize( QLatin1String( "BEGIN:" ), m_color ) )
 
186
        << m_label;
 
187
    IndentPrivate::instance()->m_string += QLatin1String("  ");
 
188
    mutex.unlock();
 
189
}
 
190
 
 
191
Block::~Block()
 
192
{
 
193
    if( !debugEnabled() )
 
194
        return;
 
195
 
 
196
#if QT_VERSION >= 0x040700
 
197
    const double duration = m_startTime.elapsed() / 1000.0;
 
198
#else
 
199
    const double duration = (double)m_startTime.msecsTo( QTime::currentTime() ) / 1000.0;
 
200
#endif
 
201
 
 
202
    mutex.lock();
 
203
    IndentPrivate::instance()->m_string.truncate( Debug::indent().length() - 2 );
 
204
    mutex.unlock();
 
205
 
 
206
    // Print timing information, and a special message (DELAY) if the method took longer than 5s
 
207
    if( duration < 5.0 )
 
208
    {
 
209
        dbgstream()
 
210
            << qPrintable( colorize( QLatin1String( "END__:" ), m_color ) )
 
211
            << m_label
 
212
            << qPrintable( colorize( QString( "[Took: %3s]")
 
213
                                     .arg( QString::number(duration, 'g', 2) ), m_color ) );
 
214
    }
 
215
    else
 
216
    {
 
217
        dbgstream()
 
218
            << qPrintable( colorize( QString( "END__:" ), m_color ) )
 
219
            << m_label
 
220
            << qPrintable( reverseColorize( QString( "[DELAY Took (quite long) %3s]")
 
221
                                            .arg( QString::number(duration, 'g', 2) ), toColor( KDEBUG_WARN ) ) );
 
222
    }
 
223
}
 
224
 
 
225
void Debug::stamp()
 
226
{
 
227
    static int n = 0;
 
228
    debug() << "| Stamp: " << ++n << endl;
 
229
}