VlmcLogger.cpp 6.62 KB
Newer Older
1
/*****************************************************************************
2
 * VlmcLogger.cpp: Debug tools for VLMC
3
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
5
 *
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "SettingsManager.h"
24
#include "VlmcLogger.h"
25
#include "VlmcDebug.h"
26

27 28 29 30 31
#include <QCoreApplication>
#include <QDesktopServices>
#include <QStringList>
#include <QThread>

32
VlmcLogger::VlmcLogger() : m_logFile( NULL )
33
{
34
    //setup log level :
35
    {
36
        SettingValue* logLevel = VLMC_CREATE_PREFERENCE( SettingValue::Int, "private/LogLevel", (int)VlmcLogger::Quiet,
37
                                                        "", "", SettingValue::Private | SettingValue::Clamped | SettingValue::Runtime );
38 39 40 41
        logLevel->setLimits((int)Debug, (int)Verbose);
        // Purposedly destroying the setting value, as we need to use the manager for other operations.
        //FIXME: Actually I'm not sure for setting the value since this is a private variable.
    }
42 43 44 45 46
    {
        SettingValue* logLevel = VLMC_CREATE_PREFERENCE( SettingValue::Int, "private/VlcLogLevel", (int)VlmcLogger::Quiet,
                                                        "", "", SettingValue::Private | SettingValue::Clamped | SettingValue::Runtime );
        logLevel->setLimits((int)Debug, (int)Verbose);
    }
47
    QStringList args = qApp->arguments();
48
    if ( args.indexOf( QRegExp( "-vv+" ) ) >= 0 )
49
        m_currentLogLevel = VlmcLogger::Debug;
50
    else if ( args.contains( "-v" ) == true )
51
        m_currentLogLevel = VlmcLogger::Verbose;
52
    else
53
        m_currentLogLevel = VlmcLogger::Quiet;
54 55 56 57
    SettingsManager* settingsManager = SettingsManager::getInstance();
    settingsManager->setValue( "private/LogLevel", m_currentLogLevel, SettingsManager::Vlmc );
    settingsManager->watchValue( "private/LogLevel", this, SLOT(logLevelChanged( const QVariant& )),
                                 SettingsManager::Vlmc, Qt::DirectConnection );
58

59 60 61 62 63 64
    int pos = args.indexOf( QRegExp( "--logfile=.*" ) );
    if ( pos > 0 )
    {
        QString arg = args[pos];
        QString logFile = arg.mid( 10 );
        if ( logFile.length() <= 0 )
65
            vlmcWarning() << tr("Invalid value supplied for argument --logfile" );
66 67 68 69 70
        else
        {
            m_logFile = fopen(logFile.toLocal8Bit().data(), "w");
        }
    }
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    pos = args.indexOf( QRegExp( "--vlcverbose=.*" ) );
    if ( pos > 0 )
    {
        QString arg = args[pos];
        QString vlcLogLevelStr = arg.mid( 13 );

        if ( vlcLogLevelStr.length() <= 0 )
            vlmcWarning() << tr("Invalid value supplied for argument --vlcverbose" );
        else
        {
            bool ok = false;
            int vlcLogLevel = vlcLogLevelStr.toInt( &ok );
            if (vlcLogLevel >= 2)
                settingsManager->setValue( "private/VlcLogLevel", VlmcLogger::Debug, SettingsManager::Vlmc );
            else if (vlcLogLevel == 1)
                settingsManager->setValue( "private/VlcLogLevel", VlmcLogger::Verbose, SettingsManager::Vlmc );
            else
                vlmcWarning() << tr("Invalid value supplied for argument --vlcverbose" );
        }
    }

93

94 95 96 97 98 99 100 101 102 103 104 105 106 107
//    QVariant setVal = SettingsManager::getInstance()->value( "private/LogFile", "log.vlmc", SettingsManager::Vlmc );
//    SettingsManager::getInstance()->watchValue( "private/LogFile", this,
//                                              SLOT( logFileChanged( const QVariant& ) ),
//                                              SettingsManager::Vlmc );
//    QObject::connect( qApp,
//                      SIGNAL( aboutToQuit() ),
//                      this,
//                      SLOT( deleteLater() ) );
//    QString logFile = setVal.toString();
//    if ( logFile.isEmpty() == false )
//    {
//        m_logFile = new QFile( logFile );
//        m_logFile->open( QFile::WriteOnly | QFile::Truncate );
//    }
108 109
}

110
VlmcLogger::~VlmcLogger()
111
{
112 113
    if ( m_logFile )
        fclose( m_logFile );
114 115
}

116
void
117
VlmcLogger::setup()
118
{
119 120 121
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
    qInstallMessageHandler( VlmcLogger::vlmcMessageHandler );
#else
122
    qInstallMsgHandler( VlmcLogger::vlmcMessageHandler );
123
#endif
124 125
}

126
void
127
VlmcLogger::logLevelChanged( const QVariant &logLevel )
128 129
{

130 131
    Q_ASSERT_X(logLevel.toInt() >= (int)VlmcLogger::Debug &&
               logLevel.toInt() <= (int)VlmcLogger::Quiet,
132
               "Setting log level", "Invalid value for log level");
133
    m_currentLogLevel = (VlmcLogger::LogLevel)logLevel.toInt();
134 135
}

136 137 138 139 140
/*********************************************************************
* Don't use anything which might use qDebug/qWarning/... below here. *
*********************************************************************/

void
141
VlmcLogger::writeToFile(const char *msg)
142 143 144 145 146 147 148
{
    flockfile( m_logFile );
    fputs( msg, m_logFile );
    fputc( '\n', m_logFile );
    funlockfile( m_logFile );
}

149
void
150 151 152 153 154 155
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
VlmcLogger::vlmcMessageHandler( QtMsgType type, const QMessageLogContext&, const QString& str )
{
    const QByteArray byteArray = str.toLocal8Bit();
    const char* msg = byteArray.constData();
#else
156
VlmcLogger::vlmcMessageHandler( QtMsgType type, const char* msg )
157
{
158
#endif
159 160
    //FIXME: This is ok as long as we guarantee no log message will arrive after
    // we uninstall the hook
161

162
    VlmcLogger* self = VlmcLogger::getInstance();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
163
    if ( self->m_logFile != NULL )
164
    {
165 166
        //FIXME: Messages are not guaranteed to arrive in order
        self->writeToFile(msg);
167
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
168 169
    if ( (int)type < (int)self->m_currentLogLevel )
        return ;
170 171 172
    switch ( type )
    {
    case QtDebugMsg:
173
        fprintf(stdout, "%s\n", msg);
174 175
        break;
    case QtWarningMsg:
176
        fprintf(stdout, "%s\n", msg);
177 178
        break;
    case QtCriticalMsg:
179
        fprintf(stdout, "%s\n", msg);
180 181 182 183 184 185
        break;
    case QtFatalMsg:
        fprintf(stderr, "%s\n", msg);
        abort();
    }
}