Commit d98d5489 authored by ThePhD's avatar ThePhD

use if statement instead of switch (changes of adding new time specifications...

use if statement instead of switch (changes of adding new time specifications outside of standard are probably unlikely anyhow)
pattern_time -> pattern_time_type
ptime variable name -> pattern_time variable name
make sure four spaces used, not tabs
parent 18a0455b
...@@ -71,7 +71,7 @@ public: ...@@ -71,7 +71,7 @@ public:
protected: protected:
void _sink_it(details::log_msg& msg) override; void _sink_it(details::log_msg& msg) override;
void _set_formatter(spdlog::formatter_ptr msg_formatter) override; void _set_formatter(spdlog::formatter_ptr msg_formatter) override;
void _set_pattern(const std::string& pattern, pattern_time ptime) override; void _set_pattern(const std::string& pattern, pattern_time_type pattern_time) override;
private: private:
std::unique_ptr<details::async_log_helper> _async_log_helper; std::unique_ptr<details::async_log_helper> _async_log_helper;
......
...@@ -110,10 +110,10 @@ enum class async_overflow_policy ...@@ -110,10 +110,10 @@ enum class async_overflow_policy
// Pattern time - specific time getting to use for pattern_formatter. // Pattern time - specific time getting to use for pattern_formatter.
// local time by default // local time by default
// //
enum class pattern_time enum class pattern_time_type
{ {
local, // use variant of time similar to std::localtime for std::tm local, // use variant of time similar to std::localtime for std::tm
utc // use variant of time similar to std::gmtime for std::tm utc // use variant of time similar to std::gmtime for std::tm
}; };
// //
......
...@@ -76,9 +76,9 @@ inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_forma ...@@ -76,9 +76,9 @@ inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_forma
_async_log_helper->set_formatter(_formatter); _async_log_helper->set_formatter(_formatter);
} }
inline void spdlog::async_logger::_set_pattern(const std::string& pattern, pattern_time ptime) inline void spdlog::async_logger::_set_pattern(const std::string& pattern, pattern_time_type pattern_time)
{ {
_formatter = std::make_shared<pattern_formatter>(pattern, ptime); _formatter = std::make_shared<pattern_formatter>(pattern, pattern_time);
_async_log_helper->set_formatter(_formatter); _async_log_helper->set_formatter(_formatter);
} }
......
...@@ -53,9 +53,9 @@ inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter) ...@@ -53,9 +53,9 @@ inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
_set_formatter(msg_formatter); _set_formatter(msg_formatter);
} }
inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time ptime) inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time_type pattern_time)
{ {
_set_pattern(pattern, ptime); _set_pattern(pattern, pattern_time);
} }
...@@ -316,9 +316,9 @@ inline void spdlog::logger::_sink_it(details::log_msg& msg) ...@@ -316,9 +316,9 @@ inline void spdlog::logger::_sink_it(details::log_msg& msg)
flush(); flush();
} }
inline void spdlog::logger::_set_pattern(const std::string& pattern, pattern_time ptime) inline void spdlog::logger::_set_pattern(const std::string& pattern, pattern_time_type pattern_time)
{ {
_formatter = std::make_shared<pattern_formatter>(pattern, ptime); _formatter = std::make_shared<pattern_formatter>(pattern, pattern_time);
} }
inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter) inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter)
{ {
......
...@@ -311,15 +311,7 @@ class R_formatter SPDLOG_FINAL:public flag_formatter ...@@ -311,15 +311,7 @@ class R_formatter SPDLOG_FINAL:public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm& tm_time) override void format(details::log_msg& msg, const std::tm& tm_time) override
{ {
msg.formatted << (tm_time.tm_year + 1900); pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':');
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, ':');
} }
}; };
...@@ -502,13 +494,13 @@ class full_formatter SPDLOG_FINAL:public flag_formatter ...@@ -502,13 +494,13 @@ class full_formatter SPDLOG_FINAL:public flag_formatter
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// pattern_formatter inline impl // pattern_formatter inline impl
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern, pattern_time ptime) inline spdlog::pattern_formatter::pattern_formatter(const std::string& pattern, pattern_time_type pattern_time)
: _time(ptime) : _pattern_time(pattern_time)
{ {
compile_pattern(pattern, _time); compile_pattern(pattern);
} }
inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern, pattern_time ptime) inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern)
{ {
auto end = pattern.end(); auto end = pattern.end();
std::unique_ptr<details::aggregate_formatter> user_chars; std::unique_ptr<details::aggregate_formatter> user_chars;
...@@ -673,26 +665,18 @@ inline void spdlog::pattern_formatter::handle_flag(char flag) ...@@ -673,26 +665,18 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
} }
} }
inline std::tm spdlog::pattern_formatter::get_time(details::log_msg& msg) {
if (_pattern_time == pattern_time_type::local)
return details::os::localtime(log_clock::to_time_t(msg.time));
else
return details::os::gmtime(log_clock::to_time_t(msg.time));
}
inline void spdlog::pattern_formatter::format(details::log_msg& msg) inline void spdlog::pattern_formatter::format(details::log_msg& msg)
{ {
#ifndef SPDLOG_NO_DATETIME #ifndef SPDLOG_NO_DATETIME
auto tm_time = [this, &msg]() auto tm_time = get_time(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 #else
std::tm tm_time; std::tm tm_time;
#endif #endif
......
...@@ -29,16 +29,17 @@ class pattern_formatter SPDLOG_FINAL : public formatter ...@@ -29,16 +29,17 @@ class pattern_formatter SPDLOG_FINAL : public formatter
{ {
public: public:
explicit pattern_formatter(const std::string& pattern, pattern_time ptime = pattern_time::local); explicit pattern_formatter(const std::string& pattern, pattern_time_type pattern_time = pattern_time_type::local);
pattern_formatter(const pattern_formatter&) = delete; pattern_formatter(const pattern_formatter&) = delete;
pattern_formatter& operator=(const pattern_formatter&) = delete; pattern_formatter& operator=(const pattern_formatter&) = delete;
void format(details::log_msg& msg) override; void format(details::log_msg& msg) override;
private: private:
const std::string _pattern; const std::string _pattern;
const pattern_time _time; const pattern_time_type _pattern_time;
std::vector<std::unique_ptr<details::flag_formatter>> _formatters; std::vector<std::unique_ptr<details::flag_formatter>> _formatters;
std::tm get_time(details::log_msg& msg);
void handle_flag(char flag); void handle_flag(char flag);
void compile_pattern(const std::string& pattern, pattern_time ptime); void compile_pattern(const std::string& pattern);
}; };
} }
......
...@@ -66,7 +66,7 @@ public: ...@@ -66,7 +66,7 @@ public:
void set_level(level::level_enum); void set_level(level::level_enum);
level::level_enum level() const; level::level_enum level() const;
const std::string& name() const; const std::string& name() const;
void set_pattern(const std::string&, pattern_time = pattern_time::local); void set_pattern(const std::string&, pattern_time_type = pattern_time_type::local);
void set_formatter(formatter_ptr); void set_formatter(formatter_ptr);
// automatically call flush() if message level >= log_level // automatically call flush() if message level >= log_level
...@@ -82,7 +82,7 @@ public: ...@@ -82,7 +82,7 @@ public:
protected: protected:
virtual void _sink_it(details::log_msg&); virtual void _sink_it(details::log_msg&);
virtual void _set_pattern(const std::string&, pattern_time); virtual void _set_pattern(const std::string&, pattern_time_type);
virtual void _set_formatter(formatter_ptr); virtual void _set_formatter(formatter_ptr);
// default error handler: print the error to stderr with the max rate of 1 message/minute // default error handler: print the error to stderr with the max rate of 1 message/minute
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment