Removed custom logger(switching to WServer's logger) + removed custom config file

This commit is contained in:
epoupon
2015-09-07 18:21:22 +02:00
parent 53c3613cf5
commit 65a8e4ba09
14 changed files with 70 additions and 298 deletions
-1
View File
@@ -8,7 +8,6 @@ lms_SOURCES = \
$(srcdir)/av/FormatContext.cpp \
$(srcdir)/av/InputFormatContext.cpp \
$(srcdir)/av/Stream.cpp \
$(srcdir)/config/ConfigReader.cpp \
$(srcdir)/cover/CoverArt.cpp \
$(srcdir)/cover/CoverArtGrabber.cpp \
$(srcdir)/database/Artist.cpp \
-21
View File
@@ -26,18 +26,6 @@
namespace {
std::vector<std::string> splitStrings(const std::string& source)
{
std::vector<std::string> res;
std::istringstream oss(source);
std::string str;
while(oss >> str)
res.push_back(str);
return res;
}
bool
isFileSupported(const boost::filesystem::path& file, const std::vector<boost::filesystem::path> extensions)
{
@@ -67,15 +55,6 @@ Grabber::instance()
return instance;
}
void
Grabber::init()
{
for (const std::string& extension : splitStrings( ConfigReader::instance().getString("main.cover.file_extensions")))
_fileExtensions.push_back("." + extension);
_maxFileSize = ConfigReader::instance().getULong("main.cover.file_max_size");
}
std::vector<CoverArt>
Grabber::getFromInputFormatContext(const Av::InputFormatContext& input, std::size_t nbMaxCovers) const
{
+5 -5
View File
@@ -38,8 +38,6 @@ class Grabber
static Grabber& instance();
void init();
std::vector<boost::filesystem::path> getCoverPaths(const boost::filesystem::path& directoryPath, std::size_t nbMaxCovers = 1) const;
std::vector<CoverArt> getFromDirectory(const boost::filesystem::path& path, std::size_t nbMaxCovers = 1) const;
std::vector<CoverArt> getFromInputFormatContext(const Av::InputFormatContext& input, std::size_t nbMaxCovers = 1) const;
@@ -50,9 +48,11 @@ class Grabber
private:
Grabber();
std::vector<boost::filesystem::path> _fileExtensions;
std::size_t _maxFileSize;
std::vector<boost::filesystem::path> _preferredFileNames;
std::vector<boost::filesystem::path> _fileExtensions
= {"jpg", "jpeg"};
std::size_t _maxFileSize = 5000000;
std::vector<boost::filesystem::path> _preferredFileNames
= {"cover", "front"};
};
+5 -2
View File
@@ -96,8 +96,11 @@ class Updater
Database::Handler _db;
std::vector<boost::filesystem::path> _audioExtensions;
std::vector<boost::filesystem::path> _videoExtensions;
std::vector<boost::filesystem::path> _audioExtensions
= {"mp3", "ogg", "oga", "aac", "m4a", "flac", "wav", "wma", "aif", "aiff", "ape", "mpc", "shn"};
std::vector<boost::filesystem::path> _videoExtensions
= {"flv", "avi", "mpg", "mpeg", "mp4", "m4v", "mkv", "mov", "wmv", "ogv", "divx", "m2ts"};
MetaData::Parser& _metadataParser;
+2
View File
@@ -155,6 +155,8 @@ Handler::getUser(const Wt::Auth::User& authUser)
Wt::Dbo::SqlConnectionPool*
Handler::createConnectionPool(boost::filesystem::path p)
{
LMS_LOG(MOD_DB, SEV_INFO) << "Creating connection pool on file " << p;
Wt::Dbo::backend::Sqlite3 *connection = new Wt::Dbo::backend::Sqlite3(p.string());
connection->executeSql("pragma journal_mode=WAL");
+27 -95
View File
@@ -17,105 +17,37 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/keywords/filter.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/attributes/constant.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/attributes/named_scope.hpp>
#include <boost/date_time/posix_time/ptime.hpp>
#include "config/ConfigReader.hpp"
#include "Logger.hpp"
Logger&
Logger::instance()
std::string getModuleName(Module mod)
{
static Logger instance;
return instance;
}
Logger::Logger()
{
// Initialiaz loggers
static const std::vector<Module> modules =
switch (mod)
{
MOD_AV,
MOD_COVER,
MOD_DB,
MOD_DBUPDATER,
MOD_MAIN,
MOD_METADATA,
MOD_REMOTE,
MOD_SERVICE,
MOD_TRANSCODE,
MOD_UI,
};
for(Module module : modules)
_loggers[module].add_attribute("Module", boost::log::attributes::constant< Module >(module));
}
boost::log::sources::severity_logger< Severity >&
Logger::get(Module module)
{
return _loggers[module];
}
void
Logger::init()
{
boost::log::add_common_attributes();
boost::log::register_simple_formatter_factory< Severity, char >("Severity");
if (ConfigReader::instance().getBool("main.logger.console.enable", false))
{
boost::log::add_console_log(std::cout,
boost::log::keywords::format = (
boost::log::expressions::stream
<< boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "[%Y-%m-%d %H:%M:%S]")
<< " [" << boost::log::expressions::attr< Module >("Module") << "]"
<< " [" << boost::log::expressions::attr< Severity >("Severity") << "]"
<< " " << boost::log::expressions::smessage
)
);
case MOD_AV: return "AV";
case MOD_COVER: return "COVER";
case MOD_DB: return "DB";
case MOD_DBUPDATER: return "DB UPDATER";
case MOD_MAIN: return "MAIN";
case MOD_METADATA: return "METADATA";
case MOD_REMOTE: return "REMOTE";
case MOD_SERVICE: return "SERVICE";
case MOD_TRANSCODE: return "TRANSCODE";
case MOD_UI: return "UI";
}
if (ConfigReader::instance().getBool("main.logger.file.enable", false))
{
boost::log::add_file_log
(
boost::log::keywords::file_name = ConfigReader::instance().getString("main.logger.file.path") + std::string(".%N"),
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
boost::log::keywords::open_mode = std::ios_base::app,
boost::log::keywords::auto_flush = true,
boost::log::keywords::format = (
boost::log::expressions::stream
<< boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "[%Y-%m-%d %H:%M:%S]")
<< " [" << boost::log::expressions::attr< Module >("Module") << "]"
<< " [" << boost::log::expressions::attr< Severity >("Severity") << "]"
<< " " << boost::log::expressions::smessage
)
);
}
boost::log::core::get()->set_filter
(
boost::log::expressions::attr<Severity>("Severity") <= ConfigReader::instance().getULong("main.logger.level", SEV_DEBUG)
);
return "";
}
std::string getSeverityName(Severity sev)
{
switch (sev)
{
case SEV_CRIT: return "fatal";
case SEV_ERROR: return "error";
case SEV_WARNING: return "warning";
case SEV_NOTICE:
case SEV_INFO: return "info";
case SEV_DEBUG: return "debug";
}
return "";
}
+8 -81
View File
@@ -20,15 +20,11 @@
#ifndef LOGGER_HPP__
#define LOGGER_HPP__
#include <map>
#include <Wt/WServer>
#include <Wt/WLogger>
#include <boost/log/expressions/keyword_fwd.hpp>
#include <boost/log/expressions/keyword.hpp>
#include <string>
#include <boost/log/trivial.hpp>
#include <boost/log/attributes/named_scope.hpp>
#define LMS_LOG(module, level) BOOST_LOG_SEV(Logger::instance().get(module), level)
enum Severity
{
@@ -42,7 +38,7 @@ enum Severity
enum Module
{
MOD_AV = 0,
MOD_AV,
MOD_COVER,
MOD_DB,
MOD_DBUPDATER,
@@ -54,78 +50,9 @@ enum Module
MOD_UI,
};
BOOST_LOG_ATTRIBUTE_KEYWORD(module, "Module", Module)
std::string getModuleName(Module mod);
std::string getSeverityName(Severity sev);
class Logger
{
public:
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
static Logger& instance();
void init();
boost::log::sources::severity_logger< Severity >&
get(Module module);
private:
Logger();
std::map<Module, boost::log::sources::severity_logger< Severity > > _loggers;
};
// The formatting logic for the severity level
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
std::basic_ostream< CharT, TraitsT >& strm, Severity lvl)
{
static const char* const str[] =
{
"",
"",
"CRIT",
"ERROR",
"WARNING",
"NOTICE",
"INFO",
"DEBUG"
};
if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
strm << str[lvl];
else
strm << static_cast< int >(lvl);
return strm;
}
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
std::basic_ostream< CharT, TraitsT >& strm, Module val)
{
const char* res = NULL;
switch(val)
{
case MOD_AV: res = "AV"; break;
case MOD_COVER: res = "COVER"; break;
case MOD_DB: res = "DB"; break;
case MOD_DBUPDATER: res = "DBUPDATER"; break;
case MOD_MAIN: res = "MAIN"; break;
case MOD_METADATA: res = "METADATA"; break;
case MOD_REMOTE: res = "REMOTE"; break;
case MOD_SERVICE: res = "SERVICE"; break;
case MOD_TRANSCODE: res = "TRANSCODE"; break;
case MOD_UI: res = "UI"; break;
}
if (res)
strm << res;
else
strm << static_cast< int >(val);
return strm;
}
#endif // LOGGER_HPP__
#define LMS_LOG(module, level) Wt::WServer::instance()->log(getSeverityName(level)) << Wt::WLogger::sep << "[" << getModuleName(module) << "]" << Wt::WLogger::sep
#endif
+11 -24
View File
@@ -20,7 +20,6 @@
#include <boost/filesystem.hpp>
#include "config/config.h"
#include "config/ConfigReader.hpp"
#include "transcode/AvConvTranscoder.hpp"
#include "av/Common.hpp"
#include "logger/Logger.hpp"
@@ -45,13 +44,8 @@ int main(int argc, char* argv[])
try
{
// Open configuration file
boost::filesystem::path configFilePath("/etc/lms.conf"); // TODO use $confdir from autotools
ConfigReader::instance().setFile(configFilePath);
Logger::instance().init();
CoverArt::Grabber::instance().init();
Wt::WServer server(argv[0]);
server.setServerConfiguration (argc, argv);
Service::ServiceManager& serviceManager = Service::ServiceManager::instance();
@@ -61,8 +55,7 @@ int main(int argc, char* argv[])
Database::Handler::configureAuth();
// Initializing a connection pool to the database that will be shared along services
std::unique_ptr<Wt::Dbo::SqlConnectionPool> connectionPool( Database::Handler::createConnectionPool( ConfigReader::instance().getString("main.database.path") ));
std::unique_ptr<Wt::Dbo::SqlConnectionPool> connectionPool( Database::Handler::createConnectionPool("/var/lms/lms.db")); // TODO use $datadir from autotools
serviceManager.add( std::make_shared<Service::DatabaseUpdateService>(*connectionPool));
@@ -70,37 +63,31 @@ int main(int argc, char* argv[])
serviceManager.add( std::make_shared<Service::LmsAPIService>(*connectionPool));
#endif
Wt::WServer server(argv[0]);
server.setServerConfiguration (argc, argv);
// bind entry point
server.addEntryPoint(Wt::Application, boost::bind(UserInterface::LmsApplication::create, _1, boost::ref(*connectionPool)));
LMS_LOG(MOD_MAIN, SEV_NOTICE) << "Now running...";
// Start underlying services
LMS_LOG(MOD_MAIN, SEV_INFO) << "Starting services...";
serviceManager.start();
// Starting the main server
LMS_LOG(MOD_MAIN, SEV_INFO) << "Starting server...";
server.start();
// Start underlying services
LMS_LOG(MOD_MAIN, SEV_INFO) << "Starting services...";
serviceManager.start();
// Waiting for shutdown command
Wt::WServer::waitForShutdown(argv[0]);
LMS_LOG(MOD_MAIN, SEV_INFO) << "Stopping services...";
serviceManager.stop();
serviceManager.clear();
LMS_LOG(MOD_MAIN, SEV_INFO) << "Stopping server...";
server.stop();
LMS_LOG(MOD_MAIN, SEV_INFO) << "Stopping services...";
serviceManager.stop();
res = EXIT_SUCCESS;
}
// TODO catch setting not found exception
catch( libconfig::ParseException& e)
{
std::cerr << "Caught libconfig::ParseException! error='" << e.getError() << "', file = '" << e.getFile() << "', line = " << e.getLine() << std::endl;
}
catch( Wt::WServer::Exception& e)
{
LMS_LOG(MOD_MAIN, SEV_CRIT) << "Caught a WServer::Exception: " << e.what();
-19
View File
@@ -20,51 +20,32 @@
#include <boost/thread.hpp>
#include "config/ConfigReader.hpp"
#include "logger/Logger.hpp"
#include "DatabaseUpdateService.hpp"
static std::vector<std::string> splitStrings(const std::string& source)
{
std::vector<std::string> res;
std::istringstream oss(source);
std::string str;
while(oss >> str)
res.push_back(str);
return res;
}
namespace Service {
DatabaseUpdateService::DatabaseUpdateService(Wt::Dbo::SqlConnectionPool &connectionPool)
: _metadataParser(),
_databaseUpdater( connectionPool, _metadataParser)
{
_databaseUpdater.setAudioExtensions(splitStrings(ConfigReader::instance().getString("main.database.audio_extensions")));
_databaseUpdater.setVideoExtensions(splitStrings(ConfigReader::instance().getString("main.database.video_extensions")));
}
void
DatabaseUpdateService::start(void)
{
LMS_LOG(MOD_SERVICE, SEV_DEBUG) << "DatabaseUpdateService, starting...";
_databaseUpdater.start();
}
void
DatabaseUpdateService::stop(void)
{
LMS_LOG(MOD_SERVICE, SEV_DEBUG) << "DatabaseUpdateService, stopping...";
_databaseUpdater.stop();
LMS_LOG(MOD_SERVICE, SEV_DEBUG) << "DatabaseUpdateService, stopped";
}
void
DatabaseUpdateService::restart(void)
{
LMS_LOG(MOD_SERVICE, SEV_DEBUG) << "DatabaseUpdateService, restart";
stop();
start();
}
+7 -4
View File
@@ -17,8 +17,6 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "logger/Logger.hpp"
#include "ServiceManager.hpp"
namespace Service {
@@ -36,7 +34,6 @@ ServiceManager::ServiceManager()
ServiceManager::~ServiceManager()
{
LMS_LOG(MOD_SERVICE, SEV_NOTICE) << "Stopping services...";
stop();
}
@@ -54,6 +51,13 @@ ServiceManager::del(Service::pointer service)
_services.erase(service);
}
void
ServiceManager::clear(void)
{
stop();
_services.clear();
}
void
ServiceManager::start(void)
{
@@ -72,7 +76,6 @@ ServiceManager::stop(void)
void
ServiceManager::restart(void)
{
LMS_LOG(MOD_SERVICE, SEV_NOTICE) << "Restarting services...";
for (Service::pointer service : _services)
service->restart();
}
+1
View File
@@ -36,6 +36,7 @@ class ServiceManager
void add(Service::pointer service);
void del(Service::pointer service);
void clear();
void start();
void stop();
+4 -3
View File
@@ -49,7 +49,7 @@ VideoWidget::search(const std::string& searchText)
void
VideoWidget::playVideo(boost::filesystem::path p)
{
LMS_LOG(MOD_UI, SEV_DEBUG) << "Want to play video " << p << "'" << std::endl;
LMS_LOG(MOD_UI, SEV_DEBUG) << "Want to play video " << p << "'";
try {
std::size_t audioBitrate = 0;
@@ -82,7 +82,8 @@ VideoWidget::playVideo(boost::filesystem::path p)
parameters.setBitrate(Transcode::Stream::Video, 0/*videoBitrate*/);
VideoMediaPlayerWidget *mediaPlayer = new VideoMediaPlayerWidget(parameters, this);
mediaPlayer->close().connect(std::bind([=] () {
mediaPlayer->close().connect(std::bind([=] ()
{
_videoDbWidget->setHidden(false);
delete mediaPlayer;
}));
@@ -90,7 +91,7 @@ VideoWidget::playVideo(boost::filesystem::path p)
_videoDbWidget->setHidden(true);
}
catch( std::exception& e) {
LMS_LOG(MOD_UI, SEV_ERROR) << "Caught exception while loading '" << p << "': " << e.what() << std::endl;
LMS_LOG(MOD_UI, SEV_ERROR) << "Caught exception while loading '" << p << "': " << e.what();
}
}