From 319db7bccc28bbe06f3415772f33ee07ef06323b Mon Sep 17 00:00:00 2001 From: gabi Date: Fri, 24 Oct 2014 01:59:39 +0300 Subject: [PATCH] Default formatter and common types into common.h --- example/bench.cpp | 37 +++-- example/example.cpp | 2 +- example/utils.h | 4 +- include/c11log/common.h | 14 ++ include/c11log/details/pattern_formatter.h | 40 +++++- include/c11log/factory.h | 153 +++++++++++---------- include/c11log/formatter.h | 2 +- include/c11log/logger.h | 60 ++++---- include/c11log/sinks/async_sink.h | 8 +- 9 files changed, 204 insertions(+), 116 deletions(-) diff --git a/example/bench.cpp b/example/bench.cpp index 862e26c4..c15ee905 100644 --- a/example/bench.cpp +++ b/example/bench.cpp @@ -2,6 +2,7 @@ // #include #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(pattern); - + //logger cout_logger("bench", { std::make_shared() }, pattern); + //logger::default_formatter(formatter); logger cout_logger("bench", { std::make_shared() }); - 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(); - auto rotating = std::make_shared("myrotating", "txt", 1024 * 1024 * 5, 5, 100); + auto rotating = std::make_shared("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> (delta).count(); diff --git a/example/example.cpp b/example/example.cpp index d4b04b16..55b17ed8 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -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"); diff --git a/example/utils.h b/example/utils.h index 64d440ad..9b7b43f4 100644 --- a/example/utils.h +++ b/example/utils.h @@ -11,7 +11,7 @@ namespace utils { template -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; diff --git a/include/c11log/common.h b/include/c11log/common.h index c34819c6..cfcc0430 100644 --- a/include/c11log/common.h +++ b/include/c11log/common.h @@ -1,9 +1,23 @@ #pragma once +#include +#include +#include + 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; + +//Log level enum namespace level { typedef enum diff --git a/include/c11log/details/pattern_formatter.h b/include/c11log/details/pattern_formatter.h index c096456d..70f6dfeb 100644 --- a/include/c11log/details/pattern_formatter.h +++ b/include/c11log/details/pattern_formatter.h @@ -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(duration).count() % 1000; + msg.formatted.put_int(static_cast(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(new details::z_formatter())); break; + case ('+'): + _formatters.push_back(std::unique_ptr(new details::full_formatter())); + break; + default: //Unkown flag appears as is _formatters.push_back(std::unique_ptr(new details::ch_formatter('%'))); _formatters.push_back(std::unique_ptr(new details::ch_formatter(flag))); diff --git a/include/c11log/factory.h b/include/c11log/factory.h index 309a7098..bb184f3f 100644 --- a/include/c11log/factory.h +++ b/include/c11log/factory.h @@ -11,85 +11,96 @@ namespace c11log { namespace factory { +// +// +//std::shared_ptr get(const std::string& name); +//std::shared_ptr create(const std::string& name, sinks_init_list , formatter_ptr); +//std::shared_ptr create(const std::string& name, sinks_init_list, string format); +//template +//std::shared_ptr create (const std::string& name, const It& begin, const It& end); + + // -// console loggers single/multi threaded // -std::unique_ptr stdout_logger(const std::string& name = "") -{ - auto sink = std::make_shared(); - return std::unique_ptr(new logger(name, { sink })); -} - -std::unique_ptr stdout_logger_mt(const std::string& name = "") -{ - auto sink = std::make_shared(); - return std::unique_ptr(new logger(name, { sink })); -} - +//// +//// console loggers single/multi threaded +//// +//std::unique_ptr stdout_logger(const std::string& name = "") +//{ +// auto sink = std::make_shared(); +// return std::unique_ptr(new logger(name, { sink })); +//} // -// simple file logger single/multi threaded +//std::unique_ptr stdout_logger_mt(const std::string& name = "") +//{ +// auto sink = std::make_shared(); +// return std::unique_ptr(new logger(name, { sink })); +//} // -std::unique_ptr simple_file_logger(const std::string& filename, const std::string& logger_name = "" ) -{ - auto fsink = std::make_shared(filename); - return std::unique_ptr(new c11log::logger(logger_name, { fsink })); - -} -std::unique_ptr simple_file_logger_mt(const std::string& filename, const std::string& logger_name = "") -{ - auto fsink = std::make_shared(filename); - return std::unique_ptr(new c11log::logger(logger_name, { fsink })); -} - +//// +//// simple file logger single/multi threaded +//// +//std::unique_ptr simple_file_logger(const std::string& filename, const std::string& logger_name = "" ) +//{ +// auto fsink = std::make_shared(filename); +// return std::unique_ptr(new c11log::logger(logger_name, { fsink })); // -// daily file logger single/multi threaded +//} +//std::unique_ptr simple_file_logger_mt(const std::string& filename, const std::string& logger_name = "") +//{ +// auto fsink = std::make_shared(filename); +// return std::unique_ptr(new c11log::logger(logger_name, { fsink })); +//} // -std::unique_ptr 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(filename, extension, flush_every); - return std::unique_ptr(new c11log::logger(logger_name, { fsink })); -} - -std::unique_ptr 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(filename, extension, flush_every); - return std::unique_ptr(new c11log::logger(logger_name, { fsink })); -} - +//// +//// daily file logger single/multi threaded +//// +//std::unique_ptr 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(filename, extension, flush_every); +// return std::unique_ptr(new c11log::logger(logger_name, { fsink })); +//} // -// rotating file logger single/multi threaded +//std::unique_ptr 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(filename, extension, flush_every); +// return std::unique_ptr(new c11log::logger(logger_name, { fsink })); +//} // -std::unique_ptr 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(filename, extension, max_size, max_files, flush_every); - return std::unique_ptr(new c11log::logger(logger_name, { fsink })); -} - -std::unique_ptr 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(filename, extension, max_size, max_files, flush_every); - return std::unique_ptr(new c11log::logger(logger_name, { fsink })); -} +//// +//// rotating file logger single/multi threaded +//// +//std::unique_ptr 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(filename, extension, max_size, max_files, flush_every); +// return std::unique_ptr(new c11log::logger(logger_name, { fsink })); +//} +// +//std::unique_ptr 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(filename, extension, max_size, max_files, flush_every); +// return std::unique_ptr(new c11log::logger(logger_name, { fsink })); +//} } // ns factory } // ns c11log diff --git a/include/c11log/formatter.h b/include/c11log/formatter.h index ee2db989..9651729f 100644 --- a/include/c11log/formatter.h +++ b/include/c11log/formatter.h @@ -6,7 +6,7 @@ namespace c11log class formatter { public: - virtual formatter() {} + virtual ~formatter() {} virtual void format(details::log_msg& msg) = 0; }; } diff --git a/include/c11log/logger.h b/include/c11log/logger.h index 29a229e7..a6857a87 100644 --- a/include/c11log/logger.h +++ b/include/c11log/logger.h @@ -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; - using formatter_ptr = std::shared_ptr; - logger(const std::string& name, std::initializer_list, formatter_ptr formatter = nullptr); + logger(const std::string& name, sinks_init_list); template 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 details::line_logger log(level::level_enum lvl, const Args&... args); template details::line_logger trace(const Args&... args); template details::line_logger debug(const Args&... args); @@ -66,8 +69,6 @@ private: template 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 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(_default_pattern); - // no support under vs2013 for member initialization for std::atomic _level = level::INFO; - } template -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("[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %t"); + if (formatter) + g_default_formatter = formatter; + return g_default_formatter; +} + template 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); } diff --git a/include/c11log/sinks/async_sink.h b/include/c11log/sinks/async_sink.h index ea0c504c..3415c630 100644 --- a/include/c11log/sinks/async_sink.h +++ b/include/c11log/sinks/async_sink.h @@ -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 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 guard(_mutex); _sinks.erase(std::remove(_sinks.begin(), _sinks.end(), s), _sinks.end());