From c7b8c762fbeb7db011aea264254066c01eab5ab3 Mon Sep 17 00:00:00 2001 From: gabi Date: Fri, 31 Oct 2014 01:13:27 +0200 Subject: [PATCH] spdlog --- include/{spitlog => spdlog}/common.h | 13 +- .../details/blocking_queue.h | 2 +- .../details/fast_istostr.h | 2 +- .../{spitlog => spdlog}/details/fast_oss.h | 2 +- .../{spitlog => spdlog}/details/file_helper.h | 15 +- .../{spitlog => spdlog}/details/line_logger.h | 4 +- include/{spitlog => spdlog}/details/log_msg.h | 2 +- include/spdlog/details/logger_impl.h | 136 +++++++++++++++ .../{spitlog => spdlog}/details/null_mutex.h | 2 +- include/{spitlog => spdlog}/details/os.h | 4 +- .../details/pattern_formatter_impl.h | 10 +- .../{spitlog => spdlog}/details/registry.h | 61 ++++--- include/spdlog/details/spdlog_impl.h | 51 ++++++ .../{spitlog => spdlog}/details/stack_buf.h | 4 +- include/{spitlog => spdlog}/formatter.h | 4 +- include/{spitlog => spdlog}/logger.h | 26 +-- .../{spitlog => spdlog}/sinks/async_sink.h | 20 +-- include/{spitlog => spdlog}/sinks/base_sink.h | 2 +- .../{spitlog => spdlog}/sinks/file_sinks.h | 8 +- include/{spitlog => spdlog}/sinks/null_sink.h | 2 +- .../{spitlog => spdlog}/sinks/ostream_sink.h | 2 +- include/{spitlog => spdlog}/sinks/sink.h | 2 +- .../{spitlog => spdlog}/sinks/stdout_sinks.h | 2 +- include/spdlog/spdlog.h | 84 +++++++++ include/spitlog/details/logger_impl.h | 163 ------------------ 25 files changed, 370 insertions(+), 253 deletions(-) rename include/{spitlog => spdlog}/common.h (79%) rename include/{spitlog => spdlog}/details/blocking_queue.h (99%) rename include/{spitlog => spdlog}/details/fast_istostr.h (98%) rename include/{spitlog => spdlog}/details/fast_oss.h (99%) rename include/{spitlog => spdlog}/details/file_helper.h (94%) rename include/{spitlog => spdlog}/details/line_logger.h (97%) rename include/{spitlog => spdlog}/details/log_msg.h (98%) create mode 100644 include/spdlog/details/logger_impl.h rename include/{spitlog => spdlog}/details/null_mutex.h (89%) rename include/{spitlog => spdlog}/details/os.h (98%) rename include/{spitlog => spdlog}/details/pattern_formatter_impl.h (97%) rename include/{spitlog => spdlog}/details/registry.h (56%) create mode 100644 include/spdlog/details/spdlog_impl.h rename include/{spitlog => spdlog}/details/stack_buf.h (97%) rename include/{spitlog => spdlog}/formatter.h (90%) rename include/{spitlog => spdlog}/logger.h (69%) rename include/{spitlog => spdlog}/sinks/async_sink.h (79%) rename include/{spitlog => spdlog}/sinks/base_sink.h (97%) rename include/{spitlog => spdlog}/sinks/file_sinks.h (97%) rename include/{spitlog => spdlog}/sinks/null_sink.h (94%) rename include/{spitlog => spdlog}/sinks/ostream_sink.h (97%) rename include/{spitlog => spdlog}/sinks/sink.h (90%) rename include/{spitlog => spdlog}/sinks/stdout_sinks.h (97%) create mode 100644 include/spdlog/spdlog.h delete mode 100644 include/spitlog/details/logger_impl.h diff --git a/include/spitlog/common.h b/include/spdlog/common.h similarity index 79% rename from include/spitlog/common.h rename to include/spdlog/common.h index 8e22060f..dd0dbe64 100644 --- a/include/spitlog/common.h +++ b/include/spdlog/common.h @@ -3,7 +3,7 @@ #include #include -namespace spitlog +namespace spdlog { class formatter; namespace sinks { @@ -14,7 +14,7 @@ class sink; 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; +using formatter_ptr = std::shared_ptr; //Log level enum namespace level @@ -27,11 +27,12 @@ typedef enum WARN, ERR, CRITICAL, - NONE = 99 + ALWAYS, + OFF } level_enum; -static const char* level_names[] { "trace", "debug", "info", "warning", "error", "critical", "fatal" }; -inline const char* to_str(spitlog::level::level_enum l) +static const char* level_names[] { "trace", "debug", "info", "warning", "error", "critical", "", ""}; +inline const char* to_str(spdlog::level::level_enum l) { return level_names[l]; } @@ -52,4 +53,4 @@ private: }; -} //spitlog +} //spdlog diff --git a/include/spitlog/details/blocking_queue.h b/include/spdlog/details/blocking_queue.h similarity index 99% rename from include/spitlog/details/blocking_queue.h rename to include/spdlog/details/blocking_queue.h index 30e29046..574a5530 100644 --- a/include/spitlog/details/blocking_queue.h +++ b/include/spdlog/details/blocking_queue.h @@ -10,7 +10,7 @@ #include #include -namespace spitlog +namespace spdlog { namespace details { diff --git a/include/spitlog/details/fast_istostr.h b/include/spdlog/details/fast_istostr.h similarity index 98% rename from include/spitlog/details/fast_istostr.h rename to include/spdlog/details/fast_istostr.h index 6da3e3f2..3855d57a 100644 --- a/include/spitlog/details/fast_istostr.h +++ b/include/spdlog/details/fast_istostr.h @@ -5,7 +5,7 @@ //Source: http://stackoverflow.com/a/4351484/192001 //Modified version to pad zeros according to padding arg -namespace spitlog { +namespace spdlog { namespace details { const char digit_pairs[201] = { diff --git a/include/spitlog/details/fast_oss.h b/include/spdlog/details/fast_oss.h similarity index 99% rename from include/spitlog/details/fast_oss.h rename to include/spdlog/details/fast_oss.h index 611b648a..3a9a07ed 100644 --- a/include/spitlog/details/fast_oss.h +++ b/include/spdlog/details/fast_oss.h @@ -9,7 +9,7 @@ #include "stack_buf.h" #include -namespace spitlog +namespace spdlog { namespace details { diff --git a/include/spitlog/details/file_helper.h b/include/spdlog/details/file_helper.h similarity index 94% rename from include/spitlog/details/file_helper.h rename to include/spdlog/details/file_helper.h index 777dbc29..fd5ee4a0 100644 --- a/include/spitlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -16,7 +16,7 @@ -namespace spitlog +namespace spdlog { namespace details { @@ -36,16 +36,14 @@ public: ~file_helper() { - if (_fd) - std::fclose(_fd); + close(); } void open(const std::string& filename) { - if (_fd) - std::fclose(_fd); + close(); _filename = filename; for (int tries = 0; tries < open_max_tries; ++tries) @@ -61,8 +59,11 @@ public: void close() { - std::fclose(_fd); - _fd = nullptr; + if (_fd) + { + std::fclose(_fd); + _fd = nullptr; + } } void write(const log_msg& msg) diff --git a/include/spitlog/details/line_logger.h b/include/spdlog/details/line_logger.h similarity index 97% rename from include/spitlog/details/line_logger.h rename to include/spdlog/details/line_logger.h index a315e22a..23b3b0cc 100644 --- a/include/spitlog/details/line_logger.h +++ b/include/spdlog/details/line_logger.h @@ -8,7 +8,7 @@ // Line logger class - aggregates operator<< calls to fast ostream // and logs upon destruction -namespace spitlog +namespace spdlog { namespace details { @@ -76,4 +76,4 @@ private: bool _enabled; }; } //Namespace details -} // Namespace spitlog +} // Namespace spdlog diff --git a/include/spitlog/details/log_msg.h b/include/spdlog/details/log_msg.h similarity index 98% rename from include/spitlog/details/log_msg.h rename to include/spdlog/details/log_msg.h index 80d80621..2ee38dc5 100644 --- a/include/spitlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -3,7 +3,7 @@ #include "../common.h" #include "./fast_oss.h" -namespace spitlog +namespace spdlog { namespace details { diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h new file mode 100644 index 00000000..32900093 --- /dev/null +++ b/include/spdlog/details/logger_impl.h @@ -0,0 +1,136 @@ +#pragma once +// +// Logger implementation +// + + +#include "./line_logger.h" + + +inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list) : + _name(logger_name), + _sinks(sinks_list) +{ + // no support under vs2013 for member initialization for std::atomic + _level = level::INFO; +} + +template +inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) : + _name(logger_name), + _sinks(begin, end) +{} + + +inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter) +{ + _formatter = msg_formatter; +} + +inline void spdlog::logger::set_pattern(const std::string& pattern) +{ + _formatter = std::make_shared(pattern); +} + +inline spdlog::formatter_ptr spdlog::logger::get_formatter() const +{ + return _formatter; +} + + +template +inline spdlog::details::line_logger spdlog::logger::log(level::level_enum lvl, const Args&... args) { + bool msg_enabled = should_log(lvl); + details::line_logger l(this, lvl, msg_enabled); + if (msg_enabled) + _variadic_log(l, args...); + return l; +} + +template +inline spdlog::details::line_logger spdlog::logger::log(const Args&... args) { + return log(level::ALWAYS, args...); +} + +template +inline spdlog::details::line_logger spdlog::logger::trace(const Args&... args) +{ + return log(level::TRACE, args...); +} + +template +inline spdlog::details::line_logger spdlog::logger::debug(const Args&... args) +{ + return log(level::DEBUG, args...); +} + +template +inline spdlog::details::line_logger spdlog::logger::info(const Args&... args) +{ + return log(level::INFO, args...); +} + +template +inline spdlog::details::line_logger spdlog::logger::warn(const Args&... args) +{ + return log(level::WARN, args...); +} + +template +inline spdlog::details::line_logger spdlog::logger::error(const Args&... args) +{ + return log(level::ERR, args...); +} + +template +inline spdlog::details::line_logger spdlog::logger::critical(const Args&... args) +{ + return log(level::CRITICAL, args...); +} + +inline const std::string& spdlog::logger::name() const +{ + return _name; +} + +inline void spdlog::logger::set_level(spdlog::level::level_enum log_level) +{ + _level.store(log_level); +} + +inline spdlog::level::level_enum spdlog::logger::level() const +{ + return static_cast(_level.load()); +} + +inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) const +{ + return msg_level >= _level.load(); +} + +inline void spdlog::logger::stop_logging() +{ + set_level(level::OFF); +} + + +inline void spdlog::logger::_variadic_log(spdlog::details::line_logger&) {} + +template +void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const First& first, const Rest&... rest) +{ + l.write(first); + l.write(' '); + _variadic_log(l, rest...); +} + +inline void spdlog::logger::_log_msg(details::log_msg& msg) +{ + //Use default formatter if not set + if (!_formatter) + _formatter = std::make_shared("%+"); + _formatter->format(msg); + for (auto &sink : _sinks) + sink->log(msg); +} + diff --git a/include/spitlog/details/null_mutex.h b/include/spdlog/details/null_mutex.h similarity index 89% rename from include/spitlog/details/null_mutex.h rename to include/spdlog/details/null_mutex.h index 3ad3f000..54c354c5 100644 --- a/include/spitlog/details/null_mutex.h +++ b/include/spdlog/details/null_mutex.h @@ -2,7 +2,7 @@ // null, no cost mutex -namespace spitlog { +namespace spdlog { namespace details { struct null_mutex { diff --git a/include/spitlog/details/os.h b/include/spdlog/details/os.h similarity index 98% rename from include/spitlog/details/os.h rename to include/spdlog/details/os.h index 19d2b36f..ebea0547 100644 --- a/include/spitlog/details/os.h +++ b/include/spdlog/details/os.h @@ -7,7 +7,7 @@ #include #endif -namespace spitlog +namespace spdlog { namespace details { @@ -123,7 +123,7 @@ inline int utc_minutes_offset(const std::tm& tm = localtime()) } //os } //details -} //spitlog +} //spdlog diff --git a/include/spitlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h similarity index 97% rename from include/spitlog/details/pattern_formatter_impl.h rename to include/spdlog/details/pattern_formatter_impl.h index 0c3b2e5a..949ecbfe 100644 --- a/include/spitlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -10,7 +10,7 @@ #include "./fast_oss.h" #include "./os.h" -namespace spitlog +namespace spdlog { namespace details { class flag_formatter @@ -377,12 +377,12 @@ class full_formatter :public flag_formatter /////////////////////////////////////////////////////////////////////////////// // pattern_formatter inline impl /////////////////////////////////////////////////////////////////////////////// -inline spitlog::pattern_formatter::pattern_formatter(const std::string& pattern) +inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern) { compile_pattern(pattern); } -inline void spitlog::pattern_formatter::compile_pattern(const std::string& pattern) +inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern) { auto end = pattern.end(); std::unique_ptr user_chars; @@ -411,7 +411,7 @@ inline void spitlog::pattern_formatter::compile_pattern(const std::string& patte } } -inline void spitlog::pattern_formatter::handle_flag(char flag) +inline void spdlog::pattern_formatter::handle_flag(char flag) { switch (flag) { @@ -523,7 +523,7 @@ inline void spitlog::pattern_formatter::handle_flag(char flag) } -inline void spitlog::pattern_formatter::format(details::log_msg& msg) +inline void spdlog::pattern_formatter::format(details::log_msg& msg) { for (auto &f : _formatters) { diff --git a/include/spitlog/details/registry.h b/include/spdlog/details/registry.h similarity index 56% rename from include/spitlog/details/registry.h rename to include/spdlog/details/registry.h index 3773ba8f..e75a17ec 100644 --- a/include/spitlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -7,58 +7,77 @@ #include #include #include + #include "../logger.h" #include "../common.h" -namespace spitlog { +namespace spdlog { namespace details { + class registry { public: std::shared_ptr get(const std::string& name) { - std::lock_guard l(_mutex); + std::lock_guard lock(_mutex); auto found = _loggers.find(name); return found == _loggers.end() ? nullptr : found->second; } - std::shared_ptr create(const std::string& logger_name, sinks_init_list sinks) + + template + std::shared_ptr create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) { - std::lock_guard l(_mutex); - auto new_logger = std::make_shared(logger_name, sinks); + std::lock_guard lock(_mutex); + auto new_logger = std::make_shared(logger_name, sinks_begin, sinks_end); new_logger->set_formatter(_formatter); + new_logger->set_level(_level); _loggers[logger_name] = new_logger; return new_logger; } + + std::shared_ptr create(const std::string& logger_name, sinks_init_list sinks) + { + return create(logger_name, sinks.begin(), sinks.end()); + } + std::shared_ptr create(const std::string& logger_name, sink_ptr sink) { return create(logger_name, { sink }); } - template - std::shared_ptr create (const std::string& logger_name, const It& sinks_begin, const It& sinks_end) - { - std::lock_guard l(_mutex); - auto new_logger = std::make_shared(logger_name, sinks_begin, sinks_end); - new_logger->set_formatter(_formatter); - _loggers[logger_name] = new_logger; - return new_logger; - - } - void formatter(formatter_ptr f) { + std::lock_guard lock(_mutex); _formatter = f; + for (auto& l : _loggers) + l.second->set_formatter(_formatter); } - formatter_ptr formatter() + + void set_pattern(const std::string& pattern) { - return _formatter; + std::lock_guard lock(_mutex); + _formatter = std::make_shared(pattern); + for (auto& l : _loggers) + l.second->set_formatter(_formatter); + } - void set_format(const std::string& format_string) + void set_level(level::level_enum log_level) { - _formatter = std::make_shared(format_string); + std::lock_guard lock(_mutex); + for (auto& l : _loggers) + l.second->set_level(log_level); + + } + + void stop_all() + { + std::lock_guard lock(_mutex); + _level = level::OFF; + for (auto& l : _loggers) + l.second->stop_logging(); } @@ -74,7 +93,7 @@ private: std::mutex _mutex; std::unordered_map > _loggers; formatter_ptr _formatter; - + level::level_enum _level = level::INFO; }; } } diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h new file mode 100644 index 00000000..05c1149a --- /dev/null +++ b/include/spdlog/details/spdlog_impl.h @@ -0,0 +1,51 @@ +#pragma once + +// +// Global registry functions +// +#include "registry.h" + +inline std::shared_ptr spdlog::get(const std::string& name) +{ + return details::registry::instance().get(name); +} + +inline std::shared_ptr spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks) +{ + return details::registry::instance().create(logger_name, sinks); +} + + +template +inline std::shared_ptr spdlog::create(const std::string& logger_name, const Args&... args) +{ + sink_ptr sink = std::make_shared(args...); + return details::registry::instance().create(logger_name, { sink }); +} + + +template +inline std::shared_ptr spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) +{ + return details::registry::instance().create(logger_name, sinks_begin, sinks_end); +} + +inline void spdlog::set_formatter(spdlog::formatter_ptr f) +{ + details::registry::instance().formatter(f); +} + +inline void spdlog::set_pattern(const std::string& format_string) +{ + return details::registry::instance().set_pattern(format_string); +} + +inline void spdlog::set_level(level::level_enum log_level) +{ + return details::registry::instance().set_level(log_level); +} + +inline void spdlog::stop() +{ + return details::registry::instance().stop_all(); +} diff --git a/include/spitlog/details/stack_buf.h b/include/spdlog/details/stack_buf.h similarity index 97% rename from include/spitlog/details/stack_buf.h rename to include/spdlog/details/stack_buf.h index 16b02d27..d8d2a2aa 100644 --- a/include/spitlog/details/stack_buf.h +++ b/include/spdlog/details/stack_buf.h @@ -7,7 +7,7 @@ // Fast memory storage on the stack when possible or in std::vector -namespace spitlog +namespace spdlog { namespace details { @@ -108,4 +108,4 @@ private: }; } -} //namespace spitlog { namespace details { +} //namespace spdlog { namespace details { diff --git a/include/spitlog/formatter.h b/include/spdlog/formatter.h similarity index 90% rename from include/spitlog/formatter.h rename to include/spdlog/formatter.h index 9ca1427b..8575c073 100644 --- a/include/spitlog/formatter.h +++ b/include/spdlog/formatter.h @@ -1,7 +1,7 @@ #pragma once #include "details/log_msg.h" -namespace spitlog +namespace spdlog { namespace details { class flag_formatter; @@ -29,5 +29,5 @@ private: }; } -#include "./details/pattern_formatter_impl.h" +#include "details/pattern_formatter_impl.h" diff --git a/include/spitlog/logger.h b/include/spdlog/logger.h similarity index 69% rename from include/spitlog/logger.h rename to include/spdlog/logger.h index 88005638..a4008dc1 100644 --- a/include/spitlog/logger.h +++ b/include/spdlog/logger.h @@ -15,7 +15,7 @@ #include "sinks/base_sink.h" #include "common.h" -namespace spitlog +namespace spdlog { namespace details @@ -31,7 +31,7 @@ public: template logger(const std::string& name, const It& begin, const It& end); - void set_format(const std::string&); + void set_pattern(const std::string&); void set_formatter(formatter_ptr); formatter_ptr get_formatter() const; @@ -45,7 +45,10 @@ public: const std::string& name() const; bool should_log(level::level_enum) const; + void stop_logging(); + template details::line_logger log(level::level_enum lvl, const Args&... args); + template details::line_logger log(const Args&... args); template details::line_logger trace(const Args&... args); template details::line_logger debug(const Args&... args); template details::line_logger info(const Args&... args); @@ -67,35 +70,20 @@ private: }; -// -// Registry functions for easy loggers creation and retrieval -// example -// auto console_logger = spitlog::create("my_logger", spitlog::sinks); -// auto same_logger = spitlog::get("my_logger"); -// auto file_logger = c11 -// -std::shared_ptr get(const std::string& name); -std::shared_ptr create(const std::string& logger_name, sinks_init_list sinks); -template -std::shared_ptr create(const std::string& logger_name, const Args&... args); -template -std::shared_ptr create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end); -void set_formatter(formatter_ptr f); -void set_format(const std::string& format_string); } // // Trace & debug macros // #ifdef FFLOG_ENABLE_TRACE -#define FFLOG_TRACE(logger, ...) logger->log(spitlog::level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__) +#define FFLOG_TRACE(logger, ...) logger->log(spdlog::level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__) #else #define FFLOG_TRACE(logger, ...) {} #endif #ifdef FFLOG_ENABLE_DEBUG -#define FFLOG_DEBUG(logger, ...) logger->log(spitlog::level::DEBUG, __VA_ARGS__) +#define FFLOG_DEBUG(logger, ...) logger->log(spdlog::level::DEBUG, __VA_ARGS__) #else #define FFLOG_DEBUG(logger, ...) {} #endif diff --git a/include/spitlog/sinks/async_sink.h b/include/spdlog/sinks/async_sink.h similarity index 79% rename from include/spitlog/sinks/async_sink.h rename to include/spdlog/sinks/async_sink.h index 7f76b824..35a8b6e0 100644 --- a/include/spitlog/sinks/async_sink.h +++ b/include/spdlog/sinks/async_sink.h @@ -13,7 +13,7 @@ #include -namespace spitlog +namespace spdlog { namespace sinks { @@ -53,26 +53,26 @@ private: /////////////////////////////////////////////////////////////////////////////// // async_sink class implementation /////////////////////////////////////////////////////////////////////////////// -inline spitlog::sinks::async_sink::async_sink(const q_type::size_type max_queue_size) +inline spdlog::sinks::async_sink::async_sink(const q_type::size_type max_queue_size) :_sinks(), _active(true), _q(max_queue_size), _back_thread(&async_sink::_thread_loop, this) {} -inline spitlog::sinks::async_sink::~async_sink() +inline spdlog::sinks::async_sink::~async_sink() { _shutdown(); } -inline void spitlog::sinks::async_sink::_sink_it(const details::log_msg& msg) +inline void spdlog::sinks::async_sink::_sink_it(const details::log_msg& msg) { if(!_active) return; _q.push(msg); } -inline void spitlog::sinks::async_sink::_thread_loop() +inline void spdlog::sinks::async_sink::_thread_loop() { static std::chrono::seconds pop_timeout { 1 }; while (_active) @@ -90,27 +90,27 @@ inline void spitlog::sinks::async_sink::_thread_loop() } } -inline void spitlog::sinks::async_sink::add_sink(spitlog::sink_ptr s) +inline void spdlog::sinks::async_sink::add_sink(spdlog::sink_ptr s) { std::lock_guard guard(_mutex); _sinks.push_back(s); } -inline void spitlog::sinks::async_sink::remove_sink(spitlog::sink_ptr s) +inline void spdlog::sinks::async_sink::remove_sink(spdlog::sink_ptr s) { std::lock_guard guard(_mutex); _sinks.erase(std::remove(_sinks.begin(), _sinks.end(), s), _sinks.end()); } -inline spitlog::sinks::async_sink::q_type& spitlog::sinks::async_sink::q() +inline spdlog::sinks::async_sink::q_type& spdlog::sinks::async_sink::q() { return _q; } -inline void spitlog::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout) +inline void spdlog::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout) { if(timeout > std::chrono::milliseconds::zero()) { @@ -124,7 +124,7 @@ inline void spitlog::sinks::async_sink::shutdown(const std::chrono::milliseconds } -inline void spitlog::sinks::async_sink::_shutdown() +inline void spdlog::sinks::async_sink::_shutdown() { std::lock_guard guard(_mutex); if(_active) diff --git a/include/spitlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h similarity index 97% rename from include/spitlog/sinks/base_sink.h rename to include/spdlog/sinks/base_sink.h index 254a680b..5e91e4f5 100644 --- a/include/spitlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -14,7 +14,7 @@ #include "../details/log_msg.h" -namespace spitlog +namespace spdlog { namespace sinks { diff --git a/include/spitlog/sinks/file_sinks.h b/include/spdlog/sinks/file_sinks.h similarity index 97% rename from include/spitlog/sinks/file_sinks.h rename to include/spdlog/sinks/file_sinks.h index 2a555829..ae35d834 100644 --- a/include/spitlog/sinks/file_sinks.h +++ b/include/spdlog/sinks/file_sinks.h @@ -10,7 +10,7 @@ -namespace spitlog +namespace spdlog { namespace sinks { @@ -159,7 +159,7 @@ private: using namespace std::chrono; auto now = system_clock::now(); time_t tnow = std::chrono::system_clock::to_time_t(now); - tm date = spitlog::details::os::localtime(tnow); + tm date = spdlog::details::os::localtime(tnow); date.tm_hour = date.tm_min = date.tm_sec = 0; auto midnight = std::chrono::system_clock::from_time_t(std::mktime(&date)); return system_clock::time_point(midnight + hours(24)); @@ -168,8 +168,8 @@ private: //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 = spitlog::details::os::localtime(); - fast_oss oss; + std::tm tm = spdlog::details::os::localtime(); + details::fast_oss oss; oss << basename << '.'; oss << tm.tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << tm.tm_mon + 1 << '-' << tm.tm_mday; oss << '.' << extension; diff --git a/include/spitlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h similarity index 94% rename from include/spitlog/sinks/null_sink.h rename to include/spdlog/sinks/null_sink.h index b79ccf32..776425e6 100644 --- a/include/spitlog/sinks/null_sink.h +++ b/include/spdlog/sinks/null_sink.h @@ -4,7 +4,7 @@ #include "../details/null_mutex.h" -namespace spitlog { +namespace spdlog { namespace sinks { template diff --git a/include/spitlog/sinks/ostream_sink.h b/include/spdlog/sinks/ostream_sink.h similarity index 97% rename from include/spitlog/sinks/ostream_sink.h rename to include/spdlog/sinks/ostream_sink.h index 6b8ef6c3..5fa62683 100644 --- a/include/spitlog/sinks/ostream_sink.h +++ b/include/spdlog/sinks/ostream_sink.h @@ -7,7 +7,7 @@ #include "../details/null_mutex.h" #include "./base_sink.h" -namespace spitlog +namespace spdlog { namespace sinks { diff --git a/include/spitlog/sinks/sink.h b/include/spdlog/sinks/sink.h similarity index 90% rename from include/spitlog/sinks/sink.h rename to include/spdlog/sinks/sink.h index 1aac1797..9d4a2cc5 100644 --- a/include/spitlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -2,7 +2,7 @@ #include "../details/log_msg.h" -namespace spitlog +namespace spdlog { namespace sinks { diff --git a/include/spitlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h similarity index 97% rename from include/spitlog/sinks/stdout_sinks.h rename to include/spdlog/sinks/stdout_sinks.h index d79bec5b..19b0f908 100644 --- a/include/spitlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -5,7 +5,7 @@ #include "./ostream_sink.h" #include "../details/null_mutex.h" -namespace spitlog +namespace spdlog { namespace sinks { diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h new file mode 100644 index 00000000..44d13485 --- /dev/null +++ b/include/spdlog/spdlog.h @@ -0,0 +1,84 @@ +// +// This is spdlog - an extremely fast and easy to use c++11 logging library +// + +// example code (create multi threaded daily logger): +// auto my_logger = spdlog::create("mylog", "dailylog_filename", "txt"); +// .. +// auto mylog = spdlog::get("mylog"); +// mylog->info("Hello logger.", "This is message number", 1, "!!") ; +// mylog->info() << "std streams are also supprted: " << std::hex << 255; + +// see example.cpp for more examples + +#pragma once + +#include "logger.h" +#include "details/registry.h" + +namespace spdlog +{ + +// Return an existing logger or nullptr if a logger with such name doesn't exist. +// Examples: +// +// spdlog::get("mylog")->info("Hello"); +// auto logger = spdlog::get("mylog"); +// logger.info("This is another message" , x, y, z); +// logger.info() << "This is another message" << x << y << z; +std::shared_ptr get(const std::string& name); + + +// Example: +// auto logger = spdlog::create("mylog", {sink1, sink2}); +std::shared_ptr create(const std::string& logger_name, sinks_init_list sinks); + + +// Example (create a logger with daily rotating file): +// using namespace spdlog::sinks; +// spdlog::create("mylog", "dailylog_filename", "txt"); +template +std::shared_ptr create(const std::string& logger_name, const Args&... args); + +// Example: +// using namespace spdlog::sinks; +// std::vector mySinks; +// mySinks.push_back(std::make_shared("filename", "txt", 1024 * 1024 * 5, 10)); +// mySinks.push_back(std::make_shared()); +// spdlog::create("mylog", mySinks.begin(), mySinks.end()); +template +std::shared_ptr create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end); + + +// Set global formatting +// Example: +// spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %t"); +void set_pattern(const std::string& format_string); + + +// Set global formatter object +void set_formatter(formatter_ptr f); + + +//Set global active logging level +void set_level(level::level_enum log_level); + + +//Stop all loggers +void stop(); + + +// +// Trace macro to turn on/off at compile time +// Example: SPDLOG_TRACE(my_logger, "Some trace message"); +// +#ifdef _DEBUG +#define SPDLOG_TRACE(logger, ...) logger->log(__FILE__, " #", __LINE__,": " __VA_ARGS__) +#else +#define SPDLOG_TRACE(logger, ...) {} +#endif + + +} + +#include "details/spdlog_impl.h" \ No newline at end of file diff --git a/include/spitlog/details/logger_impl.h b/include/spitlog/details/logger_impl.h deleted file mode 100644 index a5ebf402..00000000 --- a/include/spitlog/details/logger_impl.h +++ /dev/null @@ -1,163 +0,0 @@ -#pragma once -// -// Logger implementation -// - - -#include "./line_logger.h" - - -inline spitlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list) : - _name(logger_name), - _sinks(sinks_list) -{ - // no support under vs2013 for member initialization for std::atomic - _level = level::INFO; -} - -template -inline spitlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) : - _name(logger_name), - _sinks(begin, end) -{} - - -inline void spitlog::logger::set_formatter(spitlog::formatter_ptr msg_formatter) -{ - _formatter = msg_formatter; -} - -inline void spitlog::logger::set_format(const std::string& format) -{ - _formatter = std::make_shared(format); -} - -inline spitlog::formatter_ptr spitlog::logger::get_formatter() const -{ - return _formatter; -} - - -template -inline spitlog::details::line_logger spitlog::logger::log(level::level_enum lvl, const Args&... args) { - bool msg_enabled = should_log(lvl); - details::line_logger l(this, lvl, msg_enabled); - if (msg_enabled) - _variadic_log(l, args...); - return l; -} - -template -inline spitlog::details::line_logger spitlog::logger::trace(const Args&... args) -{ - return log(level::TRACE, args...); -} - -template -inline spitlog::details::line_logger spitlog::logger::debug(const Args&... args) -{ - return log(level::DEBUG, args...); -} - -template -inline spitlog::details::line_logger spitlog::logger::info(const Args&... args) -{ - return log(level::INFO, args...); -} - -template -inline spitlog::details::line_logger spitlog::logger::warn(const Args&... args) -{ - return log(level::WARN, args...); -} - -template -inline spitlog::details::line_logger spitlog::logger::error(const Args&... args) -{ - return log(level::ERR, args...); -} - -template -inline spitlog::details::line_logger spitlog::logger::critical(const Args&... args) -{ - return log(level::CRITICAL, args...); -} - -inline const std::string& spitlog::logger::name() const -{ - return _name; -} - -inline void spitlog::logger::set_level(spitlog::level::level_enum log_level) -{ - _level.store(log_level); -} - -inline spitlog::level::level_enum spitlog::logger::level() const -{ - return static_cast(_level.load()); -} - -inline bool spitlog::logger::should_log(spitlog::level::level_enum msg_level) const -{ - return msg_level >= _level.load(); -} - -inline void spitlog::logger::_variadic_log(spitlog::details::line_logger&) {} - -template -void spitlog::logger::_variadic_log(spitlog::details::line_logger& l, const First& first, const Rest&... rest) -{ - l.write(first); - l.write(' '); - _variadic_log(l, rest...); -} - -inline void spitlog::logger::_log_msg(details::log_msg& msg) -{ - //Use default formatter if not set - if (!_formatter) - _formatter = std::make_shared("%+"); - _formatter->format(msg); - for (auto &sink : _sinks) - sink->log(msg); -} - -// -// Global registry functions -// -#include "./registry.h" -inline std::shared_ptr spitlog::get(const std::string& name) -{ - return details::registry::instance().get(name); -} - -inline std::shared_ptr spitlog::create(const std::string& logger_name, spitlog::sinks_init_list sinks) -{ - return details::registry::instance().create(logger_name, sinks); -} - - -template -inline std::shared_ptr spitlog::create(const std::string& logger_name, const Args&... args) -{ - sink_ptr sink = std::make_shared(args...); - return details::registry::instance().create(logger_name, { sink }); -} - - -template -inline std::shared_ptr spitlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) -{ - return details::registry::instance().create(logger_name, std::forward(sinks_begin), std::forward(sinks_end)); -} - -inline void spitlog::set_formatter(spitlog::formatter_ptr f) -{ - details::registry::instance().formatter(f); -} - -inline void spitlog::set_format(const std::string& format_string) -{ - return details::registry::instance().set_format(format_string); -}