Commit 63e1ba59 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Allow verbosity to be adjusted by the user

parent b121f05b
......@@ -76,6 +76,7 @@ class IMediaLibrary
/// \return true in case of success, false otherwise
///
virtual bool initialize( const std::string& dbPath, const std::string& snapshotPath, IMediaLibraryCb* metadataCb ) = 0;
virtual void setVerbosity( LogLevel v ) = 0;
/**
* Replaces the default filesystem factory
* The default one will use standard opendir/readdir functions
......
......@@ -55,4 +55,13 @@ typedef std::shared_ptr<IArtist> ArtistPtr;
typedef SqliteConnection* DBConnection;
enum class LogLevel
{
Debug,
Error,
Warning,
Info,
};
#endif // TYPES_H
......@@ -73,6 +73,7 @@ const std::vector<std::string> MediaLibrary::supportedAudioExtensions {
MediaLibrary::MediaLibrary()
: m_discoverer( new DiscovererWorker )
, m_verbosity( LogLevel::Error )
{
}
......@@ -116,14 +117,15 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& sna
m_dbConnection.reset( new SqliteConnection( dbPath ) );
m_parser.reset( new Parser( m_dbConnection.get(), m_callback ) );
if ( mlCallback != nullptr )
{
const char* args[] = {
"-vv",
};
m_vlcInstance = VLC::Instance( sizeof(args) / sizeof(args[0]), args );
m_vlcInstance.logSet([](int lvl, const libvlc_log_t*, std::string msg) {
m_vlcInstance.logSet([this](int lvl, const libvlc_log_t*, std::string msg) {
if ( m_verbosity != LogLevel::Debug )
return ;
if ( lvl == LIBVLC_ERROR )
Log::Error( msg );
else if ( lvl == LIBVLC_WARNING )
......@@ -160,6 +162,12 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& sna
return true;
}
void MediaLibrary::setVerbosity(LogLevel v)
{
m_verbosity = v;
Log::setLogLevel( v );
}
std::vector<MediaPtr> MediaLibrary::files()
{
return Media::fetchAll( m_dbConnection.get() );
......
......@@ -44,6 +44,7 @@ class MediaLibrary : public IMediaLibrary
MediaLibrary();
~MediaLibrary();
virtual bool initialize( const std::string& dbPath, const std::string& snapshotPath, IMediaLibraryCb* metadataCb ) override;
virtual void setVerbosity( LogLevel v ) override;
virtual void setFsFactory( std::shared_ptr<factory::IFileSystem> fsFactory ) override;
virtual std::vector<MediaPtr> files() override;
......@@ -112,5 +113,6 @@ class MediaLibrary : public IMediaLibrary
// Same reasoning applies here.
//FIXME: Having to maintain a specific ordering sucks, let's use shared_ptr or something
std::unique_ptr<DiscovererWorker> m_discoverer;
LogLevel m_verbosity;
};
#endif // MEDIALIBRARY_H
......@@ -25,3 +25,4 @@
std::unique_ptr<ILogger> Log::s_defaultLogger = std::unique_ptr<ILogger>( new IostreamLogger );
std::atomic<ILogger*> Log::s_logger;
std::atomic<LogLevel> Log::s_logLevel;
......@@ -27,17 +27,11 @@
#include <sstream>
#include "ILogger.h"
#include "Types.h"
class Log
{
private:
enum class Level
{
Error,
Warning,
Info,
};
template <typename T>
static void createMsg( std::stringstream& s, T&& t )
{
......@@ -61,21 +55,26 @@ private:
}
template <typename... Args>
static void log(Level lvl, Args&&... args)
static void log(LogLevel lvl, Args&&... args)
{
if ( lvl < s_logLevel.load( std::memory_order_relaxed ) )
return;
auto msg = createMsg( std::forward<Args>( args )... );
auto l = s_logger.load( std::memory_order_consume );
if ( l == nullptr )
l = s_defaultLogger.get();
switch ( lvl )
{
case Level::Error:
case LogLevel::Error:
l->Error( msg );
break;
case Level::Warning:
case LogLevel::Warning:
l->Warning( msg );
break;
case Level::Info:
case LogLevel::Debug:
case LogLevel::Info:
l->Info( msg );
break;
}
......@@ -87,22 +86,27 @@ public:
s_logger.store( logger, std::memory_order_relaxed );
}
static void setLogLevel( LogLevel level )
{
s_logLevel.store( level, std::memory_order_relaxed );
}
template <typename... Args>
static void Error( Args... args )
{
log( Level::Error, std::forward<Args>( args )... );
log( LogLevel::Error, std::forward<Args>( args )... );
}
template <typename... Args>
static void Warning( Args... args )
{
log( Level::Warning, std::forward<Args>( args )... );
log( LogLevel::Warning, std::forward<Args>( args )... );
}
template <typename... Args>
static void Info( Args... args )
{
log( Level::Info, std::forward<Args>( args )... );
log( LogLevel::Info, std::forward<Args>( args )... );
}
private:
......@@ -110,6 +114,7 @@ private:
private:
static std::unique_ptr<ILogger> s_defaultLogger;
static std::atomic<ILogger*> s_logger;
static std::atomic<LogLevel> s_logLevel;
};
#if defined(__clang__) || defined(__GNUG__)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment