From c9331594bb2dddf5de9ee6ba79317b67ed049220 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 7 Jul 2018 16:15:17 +0300 Subject: [PATCH] Renamed file to pattern_forammter.h and fixed utc support --- example/example.cpp | 2 +- include/spdlog/details/logger_impl.h | 4 ++-- include/spdlog/details/pattern_formatter.h | 9 +++++---- include/spdlog/details/registry.h | 8 +++++--- include/spdlog/logger.h | 2 +- include/spdlog/sinks/sink.h | 2 +- include/spdlog/spdlog.h | 4 ++-- 7 files changed, 17 insertions(+), 14 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 4a851942..113cf454 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -65,7 +65,7 @@ void stdout_example() auto err_logger = spdlog::stderr_color_mt("error_logger"); err_logger->error("Some error message"); - + // Formatting examples console->warn("Easy padding in numbers like {:08d}", 12); console->critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 5a61a709..dc3dcfa5 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -46,9 +46,9 @@ inline void spdlog::logger::set_formatter(const Args &... args) } } -inline void spdlog::logger::set_pattern(const std::string &pattern, pattern_time_type pattern_time) +inline void spdlog::logger::set_pattern(const std::string &pattern, pattern_time_type time_type) { - set_formatter(pattern, pattern_time); + set_formatter(pattern, time_type); } template diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index 3d54c883..a5ebe923 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -525,10 +525,10 @@ private: class pattern_formatter SPDLOG_FINAL : public formatter { public: - explicit pattern_formatter(const std::string &pattern, pattern_time_type pattern_time = pattern_time_type::local, + explicit pattern_formatter(const std::string &pattern, pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol) : eol_(std::move(eol)) - , pattern_time_(pattern_time) + , pattern_time_type_(time_type) , last_log_secs_(0) { std::memset(&cached_tm_, 0, sizeof(cached_tm_)); @@ -557,14 +557,15 @@ public: private: const std::string eol_; - const pattern_time_type pattern_time_; + pattern_time_type pattern_time_type_; std::tm cached_tm_; std::chrono::seconds last_log_secs_; std::vector> formatters_; + std::tm get_time_(const details::log_msg &msg) { - if (pattern_time_ == pattern_time_type::local) + if (pattern_time_type_ == pattern_time_type::local) { return details::os::localtime(log_clock::to_time_t(msg.time)); } diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 87495de1..6de2f57a 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -48,7 +48,7 @@ public: // create default formatter if not exists - new_logger->set_pattern(formatter_pattern_); + new_logger->set_formatter(formatter_pattern_, pattern_time_type_); if (err_handler_) { @@ -81,13 +81,14 @@ public: return tp_; } - void set_pattern(const std::string &pattern) + void set_pattern(const std::string &pattern, pattern_time_type time_type) { std::lock_guard lock(mutex_); formatter_pattern_ = pattern; + pattern_time_type_ = time_type; for (auto &l : loggers_) { - l.second->set_pattern(pattern); + l.second->set_pattern(pattern, time_type); } } @@ -174,6 +175,7 @@ private: Mutex tp_mutex_; std::unordered_map> loggers_; std::string formatter_pattern_ = "%+"; + pattern_time_type pattern_time_type_ = pattern_time_type::local; level::level_enum level_ = level::info; level::level_enum flush_level_ = level::off; log_err_handler err_handler_; diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index f67a07ff..5d67bf8d 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -117,7 +117,7 @@ public: // create a pattern formatter all the sinks in this logger. // each sink gets itw own private copy of a formatter object. - void set_pattern(const std::string &pattern, pattern_time_type pattern_time = pattern_time_type::local); + void set_pattern(const std::string &pattern, pattern_time_type time_type = pattern_time_type::local); // create a FormatterT formatter for each sink in this logger. // each sink gets its own private copy of a formatter object. diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index 2c2a764c..39eb0abb 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -6,7 +6,7 @@ #pragma once #include "spdlog/details/log_msg.h" -#include "spdlog/details/pattern_formatter_impl.h" +#include "spdlog/details/pattern_formatter.h" #include "spdlog/formatter.h" namespace spdlog { diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 53c56a30..90148d6f 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -57,9 +57,9 @@ inline std::shared_ptr get(const std::string &name) // Set global formatting // example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v"); // -inline void set_pattern(const std::string &format_string) +inline void set_pattern(const std::string &format_string, pattern_time_type time_type = pattern_time_type::local) { - details::registry::instance().set_pattern(format_string); + details::registry::instance().set_pattern(format_string, time_type); } //