Start of move to variadic template functions in logger

This commit is contained in:
gabi 2014-10-12 04:38:06 +03:00
parent 535e332dc1
commit f9425bb666
5 changed files with 148 additions and 91 deletions

View File

@ -14,7 +14,6 @@ typedef enum
WARNING,
ERROR,
CRITICAL,
FATAL,
NONE = 99
} level_enum;

View File

@ -46,7 +46,7 @@ public:
_log_msg.logger_name = _callback_logger->name();
_log_msg.time = log_clock::now();
_log_msg.raw = _oss.str();
_callback_logger->_log_it(_log_msg);
_callback_logger->_log_msg(_log_msg);
}
}

View File

@ -1,60 +1,95 @@
#pragma once
#include <unordered_map>
#include <string>
#include <memory>
#include <mutex>
#include "logger.h"
#include "sinks/file_sinks.h"
#include "sinks/stdout_sinks.h"
//
// logger creation shotcuts
//
namespace c11log
{
namespace details
namespace factory
{
class factory
//
// console loggers single/multi threaded
//
std::unique_ptr<logger> stdout_logger(const std::string& name = "")
{
public:
using logger_ptr = std::shared_ptr<c11log::logger>;
using logger_map = std::unordered_map<std::string, logger_ptr>;
std::shared_ptr<logger> create_logger(const std::string& name, logger::sinks_init_list, logger::formatter_ptr = nullptr);
logger_ptr get_logger(const std::string &name);
static factory& instance();
private:
std::mutex _factory_mutex;
logger_map _factory;
};
}
auto sink = std::make_shared<sinks::stderr_sink_st>();
return std::unique_ptr<logger>(new logger(name, sink));
}
inline std::shared_ptr<c11log::logger> c11log::details::factory::create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter)
std::unique_ptr<logger> stdout_logger_mt(const std::string& name = "")
{
std::lock_guard<std::mutex> lock(_factory_mutex);
logger_ptr logger_p = std::make_shared<logger>(name, sinks, std::move(formatter));
_factory.insert(logger_map::value_type(name, logger_p));
return logger_p;
auto sink = std::make_shared<sinks::stderr_sink_mt>();
return std::unique_ptr<logger>(new logger(name, sink));
}
inline c11log::details::factory::logger_ptr c11log::details::factory::get_logger(const std::string &name)
//
// simple file logger single/multi threaded
//
std::unique_ptr<logger> simple_file_logger(const std::string& filename, const std::string& logger_name = "" )
{
std::lock_guard<std::mutex> lock(_factory_mutex);
auto fsink = std::make_shared<sinks::simple_file_sink_st>(filename);
return std::unique_ptr<logger>(new c11log::logger(logger_name, fsink));
auto found = _factory.find(name);
if (found != _factory.end())
return found->second;
else
return logger_ptr(nullptr);
}
std::unique_ptr<logger> simple_file_logger_mt(const std::string& filename, const std::string& logger_name = "")
{
auto fsink = std::make_shared<sinks::simple_file_sink_mt>(filename);
return std::unique_ptr<logger>(new c11log::logger(logger_name, fsink));
}
inline c11log::details::factory & c11log::details::factory::instance()
//
// daily file logger single/multi threaded
//
std::unique_ptr<logger> daily_file_logger(
const std::string &filename,
const std::string &extension,
const std::size_t flush_every,
const std::string& logger_name = "")
{
static factory instance;
return instance;
auto fsink = std::make_shared<sinks::daily_file_sink_st>(filename, extension, flush_every);
return std::unique_ptr<logger>(new c11log::logger(logger_name, fsink));
}
std::unique_ptr<logger> daily_file_logger_mt(
const std::string &filename,
const std::string &extension,
const std::size_t flush_every,
const std::string& logger_name = "")
{
auto fsink = std::make_shared<sinks::daily_file_sink_mt>(filename, extension, flush_every);
return std::unique_ptr<logger>(new c11log::logger(logger_name, fsink));
}
//
// rotating file logger single/multi threaded
//
std::unique_ptr<logger> rotating_file_logger(
const std::string &filename,
const std::string &extension,
const std::size_t max_size,
const std::size_t max_files,
const std::size_t flush_every,
const std::string& logger_name = "")
{
auto fsink = std::make_shared<sinks::rotating_file_sink_st>(filename, extension, max_size, max_files, flush_every);
return std::unique_ptr<logger>(new c11log::logger(logger_name, fsink));
}
std::unique_ptr<logger> rotating_file_logger_mt(
const std::string &filename,
const std::string &extension,
const std::size_t max_size,
const std::size_t max_files,
const std::size_t flush_every,
const std::string& logger_name = "")
{
auto fsink = std::make_shared<sinks::rotating_file_sink_mt>(filename, extension, max_size, max_files, flush_every);
return std::unique_ptr<logger>(new c11log::logger(logger_name, fsink));
}
} // ns factory
} // ns c11log

View File

@ -32,10 +32,13 @@ public:
using formatter_ptr = std::unique_ptr<formatters::formatter>;
logger(const std::string& name, sinks_init_list, formatter_ptr = nullptr);
template<class It>
logger(const std::string& name, It begin, It end, formatter_ptr = nullptr);
logger(const std::string& name, sink_ptr, formatter_ptr = nullptr);
~logger() = default;
logger(const logger&) = delete;
logger& operator=(const logger&) = delete;
~logger() = default;
void level(level::level_enum);
level::level_enum level() const;
@ -43,21 +46,25 @@ public:
const std::string& name() const;
bool should_log(level::level_enum) const;
template <typename... Args> details::line_logger log(level::level_enum lvl, const Args&... args);
template<typename T> details::line_logger trace(const T&);
template<typename T> details::line_logger debug(const T&);
template<typename T> details::line_logger info(const T&);
template <typename... Args> details::line_logger info(const Args&... args);
template<typename T> details::line_logger warn(const T&);
template<typename T> details::line_logger error(const T&);
template<typename T> details::line_logger critical(const T&);
template<typename T> details::line_logger fatal(const T&);
details::line_logger trace();
details::line_logger debug();
details::line_logger info();
details::line_logger warn();
details::line_logger error();
details::line_logger& error();
details::line_logger critical();
details::line_logger fatal();
private:
friend details::line_logger;
@ -66,31 +73,35 @@ private:
sinks_vector_t _sinks;
std::atomic_int _level;
void _log_it(details::log_msg& msg);
void _variadic_log(details::line_logger& l);
template <typename First, typename... Rest>
void _variadic_log(details::line_logger&l, const First& first, const Rest&... rest);
void _log_msg(details::log_msg& msg);
};
//std::shared_ptr<c11log::logger> create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter = nullptr);
//std::shared_ptr<logger> get_logger(const std::string& name);
}
//
// trace & debug macros
//
#ifdef FFLOG_ENABLE_TRACE
#define FFLOG_TRACE(logger, ...) logger->log(c11log::level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__)
#else
#define FFLOG_TRACE(logger, ...) {}
#endif
#ifdef FFLOG_ENABLE_DEBUG
#define FFLOG_DEBUG(logger, ...) logger->log(c11log::level::DEBUG, __VA_ARGS__)
#else
#define FFLOG_DEBUG(logger, ...) {}
#endif
//
// Logger implementation
//
#include "details/line_logger.h"
#include "details/factory.h"
/*
inline std::shared_ptr<c11log::logger> c11log::create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter)
{
return details::factory::instance().create_logger(name, sinks, std::move(formatter));
}
inline std::shared_ptr<c11log::logger> c11log::get_logger(const std::string& name)
{
return details::factory::instance().get_logger(name);
}*/
inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_list, formatter_ptr f) :
@ -105,6 +116,15 @@ inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_lis
}
template<class It>
inline c11log::logger::logger(const std::string& name, It begin, It end, formatter_ptr f):
_name(name),
_formatter(std::move(f)),
_sinks(begin, end)
{
}
inline c11log::logger::logger(const std::string& name, sink_ptr sink, formatter_ptr f) :
logger(name, {sink}, std::move(f)) {}
@ -121,19 +141,26 @@ inline c11log::details::line_logger c11log::logger::trace(const T& msg)
template<typename T>
inline c11log::details::line_logger c11log::logger::debug(const T& msg)
{
details::line_logger l(this, level::DEBUG, should_log(level::DEBUG));
c11log::details::line_logger l(this, level::DEBUG, should_log(level::DEBUG));
l.write(msg);
return l;
}
template<typename T>
inline c11log::details::line_logger c11log::logger::info(const T& msg)
{
details::line_logger l(this, level::INFO, should_log(level::INFO));
l.write(msg);
template <typename... Args>
inline c11log::details::line_logger c11log::logger::log(level::level_enum lvl, const Args&... args) {
details::line_logger l(this, lvl, true);
_variadic_log(l, args...);
return l;
}
template <typename... Args>
inline c11log::details::line_logger c11log::logger::info(const Args&... args) {
details::line_logger l(this, level::INFO, should_log(level::INFO));
_variadic_log(l, args...);
return l;
}
template<typename T>
inline c11log::details::line_logger c11log::logger::warn(const T& msg)
{
@ -150,13 +177,7 @@ inline c11log::details::line_logger c11log::logger::critical(const T& msg)
return l;
}
template<typename T>
inline c11log::details::line_logger c11log::logger::fatal(const T& msg)
{
details::line_logger l(this, level::FATAL, should_log(level::FATAL));
l.write(msg);
return l;
}
inline c11log::details::line_logger c11log::logger::trace()
@ -169,10 +190,6 @@ inline c11log::details::line_logger c11log::logger::debug()
return details::line_logger(this, level::DEBUG, should_log(level::DEBUG));
}
inline c11log::details::line_logger c11log::logger::info()
{
return details::line_logger(this, level::INFO, should_log(level::INFO));
}
inline c11log::details::line_logger c11log::logger::warn()
{
@ -184,11 +201,6 @@ inline c11log::details::line_logger c11log::logger::critical()
return details::line_logger(this, level::CRITICAL, should_log(level::CRITICAL));
}
inline c11log::details::line_logger c11log::logger::fatal()
{
return details::line_logger(this, level::FATAL, should_log(level::FATAL));
}
inline const std::string& c11log::logger::name() const
{
@ -210,7 +222,17 @@ inline bool c11log::logger::should_log(c11log::level::level_enum level) const
return level >= _level.load();
}
inline void c11log::logger::_log_it(details::log_msg& msg)
inline void c11log::logger::_variadic_log(c11log::details::line_logger& l) {}
template <typename First, typename... Rest>
void c11log::logger::_variadic_log(c11log::details::line_logger& l, const First& first, const Rest&... rest)
{
l.write(first);
_variadic_log(l, rest...);
}
inline void c11log::logger::_log_msg(details::log_msg& msg)
{
_formatter->format(msg);
for (auto &sink : _sinks)

View File

@ -17,8 +17,9 @@ public:
stdout_sink() : ostream_sink<Mutex>(std::cout) {}
};
typedef stdout_sink<std::mutex> stdout_sink_mt;
typedef stdout_sink<details::null_mutex> stdout_sink_st;
typedef stdout_sink<std::mutex> stdout_sink_mt;
template <class Mutex>
class stderr_sink : public ostream_sink<Mutex>