From 497d511746ff3a9e1113b9bea484afa1ab11de1b Mon Sep 17 00:00:00 2001 From: gabi Date: Fri, 9 May 2014 15:27:06 +0300 Subject: [PATCH] log_msg refactoring and raw message support --- example/example.cpp | 16 +++---- include/c11log/details/line_logger.h | 28 +++++------ include/c11log/details/log_msg.h | 71 ++++++++++++++-------------- include/c11log/details/os.h | 24 ++++++---- include/c11log/formatter.h | 38 ++++++++------- include/c11log/logger.h | 12 ++--- include/c11log/sinks/async_sink.h | 4 +- include/c11log/sinks/console_sinks.h | 2 +- include/c11log/sinks/file_sinks.h | 13 ++--- 9 files changed, 106 insertions(+), 102 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index ed825cc6..11afa9a3 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -17,16 +17,14 @@ using namespace utils; int main(int argc, char* argv[]) { - - const unsigned int howmany = argc <= 1 ? 4000000:atoi(argv[1]); - logger cout_logger ("", sinks::stdout_sink()); - cout_logger.set_min_level(c11log::level::TRACE); - cout_logger.info() << "Hello " << "man" << 123; + const unsigned int howmany = argc <= 1 ? 1000000:atoi(argv[1]); + logger cout_logger ("example", sinks::stdout_sink()); + cout_logger.info() << "Hello logger"; - auto fsink = std::make_shared("log", "txt", 1024*1024*50 , 5, 0); auto nullsink = sinks::null_sink::get(); + //auto fsink = std::make_shared("log", "txt", 1024*1024*50 , 5, 0); //auto as = std::make_shared(1000); //as->add_sink(fsink); @@ -34,10 +32,10 @@ int main(int argc, char* argv[]) auto start = system_clock::now(); - for(unsigned int i = 1; i <= howmany ; ++i) - my_logger.info("Hello logger: ") << 4.5 <<'\t' << i << "\tasdasd:" << 123 << 'f'; + for (unsigned int i = 1; i <= howmany; ++i) + my_logger.info("Hello logger: msg #") << i << " дан жд йтбег??"; - //as->shutdown(std::chrono::milliseconds(15000)); + //as->shutdown(std::chrono::milliseconds(15000)); auto delta = system_clock::now() - start; auto delta_d = duration_cast> (delta).count(); diff --git a/include/c11log/details/line_logger.h b/include/c11log/details/line_logger.h index 03d47e5c..0faa5fab 100644 --- a/include/c11log/details/line_logger.h +++ b/include/c11log/details/line_logger.h @@ -26,12 +26,7 @@ public: { if(enabled) { - _log_msg.msg_time = log_clock::now(); - callback_logger->_formatter->format_header(callback_logger->_logger_name, - _log_msg.msg_level, - _log_msg.msg_time, - _oss); - _log_msg.msg_header_size = _oss.str().size(); + _log_msg.time = log_clock::now(); } } @@ -46,10 +41,10 @@ public: _log_msg(std::move(other._log_msg)), _oss(std::move(other._oss.str())), _enabled(other._enabled), - _empty(other._empty) - { - other.disable(); - } + _empty(other._empty) + { + other.disable(); + } @@ -58,8 +53,7 @@ public: //only if enabled and not empty if (_enabled && !_empty) { - _oss << os::eol(); - _log_msg.str = _oss.str(); + _log_msg.raw = _oss.str(); _callback_logger->_log_it(_log_msg); } } @@ -81,10 +75,10 @@ public: return *this; } - void disable() - { - _enabled = false; - } + void disable() + { + _enabled = false; + } @@ -92,7 +86,7 @@ private: logger* _callback_logger; log_msg _log_msg; //details::stack_oss _oss; - std::ostringstream _oss; + std::ostringstream _oss; bool _enabled; bool _empty; }; diff --git a/include/c11log/details/log_msg.h b/include/c11log/details/log_msg.h index 85fda0ba..7b93e78d 100644 --- a/include/c11log/details/log_msg.h +++ b/include/c11log/details/log_msg.h @@ -8,51 +8,50 @@ struct log_msg { log_msg() = default; log_msg(level::level_enum l): - msg_level(l), - msg_time(), - msg_header_size(0), - str() {} + level(l), + time(), + raw(), + formatted() {} - log_msg(const log_msg& other): - msg_level(other.msg_level), - msg_time(other.msg_time), - msg_header_size(other.msg_header_size), - str(other.str) {} + log_msg(const log_msg& other): + level(other.level), + time(other.time), + raw(other.raw), + formatted(other.formatted) {} - log_msg(log_msg&& other):log_msg() - { - swap(*this, other); - } + log_msg(log_msg&& other) + { + swap(*this, other); + } - friend void swap(log_msg& l, log_msg& r) - { - using std::swap; - swap(l.msg_level, r.msg_level); - swap(l.msg_time, r.msg_time); - swap(l.msg_header_size, r.msg_header_size); - swap(l.str, r.str); - } + friend void swap(log_msg& l, log_msg& r) + { + using std::swap; + swap(l.level, r.level); + swap(l.time, r.time); + swap(l.raw, r.raw); + swap(l.formatted, r.formatted); + } - - log_msg& operator=(log_msg other) - { - swap(*this, other); - return *this; - } + log_msg& operator=(log_msg other) + { + swap(*this, other); + return *this; + } - void clear() - { - msg_header_size = 0; - str.clear(); - } + void clear() + { + raw.clear(); + formatted.clear(); + } - level::level_enum msg_level; - log_clock::time_point msg_time; - std::size_t msg_header_size; - std::string str; + level::level_enum level; + log_clock::time_point time; + std::string raw; + std::string formatted; }; } diff --git a/include/c11log/details/os.h b/include/c11log/details/os.h index 0e66950a..3e3556ee 100644 --- a/include/c11log/details/os.h +++ b/include/c11log/details/os.h @@ -44,23 +44,31 @@ inline bool operator!=(const std::tm& tm1, const std::tm& tm2) return !(tm1==tm2); } +#ifdef _WIN32 +inline const char* eol() +{ + return "\r\n"; +} +#else constexpr inline const char* eol() { -#ifdef _WIN32 - return "\r\n"; -#else return "\n"; -#endif } +#endif +#ifdef _WIN32 +inline unsigned short eol_size() +{ + return 2; +} +#else constexpr inline unsigned short eol_size() { -#ifdef _WIN32 - return 2; -#else + return 1; -#endif } +#endif + } //os } //details } //c11log diff --git a/include/c11log/formatter.h b/include/c11log/formatter.h index 7bc76395..6f436a31 100644 --- a/include/c11log/formatter.h +++ b/include/c11log/formatter.h @@ -2,14 +2,15 @@ #include #include -#include #include #include #include #include + #include "common_types.h" #include "details/os.h" +#include "details/log_msg.h" namespace c11log @@ -20,7 +21,7 @@ namespace formatters class formatter { public: - virtual void format_header(const std::string& logger_name, level::level_enum level, const log_clock::time_point& tp, std::ostream& output) = 0; + virtual void format(const std::string& logger_name, details::log_msg& msg) = 0; }; @@ -28,17 +29,20 @@ class default_formatter: public formatter { public: // Format: [2013-12-29 01:04:42.900] [logger_name:Info] Message body - void format_header(const std::string& logger_name, level::level_enum level, const log_clock::time_point& tp, std::ostream& output) override + void format(const std::string& logger_name, details::log_msg& msg) override { - _format_time(tp, output); + std::ostringstream oss; + //Format datetime like this:[2014 - 03 - 14 17:15 : 22] + _format_time(msg.time, oss); if(!logger_name.empty()) - output << " [" << logger_name << ':' << c11log::level::to_str(level) << "] "; + oss << " [" << logger_name << ':' << c11log::level::to_str(msg.level) << "] "; else - output << " [" << c11log::level::to_str(level) << "] "; - + oss << " [" << c11log::level::to_str(msg.level) << "] "; + oss << msg.raw << details::os::eol(); + msg.formatted = oss.str(); } private: - void _format_time(const log_clock::time_point& tp, std::ostream &output); + void _format_time(const log_clock::time_point& tp, std::ostream& output); }; @@ -52,12 +56,13 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock: using namespace c11log::details::os; using namespace std::chrono; -#ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword - __declspec(thread) static char s_cache_str[64]; - __declspec(thread) static size_t s_cache_size; +#ifdef _WIN32 //VS2013 doesn't support yet thread_local keyword + __declspec(thread) static char s_cache_timestr[128]; + __declspec(thread) static int s_cache_timesize = 0; __declspec(thread) static std::time_t s_cache_time_t = 0; #else - thread_local static std::string s_cache_timestr; + thread_local static char s_cache_timestr[128]; + thread_local static int s_cache_timesize = 0; thread_local static std::time_t s_cache_time_t = 0; #endif @@ -81,10 +86,9 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock: time_oss << tm_now.tm_sec << ']'; //Cache the resulted string and its size s_cache_time_t = tp_time_t; - //const std::string &s = time_oss.str_ref(); - s_cache_timestr = time_oss.str(); - - + const std::string s = time_oss.str(); + s_cache_timesize = s.size(); + std::memcpy(s_cache_timestr, s.c_str(), s_cache_timesize); } - output << s_cache_timestr; + output.write(s_cache_timestr, s_cache_timesize); } diff --git a/include/c11log/logger.h b/include/c11log/logger.h index 62e8390c..a11c7c1e 100644 --- a/include/c11log/logger.h +++ b/include/c11log/logger.h @@ -27,11 +27,11 @@ class logger { public: - using sink_ptr = std::shared_ptr; + using sink_ptr = std::shared_ptr; using sinks_vector_t = std::vector; - using sinks_init_list = std::initializer_list; + using sinks_init_list = std::initializer_list; - using formatter_ptr = std::shared_ptr; + using formatter_ptr = std::shared_ptr; logger(const std::string& name, sinks_init_list, formatter_ptr = nullptr); logger(const std::string& name, sink_ptr, formatter_ptr = nullptr); @@ -75,8 +75,7 @@ private: sinks_vector_t _sinks; std::atomic_int _min_level; - void _log_it(const details::log_msg& msg); - + void _log_it(details::log_msg& msg); }; @@ -208,8 +207,9 @@ inline bool c11log::logger::should_log(c11log::level::level_enum level) const return level >= _min_level.load(); } -inline void c11log::logger::_log_it(const details::log_msg& msg) +inline void c11log::logger::_log_it(details::log_msg& msg) { + _formatter->format(_logger_name, 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 e3553520..49640995 100644 --- a/include/c11log/sinks/async_sink.h +++ b/include/c11log/sinks/async_sink.h @@ -69,8 +69,8 @@ inline c11log::sinks::async_sink::~async_sink() inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg) -{ - if(!_active || msg.str.empty()) +{ + if(!_active || msg.formatted.empty()) return; _q.push(msg); } diff --git a/include/c11log/sinks/console_sinks.h b/include/c11log/sinks/console_sinks.h index 6adb9759..e0004456 100644 --- a/include/c11log/sinks/console_sinks.h +++ b/include/c11log/sinks/console_sinks.h @@ -23,7 +23,7 @@ protected: virtual void _sink_it(const details::log_msg& msg) override { std::lock_guard lock(_mutex); - _ostream << msg.str; + _ostream << msg.formatted; } std::ostream& _ostream; diff --git a/include/c11log/sinks/file_sinks.h b/include/c11log/sinks/file_sinks.h index a82f841d..60791b73 100644 --- a/include/c11log/sinks/file_sinks.h +++ b/include/c11log/sinks/file_sinks.h @@ -8,6 +8,7 @@ #include "../details/flush_helper.h" #include "../details/blocking_queue.h" + namespace c11log { namespace sinks @@ -31,7 +32,7 @@ protected: void _sink_it(const details::log_msg& msg) override { std::lock_guard lock(_mutex); - _flush_helper.write(msg.str, _ofstream); + _flush_helper.write(msg.formatted, _ofstream); } private: std::mutex _mutex; @@ -65,13 +66,13 @@ protected: { std::lock_guard lock(_mutex); - _current_size += msg.str.size(); + _current_size += msg.formatted.size(); if (_current_size > _max_size) { _rotate(); - _current_size = msg.str.size(); + _current_size = msg.formatted.size(); } - _flush_helper.write(msg.str, _ofstream); + _flush_helper.write(msg.formatted, _ofstream); } @@ -104,7 +105,7 @@ private: std::remove(target.c_str()); std::rename(src.c_str(), target.c_str()); } - _ofstream.open(_calc_filename(_base_filename, 0, _extension)); + _ofstream.open(_calc_filename(_base_filename, 0, _extension), std::ofstream::binary); } std::string _base_filename; std::string _extension; @@ -144,7 +145,7 @@ protected: _ofstream.open(_calc_filename(_base_filename, _extension)); _midnight_tp = _calc_midnight_tp(); } - _flush_helper.write(msg.str, _ofstream); + _flush_helper.write(msg.formatted, _ofstream); } private: