[vlmc-devel] VlmcLogger: Allow backend & vlmc to have different log levels

Hugo Beauzée-Luyssen git at videolan.org
Fri Feb 28 20:14:02 CET 2014


vlmc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Fri Feb 28 21:04:02 2014 +0200| [5f8898b03fa2ed689c5c10cfc83b5e260917071f] | committer: Hugo Beauzée-Luyssen

VlmcLogger: Allow backend & vlmc to have different log levels

> http://git.videolan.org/gitweb.cgi/vlmc.git/?a=commit;h=5f8898b03fa2ed689c5c10cfc83b5e260917071f
---

 src/Tools/VlmcLogger.cpp |   82 ++++++++++++++++++++++++++++++----------------
 src/Tools/VlmcLogger.h   |    9 +++--
 2 files changed, 60 insertions(+), 31 deletions(-)

diff --git a/src/Tools/VlmcLogger.cpp b/src/Tools/VlmcLogger.cpp
index f4ad9f1..358df2a 100644
--- a/src/Tools/VlmcLogger.cpp
+++ b/src/Tools/VlmcLogger.cpp
@@ -29,7 +29,9 @@
 #include <QStringList>
 #include <QThread>
 
-VlmcLogger::VlmcLogger() : m_logFile( NULL )
+VlmcLogger::VlmcLogger()
+    : m_logFile( NULL )
+    , m_backendLogLevel( Backend::IBackend::None )
 {
     //setup log level :
     {
@@ -39,11 +41,6 @@ VlmcLogger::VlmcLogger() : m_logFile( NULL )
         // 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.
     }
-    {
-        SettingValue* logLevel = VLMC_CREATE_PREFERENCE( SettingValue::Int, "private/VlcLogLevel", (int)VlmcLogger::Quiet,
-                                                        "", "", SettingValue::Private | SettingValue::Clamped | SettingValue::Runtime );
-        logLevel->setLimits((int)Debug, (int)Verbose);
-    }
     QStringList args = qApp->arguments();
     if ( args.indexOf( QRegExp( "-vv+" ) ) >= 0 )
         m_currentLogLevel = VlmcLogger::Debug;
@@ -80,31 +77,22 @@ VlmcLogger::VlmcLogger() : m_logFile( NULL )
         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 );
+            unsigned int vlcLogLevel = vlcLogLevelStr.toUInt( &ok );
+            if ( ok == true )
+            {
+                if ( vlcLogLevel >= 3 )
+                    m_backendLogLevel = Backend::IBackend::Debug;
+                else if ( vlcLogLevel == 2 )
+                    m_backendLogLevel = Backend::IBackend::Warning;
+                else if ( vlcLogLevel == 1 )
+                    m_backendLogLevel = Backend::IBackend::Error;
+            }
             else
                 vlmcWarning() << tr("Invalid value supplied for argument --vlcverbose" );
         }
     }
-
-
-//    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 );
-//    }
+    Backend::IBackend* backend = Backend::getBackend();
+    backend->setLogHandler( this, &VlmcLogger::backendLogHandler );
 }
 
 VlmcLogger::~VlmcLogger()
@@ -165,9 +153,15 @@ VlmcLogger::vlmcMessageHandler( QtMsgType type, const char* msg )
         //FIXME: Messages are not guaranteed to arrive in order
         self->writeToFile(msg);
     }
-    if ( (int)type < (int)self->m_currentLogLevel )
+    self->outputToConsole( (int)type, msg );
+}
+
+void
+VlmcLogger::outputToConsole( int level, const char *msg )
+{
+    if ( level < (int)m_currentLogLevel )
         return ;
-    switch ( type )
+    switch ( (QtMsgType)level )
     {
     case QtDebugMsg:
         fprintf(stdout, "%s\n", msg);
@@ -183,3 +177,33 @@ VlmcLogger::vlmcMessageHandler( QtMsgType type, const char* msg )
         abort();
     }
 }
+
+void
+VlmcLogger::backendLogHandler(void *data, Backend::IBackend::LogLevel logLevel, const char* msg )
+{
+    VlmcLogger* self = reinterpret_cast<VlmcLogger*>( data );
+    char* newMsg = NULL;
+    asprintf( &newMsg, "[%s] T #%p [Backend] %s", qPrintable( QTime::currentTime().toString( "hh:mm:ss.zzz" ) ),
+              QThread::currentThreadId(), msg );
+    self->writeToFile( newMsg );
+    if ( logLevel < self->m_backendLogLevel )
+    {
+        free( newMsg );
+        return ;
+    }
+    switch ( logLevel )
+    {
+        case Backend::IBackend::Debug:
+            self->outputToConsole( Debug, newMsg );
+            break;
+        case Backend::IBackend::Warning:
+            self->outputToConsole( Verbose, newMsg );
+            break;
+        case Backend::IBackend::Error:
+            self->outputToConsole( Quiet, newMsg );
+            break;
+        default:
+            Q_ASSERT(false);
+    }
+    free( newMsg );
+}
diff --git a/src/Tools/VlmcLogger.h b/src/Tools/VlmcLogger.h
index 277e014..c5aa768 100644
--- a/src/Tools/VlmcLogger.h
+++ b/src/Tools/VlmcLogger.h
@@ -26,6 +26,7 @@
 #include <QObject>
 #include <cstdio>
 
+#include "Ibackend.h"
 #include "Singleton.hpp"
 
 /**
@@ -52,14 +53,18 @@ class   VlmcLogger : public QObject, public Singleton<VlmcLogger>
 #else
         static void     vlmcMessageHandler( QtMsgType type, const char* msg );
 #endif
+        static void     backendLogHandler( void* data, Backend::IBackend::LogLevel logLevel, const char* msg );
+
         void            setup();
     private:
         VlmcLogger();
         virtual ~VlmcLogger();
         void            writeToFile(const char* msg);
+        void            outputToConsole( int level, const char* msg );
 
-        FILE*           m_logFile;
-        LogLevel        m_currentLogLevel;
+        FILE*                           m_logFile;
+        LogLevel                        m_currentLogLevel;
+        Backend::IBackend::LogLevel     m_backendLogLevel;
 
     private slots:
         void            logLevelChanged( const QVariant& logLevel );



More information about the Vlmc-devel mailing list