PageRenderTime 72ms CodeModel.GetById 29ms app.highlight 19ms RepoModel.GetById 21ms app.codeStats 0ms

/src/libtomahawk/utils/Logger.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 220 lines | 158 code | 42 blank | 20 comment | 14 complexity | 17327efd964f847260717c9c9cf9a4ed MD5 | raw file
  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
 39using namespace std;
 40
 41ofstream logStream;
 42static int s_threshold = -1;
 43QMutex s_mutex;
 44bool shutdownInProgress = false;
 45
 46
 47namespace Logger
 48{
 49
 50static void
 51log( const char *msg, unsigned int debugLevel, bool toDisk = true )
 52{
 53    if ( s_threshold < 0 )
 54    {
 55        if ( qApp->arguments().contains( "--verbose" ) )
 56            s_threshold = LOGTHIRDPARTY;
 57        else
 58            #ifdef QT_NO_DEBUG
 59            s_threshold = RELEASE_LEVEL_THRESHOLD;
 60            #else
 61            s_threshold = DEBUG_LEVEL_THRESHOLD;
 62            #endif
 63    }
 64
 65    if ( debugLevel > LOGTHIRDPARTY )
 66        toDisk = false;
 67
 68    #ifdef LOG_SQL_QUERIES
 69    if ( debugLevel == LOGSQL )
 70        toDisk = true;
 71    #endif
 72
 73    if ( toDisk || (int)debugLevel <= s_threshold )
 74    {
 75        QMutexLocker lock( &s_mutex );
 76
 77        #ifdef LOG_SQL_QUERIES
 78        if ( debugLevel == LOGSQL )
 79            logStream << "TSQLQUERY: ";
 80        #endif
 81
 82        if ( shutdownInProgress )
 83        {
 84            // Do not use locales anymore in shutdown
 85            logStream << QDate::currentDate().day() << "."
 86                      << QDate::currentDate().month() << "."
 87                      << QDate::currentDate().year() << " - "
 88                      << QTime::currentTime().hour() << ":"
 89                      << QTime::currentTime().minute() << ":"
 90                      << QTime::currentTime().second()
 91                      << " [" << QString::number( debugLevel ).toUtf8().data() << "]: "
 92                      << msg << endl;
 93        }
 94        else
 95        {
 96            logStream << QDate::currentDate().toString().toUtf8().data()
 97                      << " - "
 98                      << QTime::currentTime().toString().toUtf8().data()
 99                      << " [" << QString::number( debugLevel ).toUtf8().data() << "]: "
100                      << msg << endl;
101        }
102
103        logStream.flush();
104    }
105
106    if ( debugLevel <= LOGEXTRA || (int)debugLevel <= s_threshold )
107    {
108        QMutexLocker lock( &s_mutex );
109
110        if ( shutdownInProgress )
111        {
112            wcout << QTime::currentTime().hour() << ":"
113                  << QTime::currentTime().minute() << ":"
114                  << QTime::currentTime().second()
115                  << " [" << QString::number( debugLevel ).toStdWString().c_str() << "]: "
116                  << msg << endl;
117        }
118        else
119        {
120            wcout << QTime::currentTime().toString().toUtf8().data()
121                  << " [" << QString::number( debugLevel ).toStdWString().c_str() << "]: "
122                  << msg << endl;
123        }
124
125        wcout.flush();
126    }
127}
128
129
130void
131TomahawkLogHandler( QtMsgType type, const QMessageLogContext& context, const QString& msg )
132{
133    static QMutex s_mutex;
134
135    QByteArray ba = msg.toUtf8();
136    const char* message = ba.constData();
137
138    QMutexLocker locker( &s_mutex );
139    switch( type )
140    {
141        case QtDebugMsg:
142            log( message, LOGTHIRDPARTY );
143            break;
144
145        case QtInfoMsg:
146            log( message, 0 );
147            break;
148
149        case QtCriticalMsg:
150            log( message, 0 );
151            break;
152
153        case QtWarningMsg:
154            log( message, 0 );
155            break;
156
157        case QtFatalMsg:
158            log( message, 0 );
159            break;
160    }
161}
162
163
164
165void
166setupLogfile( QFile& f )
167{
168    if ( QFileInfo( f ).size() > LOGFILE_SIZE )
169    {
170        QByteArray lc;
171        {
172            f.open( QIODevice::ReadOnly | QIODevice::Text );
173            f.seek( f.size() - ( LOGFILE_SIZE - ( LOGFILE_SIZE / 4 ) ) );
174            lc = f.readAll();
175            f.close();
176        }
177
178        f.remove();
179
180        {
181            f.open( QIODevice::WriteOnly | QIODevice::Text );
182            f.write( lc );
183            f.close();
184        }
185    }
186
187#ifdef OFSTREAM_CAN_OPEN_WCHAR_FILE_NAMES
188    // this is not supported in upstream libstdc++ as shipped with GCC
189    // GCC needs the patch from https://gcc.gnu.org/ml/libstdc++/2011-06/msg00066.html applied
190    logStream.open( f.fileName().toStdWString().c_str() );
191#else
192    logStream.open( f.fileName().toStdString().c_str() );
193#endif
194
195    qInstallMessageHandler( TomahawkLogHandler );
196}
197
198}
199
200using namespace Logger;
201
202TLog::TLog( unsigned int debugLevel )
203    : QDebug( &m_msg )
204    , m_debugLevel( debugLevel )
205{
206}
207
208
209TLog::~TLog()
210{
211    log( m_msg.toUtf8().data(), m_debugLevel );
212}
213
214
215void
216tLogNotifyShutdown()
217{
218    QMutexLocker locker( &s_mutex );
219    shutdownInProgress = true;
220}