From c9dd1169f229c4744c869be4380a5e7f7d10e7ef Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 7 Mar 2014 00:52:50 +0200 Subject: [PATCH] returned fast_oss with optimizations --- astyle.sh | 3 +- example/example.cpp | 2 +- example/utils.h | 3 +- include/c11log/common_types.h | 6 ++-- include/c11log/details/blocking_queue.h | 9 ++++-- include/c11log/details/factory.h | 9 ++++-- include/c11log/details/fast_oss.h | 37 +++++++++++-------------- include/c11log/details/flush_helper.h | 9 ++++-- include/c11log/details/line_logger.h | 17 ++++++++---- include/c11log/details/os.h | 9 ++++-- include/c11log/formatter.h | 15 ++++++---- include/c11log/logger.h | 16 +++++------ include/c11log/sinks/async_sink.h | 9 ++++-- include/c11log/sinks/base_sink.h | 12 +++++--- include/c11log/sinks/file_sinks.h | 29 +++++++++++-------- include/c11log/sinks/stdout_sinks.h | 17 +++++++----- 16 files changed, 117 insertions(+), 85 deletions(-) diff --git a/astyle.sh b/astyle.sh index 21863846..2dcd6b17 100755 --- a/astyle.sh +++ b/astyle.sh @@ -1,3 +1,4 @@ #!/bin/bash -find . -name "*\.h" -o -name "*\.cpp"|xargs astyle --style=stroustrup +find . -name "*\.h" -o -name "*\.cpp"|xargs astyle -A8 + diff --git a/example/example.cpp b/example/example.cpp index 4d82c994..44c14582 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -58,8 +58,8 @@ int main(int argc, char* argv[]) auto &my_logger = get_logger("example"); auto null_sink = std::make_shared(); //auto async = std::make_shared(1000); - //async->add_sink(fsink); my_logger.add_sink(fsink); + //my_logger.add_sink(null_sink); auto start = system_clock::now(); diff --git a/example/utils.h b/example/utils.h index 2eaa592b..a5a0aa6f 100644 --- a/example/utils.h +++ b/example/utils.h @@ -7,7 +7,8 @@ #include #include -namespace utils { +namespace utils +{ template std::string format(const T& value) diff --git a/include/c11log/common_types.h b/include/c11log/common_types.h index bc25297d..d7d17e64 100644 --- a/include/c11log/common_types.h +++ b/include/c11log/common_types.h @@ -2,11 +2,13 @@ #include -namespace c11log { +namespace c11log +{ typedef std::chrono::system_clock log_clock; -namespace level { +namespace level +{ typedef enum { DEBUG, INFO, diff --git a/include/c11log/details/blocking_queue.h b/include/c11log/details/blocking_queue.h index 13576582..05c3d91b 100644 --- a/include/c11log/details/blocking_queue.h +++ b/include/c11log/details/blocking_queue.h @@ -10,11 +10,14 @@ #include #include -namespace c11log { -namespace details { +namespace c11log +{ +namespace details +{ template -class blocking_queue { +class blocking_queue +{ public: using queue_t = std::queue; using size_type = typename queue_t::size_type; diff --git a/include/c11log/details/factory.h b/include/c11log/details/factory.h index 68750f93..52c5603f 100644 --- a/include/c11log/details/factory.h +++ b/include/c11log/details/factory.h @@ -5,10 +5,13 @@ #include #include -namespace c11log { +namespace c11log +{ class logger; -namespace details { -class factory { +namespace details +{ +class factory +{ public: typedef std::shared_ptr logger_ptr; typedef std::unordered_map logger_map; diff --git a/include/c11log/details/fast_oss.h b/include/c11log/details/fast_oss.h index 68149fd0..29349fb3 100644 --- a/include/c11log/details/fast_oss.h +++ b/include/c11log/details/fast_oss.h @@ -1,27 +1,29 @@ #pragma once +// Fast ostringstream like supprt which return its string by ref and nothing more + #include #include -namespace c11log { -namespace details { -class str_devicebuf:public std::streambuf { +namespace c11log +{ +namespace details +{ + +class str_devicebuf:public std::streambuf +{ public: str_devicebuf() = default; ~str_devicebuf() = default; - str_devicebuf(const str_devicebuf& other):std::streambuf(),_str(other._str) {} - - str_devicebuf(str_devicebuf&& other) :std::streambuf(), _str(std::move(other._str)) { - other._str.clear(); - } + str_devicebuf(const str_devicebuf& other) = delete; + str_devicebuf(str_devicebuf&& other) = delete; str_devicebuf& operator=(const str_devicebuf&) = delete; str_devicebuf& operator=(str_devicebuf&&) = delete; const std::string& str_ref() const { return _str; - std::ostringstream oss; } void clear() { @@ -47,29 +49,22 @@ private: std::string _str; }; -class fast_oss:public std::ostream { +class fast_oss:public std::ostream +{ public: fast_oss():std::ostream(&_dev) {} ~fast_oss() = default; - fast_oss(const fast_oss& other) :std::basic_ios(), std::ostream(&_dev), _dev(other._dev) {} - - fast_oss(fast_oss&& other) :std::basic_ios(), std::ostream(&_dev), _dev(std::move(other._dev)) {} - + fast_oss(const fast_oss& other) = delete; + fast_oss(fast_oss&& other) = delete; fast_oss& operator=(const fast_oss& other) = delete; - const std::string& str_ref() const { return _dev.str_ref(); } - const std::string str() const { - return _dev.str_ref(); - } - void clear() { - _dev.clear(); - } + private: str_devicebuf _dev; }; diff --git a/include/c11log/details/flush_helper.h b/include/c11log/details/flush_helper.h index f179891d..e9226f32 100644 --- a/include/c11log/details/flush_helper.h +++ b/include/c11log/details/flush_helper.h @@ -3,9 +3,12 @@ #include // Flush to file every X writes.. -namespace c11log { -namespace details { -class file_flush_helper { +namespace c11log +{ +namespace details +{ +class file_flush_helper +{ public: explicit file_flush_helper(const std::chrono::milliseconds &flush_every): _flush_every(flush_every), _last_flush() {}; diff --git a/include/c11log/details/line_logger.h b/include/c11log/details/line_logger.h index 215fe9d7..1010bc82 100644 --- a/include/c11log/details/line_logger.h +++ b/include/c11log/details/line_logger.h @@ -2,13 +2,17 @@ #include "../common_types.h" #include "../logger.h" +#include "fast_oss.h" #include -namespace c11log { +namespace c11log +{ class logger; -namespace details { +namespace details +{ -class line_logger { +class line_logger +{ public: line_logger(logger* callback_logger, level::level_enum msg_level, bool enabled): _callback_logger(callback_logger), @@ -25,7 +29,7 @@ public: line_logger(line_logger&& other) : _callback_logger(other._callback_logger), - _oss(std::move(other._oss)), + _oss(), _level(other._level) { }; @@ -36,7 +40,7 @@ public: ~line_logger() { if (_enabled) { _oss << '\n'; - _callback_logger->_log_it(_oss.str(), _level); + _callback_logger->_log_it(_oss.str_ref(), _level); } } @@ -50,7 +54,8 @@ public: private: logger* _callback_logger; - std::ostringstream _oss; + //std::ostringstream _oss; + details::fast_oss _oss; level::level_enum _level; bool _enabled; diff --git a/include/c11log/details/os.h b/include/c11log/details/os.h index dcc1f532..b799f601 100644 --- a/include/c11log/details/os.h +++ b/include/c11log/details/os.h @@ -3,9 +3,12 @@ #include #include -namespace c11log { -namespace details { -namespace os { +namespace c11log +{ +namespace details +{ +namespace os +{ inline std::tm localtime(const std::time_t &time_tt) { diff --git a/include/c11log/formatter.h b/include/c11log/formatter.h index 907e4497..1acfc9d7 100644 --- a/include/c11log/formatter.h +++ b/include/c11log/formatter.h @@ -10,15 +10,18 @@ #include "common_types.h" #include "details/os.h" -namespace c11log { -namespace formatters { +namespace c11log +{ +namespace formatters +{ typedef std::function format_fn; std::string to_hex(const unsigned char* buf, std::size_t size); -class formatter { +class formatter +{ public: formatter() {} virtual ~formatter() {} @@ -26,7 +29,8 @@ public: }; -class default_formatter: public formatter { +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& dest) override { @@ -55,8 +59,7 @@ inline void c11log::formatters::default_formatter::_format_time(const log_clock: auto tm_now = details::os::localtime(log_clock::to_time_t(tp)); using namespace c11log::details::os; - if(last_tm != tm_now) - { + if(last_tm != tm_now) { #ifdef _MSC_VER ::sprintf_s #else diff --git a/include/c11log/logger.h b/include/c11log/logger.h index 07b724fb..45eb471a 100644 --- a/include/c11log/logger.h +++ b/include/c11log/logger.h @@ -12,15 +12,18 @@ #include "sinks/base_sink.h" #include "details/factory.h" -namespace c11log { +namespace c11log +{ -namespace details { +namespace details +{ class line_logger; } -class logger { +class logger +{ public: typedef std::shared_ptr sink_ptr_t; @@ -30,8 +33,7 @@ public: _logger_name(name), _formatter(new formatters::default_formatter()), _sinks(), - _mutex() - { + _mutex() { //Seems that vs2013 doesnt support atomic member initialization in ctor, so its done here _atomic_level = level::INFO; } @@ -72,13 +74,9 @@ private: logger& get_logger(const std::string& name); - - - } - // // Logger inline impl // diff --git a/include/c11log/sinks/async_sink.h b/include/c11log/sinks/async_sink.h index c5fa4b18..73d86d8e 100644 --- a/include/c11log/sinks/async_sink.h +++ b/include/c11log/sinks/async_sink.h @@ -8,10 +8,13 @@ #include "../logger.h" #include "../details/blocking_queue.h" -namespace c11log { -namespace sinks { +namespace c11log +{ +namespace sinks +{ -class async_sink : public base_sink { +class async_sink : public base_sink +{ public: using size_type = c11log::details::blocking_queue::size_type; diff --git a/include/c11log/sinks/base_sink.h b/include/c11log/sinks/base_sink.h index dbe3bdb0..fce5cf66 100644 --- a/include/c11log/sinks/base_sink.h +++ b/include/c11log/sinks/base_sink.h @@ -6,9 +6,12 @@ #include "../formatter.h" #include "../common_types.h" -namespace c11log { -namespace sinks { -class base_sink { +namespace c11log +{ +namespace sinks +{ +class base_sink +{ public: base_sink() = default; base_sink(level::level_enum l):_level(l) { @@ -33,7 +36,8 @@ protected: std::atomic _level {level::INFO}; }; -class null_sink:public base_sink { +class null_sink:public base_sink +{ protected: void _sink_it(const std::string& ) override { } diff --git a/include/c11log/sinks/file_sinks.h b/include/c11log/sinks/file_sinks.h index 539fbb06..1043e400 100644 --- a/include/c11log/sinks/file_sinks.h +++ b/include/c11log/sinks/file_sinks.h @@ -6,13 +6,16 @@ #include "base_sink.h" #include "../details/flush_helper.h" -namespace c11log { -namespace sinks { +namespace c11log +{ +namespace sinks +{ /* * Trivial file sink with single file as target */ -class simple_file_sink : public base_sink { +class simple_file_sink : public base_sink +{ public: explicit simple_file_sink(const std::string &filename, const std::string& extension, @@ -36,7 +39,8 @@ private: /* * Thread safe, size limited file sink */ -class rotating_file_sink : public base_sink { +class rotating_file_sink : public base_sink +{ public: rotating_file_sink(const std::string &base_filename, const std::string &extension, size_t max_size, size_t max_files, @@ -104,7 +108,8 @@ private: /* * Thread safe file sink that closes the log file at midnight and opens new one */ -class daily_file_sink:public base_sink { +class daily_file_sink:public base_sink +{ public: explicit daily_file_sink(const std::string& base_filename, const std::string& extension, @@ -140,14 +145,14 @@ private: return system_clock::time_point(midnight + hours(24)); } - //Create filename for the form basename.YYYY-MM-DD.extension + //Create filename for the form basename.YYYY-MM-DD.extension static std::string _calc_filename(const std::string& basename, const std::string& extension) { - std::tm tm = c11log::details::os::localtime(); - std::ostringstream oss; - oss << basename << '.'; - oss << tm.tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << tm.tm_mon + 1 << '-' << tm.tm_mday; - oss << '.' << extension; - return oss.str(); + std::tm tm = c11log::details::os::localtime(); + std::ostringstream oss; + oss << basename << '.'; + oss << tm.tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << tm.tm_mon + 1 << '-' << tm.tm_mday; + oss << '.' << extension; + return oss.str(); } std::string _base_filename; diff --git a/include/c11log/sinks/stdout_sinks.h b/include/c11log/sinks/stdout_sinks.h index a0639c83..3461b617 100644 --- a/include/c11log/sinks/stdout_sinks.h +++ b/include/c11log/sinks/stdout_sinks.h @@ -6,23 +6,26 @@ #include "base_sink.h" -namespace c11log { -namespace sinks { -class ostream_sink: public base_sink { +namespace c11log +{ +namespace sinks +{ +class ostream_sink: public base_sink +{ public: explicit ostream_sink(std::ostream& os):_ostream(os) {} - ostream_sink(const ostream_sink&) = delete; - ostream_sink& operator=(const ostream_sink&) = delete; + ostream_sink(const ostream_sink&) = delete; + ostream_sink& operator=(const ostream_sink&) = delete; virtual ~ostream_sink() = default; protected: virtual void _sink_it(const std::string& msg) override { - std::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); _ostream << msg; } std::ostream& _ostream; - std::mutex _mutex; + std::mutex _mutex; };