1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
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.
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.
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/>.
24
#include <QCoreApplication>
31
#include "utils/TomahawkUtils.h"
33
#define LOGFILE_SIZE 1024 * 256
35
#define RELEASE_LEVEL_THRESHOLD 0
36
#define DEBUG_LEVEL_THRESHOLD LOGEXTRA
37
#define LOG_SQL_QUERIES 1
42
static int s_threshold = -1;
49
log( const char *msg, unsigned int debugLevel, bool toDisk = true )
51
if ( s_threshold < 0 )
53
if ( qApp->arguments().contains( "--verbose" ) )
54
s_threshold = LOGTHIRDPARTY;
57
s_threshold = RELEASE_LEVEL_THRESHOLD;
59
s_threshold = DEBUG_LEVEL_THRESHOLD;
64
if ( debugLevel > RELEASE_LEVEL_THRESHOLD )
67
if ( debugLevel > DEBUG_LEVEL_THRESHOLD )
71
#ifdef LOG_SQL_QUERIES
72
if ( debugLevel == LOGSQL )
76
if ( toDisk || (int)debugLevel <= s_threshold )
78
QMutexLocker lock( &s_mutex );
80
#ifdef LOG_SQL_QUERIES
81
if ( debugLevel == LOGSQL )
82
logfile << "TSQLQUERY: ";
85
logfile << QTime::currentTime().toString().toAscii().data() << " [" << QString::number( debugLevel ).toAscii().data() << "]: " << msg << endl;
89
if ( debugLevel <= LOGEXTRA || (int)debugLevel <= s_threshold )
91
QMutexLocker lock( &s_mutex );
100
TomahawkLogHandler( QtMsgType type, const char* msg )
102
static QMutex s_mutex;
104
QMutexLocker locker( &s_mutex );
108
log( msg, LOGTHIRDPARTY );
129
return TomahawkUtils::appLogDir().filePath( "Tomahawk.log" );
136
if ( QFileInfo( logFile().toLocal8Bit() ).size() > LOGFILE_SIZE )
140
QFile f( logFile().toLocal8Bit() );
141
f.open( QIODevice::ReadOnly | QIODevice::Text );
146
QFile::remove( logFile().toLocal8Bit() );
149
QFile f( logFile().toLocal8Bit() );
150
f.open( QIODevice::WriteOnly | QIODevice::Text );
151
f.write( lc.right( LOGFILE_SIZE - ( LOGFILE_SIZE / 4 ) ) );
156
logfile.open( logFile().toLocal8Bit(), ios::app );
157
qInstallMsgHandler( TomahawkLogHandler );
162
using namespace Logger;
164
TLog::TLog( unsigned int debugLevel )
166
, m_debugLevel( debugLevel )
173
log( m_msg.toLocal8Bit().data(), m_debugLevel );