VlmcDebug.cpp 4.25 KB
Newer Older
1 2 3
/*****************************************************************************
 * VlmcDebug.cpp: Debug tools for VLMC
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
 *
 * 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 <QCoreApplication>
Clement CHAVANCE's avatar
Clement CHAVANCE committed
24
#include <QStringList>
25 26 27 28 29 30

#include "SettingsManager.h"
#include "VlmcDebug.h"

VlmcDebug::VlmcDebug() : m_logFile( NULL )
{
31
    //setup log level :
32
    VLMC_CREATE_PRIVATE_PREFERENCE_INT( "private/LogLevel", 0 );
33
    QStringList args = qApp->arguments();
34 35
    if ( args.indexOf( QRegExp( "-vv+" ) ) >= 0 )
        SettingsManager::getInstance()->setValue( "private/LogLevel", VlmcDebug::Debug, SettingsManager::Vlmc );
36
    else if ( args.contains( "-v" ) == true )
37
        SettingsManager::getInstance()->setValue( "private/LogLevel", VlmcDebug::Verbose, SettingsManager::Vlmc );
38
    else
39
        SettingsManager::getInstance()->setValue( "private/LogLevel", VlmcDebug::Quiet, SettingsManager::Vlmc );
40

41 42 43 44 45 46 47 48 49 50
//    int pos = args.indexOf( QRegExp( "--logfile=.*" ) );
//    if ( pos > 0 )
//    {
//        QString arg = args[pos];
//        QString logFile = arg.mid( 10 );
//        if ( logFile.length() <= 0 )
//            qWarning() << tr("Invalid value supplied for argument --logfile" );
//        else
//            SettingsManager::getInstance()->setValue( "private/LogFile", logFile, SettingsManager::Vlmc );
//    }
51

52

53 54 55 56 57 58 59 60 61 62 63 64 65 66
//    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 );
//    }
67 68 69 70 71 72 73
}

VlmcDebug::~VlmcDebug()
{
    delete m_logFile;
}

74 75
void
VlmcDebug::setup()
76
{
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
77
//    qInstallMsgHandler( VlmcDebug::vlmcMessageHandler );
78 79
}

80 81
void
VlmcDebug::logFileChanged( const QVariant& logFileV )
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
{
    QString logFile = logFileV.toString();
    if ( logFile.isEmpty() == true )
    {
        m_logFile->close();
        return ;
    }
    if ( logFile == m_logFile->fileName() )
    {
        fprintf( stderr, "Trying to use the same log file. Nothing will be done" );
        return ;
    }
    m_logFile->close();
    m_logFile->setFileName( logFile );
    m_logFile->open( QFile::Append | QFile::Truncate );
}

99 100
void
VlmcDebug::vlmcMessageHandler( QtMsgType type, const char* msg )
101 102 103 104 105 106
{
    if ( VlmcDebug::getInstance()->m_logFile != NULL )
    {
        VlmcDebug::getInstance()->m_logFile->write( msg );
        VlmcDebug::getInstance()->m_logFile->write( "\n" );
    }
107 108 109
//    if ( type != QtFatalMsg
//         && type < SettingsManager::getInstance()->value( "private/LogLevel", QtDebugMsg, SettingsManager::Vlmc ).toInt() )
//        return ;
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    switch ( type )
    {
    case QtDebugMsg:
        fprintf(stderr, "%s\n", msg);
        break;
    case QtWarningMsg:
        fprintf(stderr, "%s\n", msg);
        break;
    case QtCriticalMsg:
        fprintf(stderr, "%s\n", msg);
        break;
    case QtFatalMsg:
        fprintf(stderr, "%s\n", msg);
        abort();
    }
}