Reworked the project layout

This commit is contained in:
emeric
2014-10-10 20:41:34 +02:00
parent 92a176c899
commit 49ff050539
146 changed files with 156 additions and 162 deletions
+121
View File
@@ -0,0 +1,121 @@
/*
* Copyright (C) 2013 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/foreach.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include </usr/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 "Logger.hpp"
Logger&
Logger::instance()
{
static Logger instance;
return instance;
}
Logger::Logger()
{
// Initialiaz loggers
static const std::vector<Module> modules =
{
MOD_AV,
MOD_COVER,
MOD_DB,
MOD_DBUPDATER,
MOD_MAIN,
MOD_METADATA,
MOD_REMOTE,
MOD_SERVICE,
MOD_TRANSCODE,
MOD_UI,
};
BOOST_FOREACH(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(const Config& config)
{
boost::log::add_common_attributes();
boost::log::register_simple_formatter_factory< Severity, char >("Severity");
if (config.enableFileLogging)
{
boost::log::add_file_log
(
boost::log::keywords::file_name = config.logPath + std::string(".%N"),
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
boost::log::keywords::open_mode = std::ios_base::app,
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
)
);
}
if (config.enableConsoleLogging)
{
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
)
);
}
boost::log::core::get()->set_filter
(
boost::log::expressions::attr<Severity>("Severity") <= config.minSeverity
);
}
+137
View File
@@ -0,0 +1,137 @@
/*
* Copyright (C) 2013 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LOGGER_HPP__
#define LOGGER_HPP__
#include <map>
#include <boost/log/expressions/keyword_fwd.hpp>
#include <boost/log/expressions/keyword.hpp>
#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
{
SEV_CRIT = 2,
SEV_ERROR = 3,
SEV_WARNING = 4,
SEV_NOTICE = 5,
SEV_INFO = 6,
SEV_DEBUG = 7,
};
enum Module
{
MOD_AV = 0,
MOD_COVER,
MOD_DB,
MOD_DBUPDATER,
MOD_MAIN,
MOD_METADATA,
MOD_REMOTE,
MOD_SERVICE,
MOD_TRANSCODE,
MOD_UI,
};
BOOST_LOG_ATTRIBUTE_KEYWORD(module, "Module", Module)
class Logger
{
public:
static Logger& instance();
struct Config {
bool enableFileLogging;
bool enableConsoleLogging;
std::string logPath;
Severity minSeverity;
};
//[ example_tutorial_file_advanced
void init(const Config& config);
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__