mirror of
https://github.com/gabime/spdlog.git
synced 2026-04-10 11:34:29 +08:00
implement a flag (in this case, an enumeration) that allows control over the type; we make it an enum for possible expansions of time abstractions that might make it into the C++ standard in the future (see Howard Hinnant's date/timezone library) or might be usefully-available from the OS at some point in time
This commit is contained in:
@@ -311,7 +311,15 @@ class R_formatter SPDLOG_FINAL:public flag_formatter
|
||||
{
|
||||
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||
{
|
||||
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':');
|
||||
msg.formatted << (tm_time.tm_year + 1900);
|
||||
msg.formatted << '-';
|
||||
msg.formatted << tm_time.tm_mon;
|
||||
msg.formatted << '-';
|
||||
msg.formatted << tm_time.tm_mday;
|
||||
|
||||
msg.formatted << ' ';
|
||||
|
||||
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -324,7 +332,6 @@ class T_formatter SPDLOG_FINAL:public flag_formatter
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// ISO 8601 offset from UTC in timezone (+-HH:MM)
|
||||
class z_formatter SPDLOG_FINAL:public flag_formatter
|
||||
{
|
||||
@@ -495,12 +502,13 @@ class full_formatter SPDLOG_FINAL:public flag_formatter
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// pattern_formatter inline impl
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern)
|
||||
inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern, pattern_time ptime)
|
||||
: _time(ptime), _pattern(pattern)
|
||||
{
|
||||
compile_pattern(pattern);
|
||||
compile_pattern(_pattern, _time);
|
||||
}
|
||||
|
||||
inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern)
|
||||
inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern, pattern_time ptime)
|
||||
{
|
||||
auto end = pattern.end();
|
||||
std::unique_ptr<details::aggregate_formatter> user_chars;
|
||||
@@ -670,7 +678,21 @@ inline void spdlog::pattern_formatter::format(details::log_msg& msg)
|
||||
{
|
||||
|
||||
#ifndef SPDLOG_NO_DATETIME
|
||||
auto tm_time = details::os::localtime(log_clock::to_time_t(msg.time));
|
||||
auto tm_time = [this, &msg]()
|
||||
{
|
||||
switch (_time)
|
||||
{
|
||||
// it is always faster to put the most-common/default case first
|
||||
case (pattern_time::local):
|
||||
return details::os::localtime(log_clock::to_time_t(msg.time));
|
||||
|
||||
case (pattern_time::utc):
|
||||
return details::os::gmtime(log_clock::to_time_t(msg.time));
|
||||
|
||||
default:
|
||||
return details::os::localtime(log_clock::to_time_t(msg.time));
|
||||
}
|
||||
}();
|
||||
#else
|
||||
std::tm tm_time;
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user