Default formatter and common types into common.h

This commit is contained in:
gabi 2014-10-24 01:59:39 +03:00
parent 6080436e8b
commit 319db7bccc
9 changed files with 204 additions and 116 deletions

View File

@ -2,6 +2,7 @@
//
#include <mutex>
#include "c11log/logger.h"
#include "c11log/factory.h"
#include "c11log/sinks/async_sink.h"
#include "c11log/sinks/file_sinks.h"
#include "c11log/sinks/stdout_sinks.h"
@ -12,27 +13,45 @@ using namespace std::chrono;
using namespace c11log;
using namespace utils;
std::ostringstream f1(int i)
{
std::ostringstream oss;
oss << "Hello oss " << i;
return oss;
}
details::fast_oss f2(int i)
{
details::fast_oss oss;
oss << "Hello oss " << i;
return oss;
}
int main(int argc, char* argv[])
{
const unsigned int howmany = argc <= 1 ? 500000 : atoi(argv[1]);
const unsigned int howmany = argc <= 1 ? 600000 : atoi(argv[1]);
std::string pattern = "%z %B %d, %Y %H:%M:%S.%e **************[%n:%l] %t";
//std::string pattern = " [%z] %t";
const std::string pattern = "%+";
auto formatter = std::make_shared<details::pattern_formatter>(pattern);
//logger cout_logger("bench", { std::make_shared<sinks::stderr_sink_mt>() }, pattern);
//logger::default_formatter(formatter);
logger cout_logger("bench", { std::make_shared<sinks::stderr_sink_mt>() });
cout_logger.formatter(formatter);
cout_logger.info() << "Hello logger " << 1234;
logger::default_formatter(formatter);
cout_logger.info() << "Hello logger " << 12.4 << 5 << ',' << 6 << 7 <<8<<9<<10;
auto nullsink = std::make_shared<sinks::null_sink_st>();
auto rotating = std::make_shared<sinks::rotating_file_sink_mt>("myrotating", "txt", 1024 * 1024 * 5, 5, 100);
auto rotating = std::make_shared<sinks::rotating_file_sink_st>("myrotating", "txt", 1024 * 1024 * 5, 20, 100);
logger my_logger("my_logger", { nullsink });
logger my_logger("my_logger", { nullsink }, formatter);
auto start = system_clock::now();
for (unsigned int i = 1; i <= howmany; ++i)
{
my_logger.info() << "Hello logger: msg #" << i;
}
auto delta = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count();

View File

@ -17,7 +17,7 @@ details::fast_oss f(const std::string& what)
oss << what;
return oss;
}
int main(int, char* [])
int main_(int, char* [])
{
auto foss = f("test2");

View File

@ -11,7 +11,7 @@ namespace utils
{
template<typename T>
std::string format(const T& value)
inline std::string format(const T& value)
{
static std::locale loc("");
std::stringstream ss;
@ -21,7 +21,7 @@ std::string format(const T& value)
}
template<>
std::string format(const double & value)
inline std::string format(const double & value)
{
static std::locale loc("");
std::stringstream ss;

View File

@ -1,9 +1,23 @@
#pragma once
#include<initializer_list>
#include<memory>
#include<chrono>
namespace c11log
{
class formatter;
namespace sinks {
class sink;
}
// Common types across the lib
using log_clock = std::chrono::system_clock;
using sink_ptr = std::shared_ptr < sinks::sink > ;
using sinks_init_list = std::initializer_list < sink_ptr > ;
using formatter_ptr = std::shared_ptr<c11log::formatter>;
//Log level enum
namespace level
{
typedef enum

View File

@ -298,8 +298,6 @@ private:
class t_formatter :public flag_formatter
{
void format(details::log_msg& msg) override
@ -340,6 +338,40 @@ private:
std::string _str;
};
// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %t
class full_formatter :public flag_formatter
{
void format(details::log_msg& msg) override
{
msg.formatted.putc('[');
msg.formatted.put_int(msg.tm_time.tm_year+1900, 4);
msg.formatted.putc('-');
msg.formatted.put_int(msg.tm_time.tm_mon+ 1, 2);
msg.formatted.putc('-');
msg.formatted.put_int(msg.tm_time.tm_mday, 2);
msg.formatted.putc(' ');
msg.formatted.put_int(msg.tm_time.tm_hour, 2);
msg.formatted.putc(':');
msg.formatted.put_int(msg.tm_time.tm_min, 2);
msg.formatted.putc(':');
msg.formatted.put_int(msg.tm_time.tm_sec, 2);
//millis
msg.formatted.putc('.');
auto duration = msg.time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
msg.formatted.put_int(static_cast<int>(millis), 3);
msg.formatted.putc(']');
msg.formatted << " [";
msg.formatted << msg.logger_name;
msg.formatted << "] [";
msg.formatted << level::to_str(msg.level);
msg.formatted << "] ";
msg.formatted.put_fast_oss(msg.raw);
}
};
class pattern_formatter : public formatter
@ -494,6 +526,10 @@ inline void c11log::details::pattern_formatter::handle_flag(char flag)
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::z_formatter()));
break;
case ('+'):
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::full_formatter()));
break;
default: //Unkown flag appears as is
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter('%')));
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::ch_formatter(flag)));

View File

@ -11,85 +11,96 @@ namespace c11log
{
namespace factory
{
//
//
//std::shared_ptr<logger> get(const std::string& name);
//std::shared_ptr<logger> create(const std::string& name, sinks_init_list , formatter_ptr);
//std::shared_ptr<logger> create(const std::string& name, sinks_init_list, string format);
//template<class It>
//std::shared_ptr<logger> create (const std::string& name, const It& begin, const It& end);
//
// console loggers single/multi threaded
//
std::unique_ptr<logger> stdout_logger(const std::string& name = "")
{
auto sink = std::make_shared<sinks::stderr_sink_st>();
return std::unique_ptr<logger>(new logger(name, { sink }));
}
std::unique_ptr<logger> stdout_logger_mt(const std::string& name = "")
{
auto sink = std::make_shared<sinks::stderr_sink_mt>();
return std::unique_ptr<logger>(new logger(name, { sink }));
}
////
//// console loggers single/multi threaded
////
//std::unique_ptr<logger> stdout_logger(const std::string& name = "")
//{
// auto sink = std::make_shared<sinks::stderr_sink_st>();
// return std::unique_ptr<logger>(new logger(name, { sink }));
//}
//
// simple file logger single/multi threaded
//std::unique_ptr<logger> stdout_logger_mt(const std::string& name = "")
//{
// auto sink = std::make_shared<sinks::stderr_sink_mt>();
// return std::unique_ptr<logger>(new logger(name, { sink }));
//}
//
std::unique_ptr<logger> simple_file_logger(const std::string& filename, const std::string& logger_name = "" )
{
auto fsink = std::make_shared<sinks::simple_file_sink_st>(filename);
return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
}
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 }));
}
////
//// simple file logger single/multi threaded
////
//std::unique_ptr<logger> simple_file_logger(const std::string& filename, const std::string& logger_name = "" )
//{
// auto fsink = std::make_shared<sinks::simple_file_sink_st>(filename);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//
// daily file logger single/multi threaded
//}
//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 }));
//}
//
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 = "")
{
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 }));
}
////
//// 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 = "")
//{
// 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 }));
//}
//
// rotating file logger single/multi threaded
//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 }));
//}
//
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 }));
}
////
//// 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

@ -6,7 +6,7 @@ namespace c11log
class formatter
{
public:
virtual formatter() {}
virtual ~formatter() {}
virtual void format(details::log_msg& msg) = 0;
};
}

View File

@ -14,7 +14,6 @@
#include "sinks/base_sink.h"
#include "common.h"
#include "formatter.h"
#include "details/pattern_formatter.h"
namespace c11log
@ -28,13 +27,21 @@ class line_logger;
class logger
{
public:
using sink_ptr = std::shared_ptr<sinks::sink>;
using formatter_ptr = std::shared_ptr<c11log::formatter>;
logger(const std::string& name, std::initializer_list<sink_ptr>, formatter_ptr formatter = nullptr);
logger(const std::string& name, sinks_init_list);
template<class It>
logger(const std::string& name, const It& begin, const It& end);
//get/set default formatter
static formatter_ptr& default_formatter(formatter_ptr formatter = nullptr);
void set_formatter(formatter_ptr);
formatter_ptr get_formatter() const;
logger(const logger&) = delete;
logger& operator=(const logger&) = delete;
@ -44,10 +51,6 @@ public:
const std::string& name() const;
bool should_log(level::level_enum) const;
void formatter(formatter_ptr);
formatter_ptr formatter() const;
template <typename... Args> details::line_logger log(level::level_enum lvl, const Args&... args);
template <typename... Args> details::line_logger trace(const Args&... args);
template <typename... Args> details::line_logger debug(const Args&... args);
@ -66,8 +69,6 @@ private:
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);
formatter_ptr _default_formatter();
const char* _default_pattern = "[%Y:%m:%d %H:%M:%S.%e] [%n:%l] %t";
};
}
@ -93,25 +94,39 @@ private:
#include "details/line_logger.h"
inline c11log::logger::logger(const std::string& logger_name, std::initializer_list<sink_ptr> sinks_list, formatter_ptr msg_formatter) :
inline c11log::logger::logger(const std::string& logger_name, sinks_init_list sinks_list):
_name(logger_name),
_formatter(msg_formatter),
_sinks(sinks_list)
{
if (!msg_formatter) //default formatter
_formatter = std::make_shared<details::pattern_formatter>(_default_pattern);
// no support under vs2013 for member initialization for std::atomic
_level = level::INFO;
}
template<class It>
inline c11log::logger::logger(const std::string& logger_name, const It& begin, const It& end):
inline c11log::logger::logger(const std::string& logger_name, const It& begin, const It& end) :
_name(logger_name),
_sinks(begin, end)
{}
inline void c11log::logger::set_formatter(c11log::formatter_ptr msg_formatter)
{
_formatter = msg_formatter;
}
inline c11log::formatter_ptr c11log::logger::get_formatter() const
{
return _formatter;
}
inline c11log::formatter_ptr& c11log::logger::default_formatter(formatter_ptr formatter)
{
static formatter_ptr g_default_formatter = std::make_shared<details::pattern_formatter>("[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %t");
if (formatter)
g_default_formatter = formatter;
return g_default_formatter;
}
template <typename... Args>
inline c11log::details::line_logger c11log::logger::log(level::level_enum lvl, const Args&... args) {
bool msg_enabled = should_log(lvl);
@ -177,15 +192,7 @@ inline bool c11log::logger::should_log(c11log::level::level_enum msg_level) cons
return msg_level >= _level.load();
}
inline void c11log::logger::formatter(formatter_ptr msg_formatter)
{
_formatter = msg_formatter;
}
inline c11log::logger::formatter_ptr c11log::logger::formatter() const
{
return _formatter;
}
inline void c11log::logger::_variadic_log(c11log::details::line_logger&) {}
@ -199,7 +206,8 @@ void c11log::logger::_variadic_log(c11log::details::line_logger& l, const First&
inline void c11log::logger::_log_msg(details::log_msg& msg)
{
_formatter->format(msg);
auto& formatter = _formatter ? _formatter : logger::default_formatter();
formatter->format(msg);
for (auto &sink : _sinks)
sink->log(msg);
}

View File

@ -27,8 +27,8 @@ public:
//Stop logging and join the back thread
~async_sink();
void add_sink(logger::sink_ptr sink);
void remove_sink(logger::sink_ptr sink_ptr);
void add_sink(sink_ptr sink);
void remove_sink(sink_ptr sink_ptr);
q_type& q();
//Wait to remaining items (if any) in the queue to be written and shutdown
void shutdown(const std::chrono::milliseconds& timeout);
@ -90,14 +90,14 @@ inline void c11log::sinks::async_sink::_thread_loop()
}
}
inline void c11log::sinks::async_sink::add_sink(logger::sink_ptr s)
inline void c11log::sinks::async_sink::add_sink(c11log::sink_ptr s)
{
std::lock_guard<std::mutex> guard(_mutex);
_sinks.push_back(s);
}
inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr s)
inline void c11log::sinks::async_sink::remove_sink(c11log::sink_ptr s)
{
std::lock_guard<std::mutex> guard(_mutex);
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), s), _sinks.end());