~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/utils/Logger.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 
4
 *
 
5
 *   Tomahawk is free software: you can redistribute it and/or modify
 
6
 *   it under the terms of the GNU General Public License as published by
 
7
 *   the Free Software Foundation, either version 3 of the License, or
 
8
 *   (at your option) any later version.
 
9
 *
 
10
 *   Tomahawk is distributed in the hope that it will be useful,
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *   GNU General Public License for more details.
 
14
 *
 
15
 *   You should have received a copy of the GNU General Public License
 
16
 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include "Logger.h"
 
20
 
 
21
#include <iostream>
 
22
#include <fstream>
 
23
 
 
24
#include <QCoreApplication>
 
25
#include <QDir>
 
26
#include <QFileInfo>
 
27
#include <QMutex>
 
28
#include <QTime>
 
29
#include <QVariant>
 
30
 
 
31
#include "utils/TomahawkUtils.h"
 
32
 
 
33
#define LOGFILE_SIZE 1024 * 256
 
34
 
 
35
#define RELEASE_LEVEL_THRESHOLD 0
 
36
#define DEBUG_LEVEL_THRESHOLD LOGEXTRA
 
37
#define LOG_SQL_QUERIES 1
 
38
 
 
39
using namespace std;
 
40
 
 
41
ofstream logfile;
 
42
static int s_threshold = -1;
 
43
QMutex s_mutex;
 
44
 
 
45
namespace Logger
 
46
{
 
47
 
 
48
static void
 
49
log( const char *msg, unsigned int debugLevel, bool toDisk = true )
 
50
{
 
51
    if ( s_threshold < 0 )
 
52
    {
 
53
        if ( qApp->arguments().contains( "--verbose" ) )
 
54
            s_threshold = LOGTHIRDPARTY;
 
55
        else
 
56
            #ifdef QT_NO_DEBUG
 
57
            s_threshold = RELEASE_LEVEL_THRESHOLD;
 
58
            #else
 
59
            s_threshold = DEBUG_LEVEL_THRESHOLD;
 
60
            #endif
 
61
    }
 
62
 
 
63
    #ifdef QT_NO_DEBUG
 
64
    if ( debugLevel > RELEASE_LEVEL_THRESHOLD )
 
65
        toDisk = false;
 
66
    #else
 
67
    if ( debugLevel > DEBUG_LEVEL_THRESHOLD )
 
68
        toDisk = false;
 
69
    #endif
 
70
 
 
71
    #ifdef LOG_SQL_QUERIES
 
72
    if ( debugLevel == LOGSQL )
 
73
        toDisk = true;
 
74
    #endif
 
75
 
 
76
    if ( toDisk || (int)debugLevel <= s_threshold )
 
77
    {
 
78
        QMutexLocker lock( &s_mutex );
 
79
 
 
80
        #ifdef LOG_SQL_QUERIES
 
81
        if ( debugLevel == LOGSQL )
 
82
            logfile << "TSQLQUERY: ";
 
83
        #endif
 
84
 
 
85
        logfile << QTime::currentTime().toString().toAscii().data() << " [" << QString::number( debugLevel ).toAscii().data() << "]: " << msg << endl;
 
86
        logfile.flush();
 
87
    }
 
88
 
 
89
    if ( debugLevel <= LOGEXTRA || (int)debugLevel <= s_threshold )
 
90
    {
 
91
        QMutexLocker lock( &s_mutex );
 
92
 
 
93
        cout << msg << endl;
 
94
        cout.flush();
 
95
    }
 
96
}
 
97
 
 
98
 
 
99
void
 
100
TomahawkLogHandler( QtMsgType type, const char* msg )
 
101
{
 
102
    static QMutex s_mutex;
 
103
 
 
104
    QMutexLocker locker( &s_mutex );
 
105
    switch( type )
 
106
    {
 
107
        case QtDebugMsg:
 
108
            log( msg, LOGTHIRDPARTY );
 
109
            break;
 
110
 
 
111
        case QtCriticalMsg:
 
112
            log( msg, 0 );
 
113
            break;
 
114
 
 
115
        case QtWarningMsg:
 
116
            log( msg, 0 );
 
117
            break;
 
118
 
 
119
        case QtFatalMsg:
 
120
            log( msg, 0 );
 
121
            break;
 
122
    }
 
123
}
 
124
 
 
125
 
 
126
QString
 
127
logFile()
 
128
{
 
129
    return TomahawkUtils::appLogDir().filePath( "Tomahawk.log" );
 
130
}
 
131
 
 
132
 
 
133
void
 
134
setupLogfile()
 
135
{
 
136
    if ( QFileInfo( logFile().toLocal8Bit() ).size() > LOGFILE_SIZE )
 
137
    {
 
138
        QByteArray lc;
 
139
        {
 
140
            QFile f( logFile().toLocal8Bit() );
 
141
            f.open( QIODevice::ReadOnly | QIODevice::Text );
 
142
            lc = f.readAll();
 
143
            f.close();
 
144
        }
 
145
 
 
146
        QFile::remove( logFile().toLocal8Bit() );
 
147
 
 
148
        {
 
149
            QFile f( logFile().toLocal8Bit() );
 
150
            f.open( QIODevice::WriteOnly | QIODevice::Text );
 
151
            f.write( lc.right( LOGFILE_SIZE - ( LOGFILE_SIZE / 4 ) ) );
 
152
            f.close();
 
153
        }
 
154
    }
 
155
 
 
156
    logfile.open( logFile().toLocal8Bit(), ios::app );
 
157
    qInstallMsgHandler( TomahawkLogHandler );
 
158
}
 
159
 
 
160
}
 
161
 
 
162
using namespace Logger;
 
163
 
 
164
TLog::TLog( unsigned int debugLevel )
 
165
    : QDebug( &m_msg )
 
166
    , m_debugLevel( debugLevel )
 
167
{
 
168
}
 
169
 
 
170
 
 
171
TLog::~TLog()
 
172
{
 
173
    log( m_msg.toLocal8Bit().data(), m_debugLevel );
 
174
}
 
175