Renamed file to pattern_forammter.h and fixed utc support

This commit is contained in:
gabime 2018-07-07 16:15:17 +03:00
parent cbc4db8649
commit c9331594bb
7 changed files with 17 additions and 14 deletions

View File

@ -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);

View File

@ -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<spdlog::pattern_formatter>(pattern, pattern_time);
set_formatter<spdlog::pattern_formatter>(pattern, time_type);
}
template<typename... Args>

View File

@ -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<std::unique_ptr<details::flag_formatter>> 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));
}

View File

@ -48,7 +48,7 @@ public:
// create default formatter if not exists
new_logger->set_pattern(formatter_pattern_);
new_logger->set_formatter<pattern_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<Mutex> 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<std::string, std::shared_ptr<logger>> 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_;

View File

@ -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.

View File

@ -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 {

View File

@ -57,9 +57,9 @@ inline std::shared_ptr<logger> 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);
}
//