mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-23 14:12:06 +08:00
defer localtime to formatter to improve async performace
This commit is contained in:
parent
c215ee6104
commit
754cac85ac
@ -4,7 +4,7 @@ CXX_RELEASE_FLAGS = -O3 -flto
|
|||||||
CXX_DEBUG_FLAGS= -g
|
CXX_DEBUG_FLAGS= -g
|
||||||
|
|
||||||
|
|
||||||
all: example bench
|
all: bench
|
||||||
debug: example-debug bench-debug
|
debug: example-debug bench-debug
|
||||||
|
|
||||||
example: example.cpp
|
example: example.cpp
|
||||||
|
@ -64,6 +64,7 @@ int main(int argc, char* argv[])
|
|||||||
if (argc > 2)
|
if (argc > 2)
|
||||||
threads = atoi(argv[2]);
|
threads = atoi(argv[2]);
|
||||||
|
|
||||||
|
|
||||||
cout << "*******************************************************************************\n";
|
cout << "*******************************************************************************\n";
|
||||||
cout << "Single thread, " << format(howmany) << " iterations, auto flush=" << auto_flush << endl;
|
cout << "Single thread, " << format(howmany) << " iterations, auto flush=" << auto_flush << endl;
|
||||||
cout << "*******************************************************************************\n";
|
cout << "*******************************************************************************\n";
|
||||||
@ -92,8 +93,15 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
|
|
||||||
spdlog::set_async_mode(howmany);
|
spdlog::set_async_mode(howmany);
|
||||||
auto daily_st_async = spdlog::daily_logger_st("daily_async", "logs/daily_async", auto_flush);
|
|
||||||
bench_mt(howmany, daily_st_async, threads);
|
for(int i = 0; i < 3; ++i)
|
||||||
|
{
|
||||||
|
auto as = spdlog::daily_logger_st("as", "logs/daily_async", auto_flush);
|
||||||
|
//bench_mt(howmany, spdlog::create<null_sink_st>("as"), threads);
|
||||||
|
bench_mt(howmany, as, threads);
|
||||||
|
as->stop();
|
||||||
|
spdlog::drop("as");
|
||||||
|
}
|
||||||
|
|
||||||
spdlog::stop();
|
spdlog::stop();
|
||||||
|
|
||||||
|
@ -56,7 +56,6 @@ class async_log_helper
|
|||||||
std::string logger_name;
|
std::string logger_name;
|
||||||
level::level_enum level;
|
level::level_enum level;
|
||||||
log_clock::time_point time;
|
log_clock::time_point time;
|
||||||
std::tm tm_time;
|
|
||||||
std::string raw_msg_str;
|
std::string raw_msg_str;
|
||||||
|
|
||||||
async_msg() = default;
|
async_msg() = default;
|
||||||
@ -65,12 +64,8 @@ class async_log_helper
|
|||||||
logger_name(m.logger_name),
|
logger_name(m.logger_name),
|
||||||
level(m.level),
|
level(m.level),
|
||||||
time(m.time),
|
time(m.time),
|
||||||
tm_time(m.tm_time),
|
|
||||||
raw_msg_str(m.raw.data(), m.raw.size())
|
raw_msg_str(m.raw.data(), m.raw.size())
|
||||||
{
|
{}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
log_msg to_log_msg()
|
log_msg to_log_msg()
|
||||||
{
|
{
|
||||||
@ -78,7 +73,6 @@ class async_log_helper
|
|||||||
msg.logger_name = logger_name;
|
msg.logger_name = logger_name;
|
||||||
msg.level = level;
|
msg.level = level;
|
||||||
msg.time = time;
|
msg.time = time;
|
||||||
msg.tm_time = tm_time;
|
|
||||||
msg.raw << raw_msg_str;
|
msg.raw << raw_msg_str;
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,7 @@ public:
|
|||||||
{
|
{
|
||||||
_log_msg.logger_name = _callback_logger->name();
|
_log_msg.logger_name = _callback_logger->name();
|
||||||
_log_msg.time = log_clock::now();
|
_log_msg.time = log_clock::now();
|
||||||
_log_msg.tm_time = details::os::localtime(log_clock::to_time_t(_log_msg.time));
|
//_log_msg.tm_time = details::os::localtime(log_clock::to_time_t(_log_msg.time));
|
||||||
_callback_logger->_log_msg(_log_msg);
|
_callback_logger->_log_msg(_log_msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,6 @@ struct log_msg
|
|||||||
logger_name(),
|
logger_name(),
|
||||||
level(l),
|
level(l),
|
||||||
time(),
|
time(),
|
||||||
tm_time(),
|
|
||||||
raw(),
|
raw(),
|
||||||
formatted() {}
|
formatted() {}
|
||||||
|
|
||||||
@ -46,9 +45,7 @@ struct log_msg
|
|||||||
log_msg(const log_msg& other) :
|
log_msg(const log_msg& other) :
|
||||||
logger_name(other.logger_name),
|
logger_name(other.logger_name),
|
||||||
level(other.level),
|
level(other.level),
|
||||||
time(other.time),
|
time(other.time)
|
||||||
tm_time(other.tm_time)
|
|
||||||
|
|
||||||
{
|
{
|
||||||
if (other.raw.size())
|
if (other.raw.size())
|
||||||
raw << fmt::BasicStringRef<char>(other.raw.data(), other.raw.size());
|
raw << fmt::BasicStringRef<char>(other.raw.data(), other.raw.size());
|
||||||
@ -61,7 +58,6 @@ struct log_msg
|
|||||||
logger_name(std::move(other.logger_name)),
|
logger_name(std::move(other.logger_name)),
|
||||||
level(other.level),
|
level(other.level),
|
||||||
time(std::move(other.time)),
|
time(std::move(other.time)),
|
||||||
tm_time(other.tm_time),
|
|
||||||
raw(std::move(other.raw)),
|
raw(std::move(other.raw)),
|
||||||
formatted(std::move(other.formatted))
|
formatted(std::move(other.formatted))
|
||||||
{
|
{
|
||||||
@ -76,7 +72,6 @@ struct log_msg
|
|||||||
logger_name = std::move(other.logger_name);
|
logger_name = std::move(other.logger_name);
|
||||||
level = other.level;
|
level = other.level;
|
||||||
time = std::move(other.time);
|
time = std::move(other.time);
|
||||||
tm_time = other.tm_time;
|
|
||||||
raw = std::move(other.raw);
|
raw = std::move(other.raw);
|
||||||
formatted = std::move(other.formatted);
|
formatted = std::move(other.formatted);
|
||||||
other.clear();
|
other.clear();
|
||||||
@ -95,7 +90,7 @@ struct log_msg
|
|||||||
std::string logger_name;
|
std::string logger_name;
|
||||||
level::level_enum level;
|
level::level_enum level;
|
||||||
log_clock::time_point time;
|
log_clock::time_point time;
|
||||||
std::tm tm_time;
|
//std::tm tm_time;
|
||||||
fmt::MemoryWriter raw;
|
fmt::MemoryWriter raw;
|
||||||
fmt::MemoryWriter formatted;
|
fmt::MemoryWriter formatted;
|
||||||
};
|
};
|
||||||
|
@ -43,7 +43,7 @@ class flag_formatter
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~flag_formatter() {}
|
virtual ~flag_formatter() {}
|
||||||
virtual void format(details::log_msg& msg) = 0;
|
virtual void format(details::log_msg& msg, const std::tm& tm_time) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
@ -53,7 +53,7 @@ namespace
|
|||||||
{
|
{
|
||||||
class name_formatter :public flag_formatter
|
class name_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
msg.formatted << msg.logger_name;
|
msg.formatted << msg.logger_name;
|
||||||
}
|
}
|
||||||
@ -63,7 +63,7 @@ class name_formatter :public flag_formatter
|
|||||||
// log level appender
|
// log level appender
|
||||||
class level_formatter :public flag_formatter
|
class level_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
msg.formatted << level::to_str(msg.level);
|
msg.formatted << level::to_str(msg.level);
|
||||||
}
|
}
|
||||||
@ -87,9 +87,9 @@ static int to12h(const tm& t)
|
|||||||
static const std::string days[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
static const std::string days[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
||||||
class a_formatter :public flag_formatter
|
class a_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << days[msg.tm_time.tm_wday];
|
msg.formatted << days[tm_time.tm_wday];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -97,9 +97,9 @@ class a_formatter :public flag_formatter
|
|||||||
static const std::string full_days[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
|
static const std::string full_days[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
|
||||||
class A_formatter :public flag_formatter
|
class A_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << full_days[msg.tm_time.tm_wday];
|
msg.formatted << full_days[tm_time.tm_wday];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -107,9 +107,9 @@ class A_formatter :public flag_formatter
|
|||||||
static const std::string months[] { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" };
|
static const std::string months[] { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec" };
|
||||||
class b_formatter :public flag_formatter
|
class b_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted<< months[msg.tm_time.tm_mon];
|
msg.formatted<< months[tm_time.tm_mon];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -117,9 +117,9 @@ class b_formatter :public flag_formatter
|
|||||||
static const std::string full_months[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
|
static const std::string full_months[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
|
||||||
class B_formatter :public flag_formatter
|
class B_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << full_months[msg.tm_time.tm_mon];
|
msg.formatted << full_months[tm_time.tm_mon];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -139,15 +139,13 @@ static fmt::MemoryWriter& pad_n_join(fmt::MemoryWriter& w, int v1, int v2, int v
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Date and time representation (Thu Aug 23 15:35:46 2014)
|
//Date and time representation (Thu Aug 23 15:35:46 2014)
|
||||||
class c_formatter :public flag_formatter
|
class c_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << days[msg.tm_time.tm_wday] << ' ' << months[msg.tm_time.tm_mon] << ' ' << msg.tm_time.tm_mday << ' ';
|
msg.formatted << days[tm_time.tm_wday] << ' ' << months[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' ';
|
||||||
pad_n_join(msg.formatted, msg.tm_time.tm_hour, msg.tm_time.tm_min, msg.tm_time.tm_sec, ':') << ' ' << msg.tm_time.tm_year + 1900;
|
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << tm_time.tm_year + 1900;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -155,9 +153,9 @@ class c_formatter :public flag_formatter
|
|||||||
// year - 2 digit
|
// year - 2 digit
|
||||||
class C_formatter :public flag_formatter
|
class C_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << fmt::pad(msg.tm_time.tm_year % 100, 2, '0');
|
msg.formatted << fmt::pad(tm_time.tm_year % 100, 2, '0');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -166,9 +164,9 @@ class C_formatter :public flag_formatter
|
|||||||
// Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
|
// Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
|
||||||
class D_formatter :public flag_formatter
|
class D_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
pad_n_join(msg.formatted, msg.tm_time.tm_mon + 1, msg.tm_time.tm_mday, msg.tm_time.tm_year % 100, '/');
|
pad_n_join(msg.formatted, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_year % 100, '/');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -176,70 +174,70 @@ class D_formatter :public flag_formatter
|
|||||||
// year - 4 digit
|
// year - 4 digit
|
||||||
class Y_formatter :public flag_formatter
|
class Y_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << msg.tm_time.tm_year + 1900;
|
msg.formatted << tm_time.tm_year + 1900;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// month 1-12
|
// month 1-12
|
||||||
class m_formatter :public flag_formatter
|
class m_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << fmt::pad(msg.tm_time.tm_mon + 1, 2, '0');
|
msg.formatted << fmt::pad(tm_time.tm_mon + 1, 2, '0');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// day of month 1-31
|
// day of month 1-31
|
||||||
class d_formatter :public flag_formatter
|
class d_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << fmt::pad(msg.tm_time.tm_mday, 2, '0');
|
msg.formatted << fmt::pad(tm_time.tm_mday, 2, '0');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// hours in 24 format 0-23
|
// hours in 24 format 0-23
|
||||||
class H_formatter :public flag_formatter
|
class H_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << fmt::pad(msg.tm_time.tm_hour, 2, '0');
|
msg.formatted << fmt::pad(tm_time.tm_hour, 2, '0');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// hours in 12 format 1-12
|
// hours in 12 format 1-12
|
||||||
class I_formatter :public flag_formatter
|
class I_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << fmt::pad(to12h(msg.tm_time), 2, '0');
|
msg.formatted << fmt::pad(to12h(tm_time), 2, '0');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// ninutes 0-59
|
// ninutes 0-59
|
||||||
class M_formatter :public flag_formatter
|
class M_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << fmt::pad(msg.tm_time.tm_min, 2, '0');
|
msg.formatted << fmt::pad(tm_time.tm_min, 2, '0');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// seconds 0-59
|
// seconds 0-59
|
||||||
class S_formatter :public flag_formatter
|
class S_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << fmt::pad(msg.tm_time.tm_sec, 2, '0');
|
msg.formatted << fmt::pad(tm_time.tm_sec, 2, '0');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// milliseconds
|
// milliseconds
|
||||||
class e_formatter :public flag_formatter
|
class e_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
auto duration = msg.time.time_since_epoch();
|
auto duration = msg.time.time_since_epoch();
|
||||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
|
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
|
||||||
@ -250,9 +248,9 @@ class e_formatter :public flag_formatter
|
|||||||
// AM/PM
|
// AM/PM
|
||||||
class p_formatter :public flag_formatter
|
class p_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
msg.formatted << ampm(msg.tm_time);
|
msg.formatted << ampm(tm_time);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -260,27 +258,27 @@ class p_formatter :public flag_formatter
|
|||||||
// 12 hour clock 02:55:02 pm
|
// 12 hour clock 02:55:02 pm
|
||||||
class r_formatter :public flag_formatter
|
class r_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
pad_n_join(msg.formatted, to12h(msg.tm_time), msg.tm_time.tm_min, msg.tm_time.tm_sec, ':') << ' ' << ampm(msg.tm_time);
|
pad_n_join(msg.formatted, to12h(tm_time), tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << ampm(tm_time);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 24-hour HH:MM time, equivalent to %H:%M
|
// 24-hour HH:MM time, equivalent to %H:%M
|
||||||
class R_formatter :public flag_formatter
|
class R_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
pad_n_join(msg.formatted, msg.tm_time.tm_hour, msg.tm_time.tm_min, ':');
|
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
|
// ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
|
||||||
class T_formatter :public flag_formatter
|
class T_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
pad_n_join(msg.formatted, msg.tm_time.tm_hour, msg.tm_time.tm_min, msg.tm_time.tm_sec, ':');
|
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -295,14 +293,14 @@ public:
|
|||||||
z_formatter(const z_formatter&) = delete;
|
z_formatter(const z_formatter&) = delete;
|
||||||
z_formatter& operator=(const z_formatter&) = delete;
|
z_formatter& operator=(const z_formatter&) = delete;
|
||||||
|
|
||||||
void format(log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
int total_minutes = get_cached_offset(msg);
|
int total_minutes = get_cached_offset(msg, tm_time);
|
||||||
#else
|
#else
|
||||||
// No need to chache under gcc,
|
// No need to chache under gcc,
|
||||||
// it is very fast (already stored in tm.tm_gmtoff)
|
// it is very fast (already stored in tm.tm_gmtoff)
|
||||||
int total_minutes = os::utc_minutes_offset(msg.tm_time);
|
int total_minutes = os::utc_minutes_offset(tm_time);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int h = total_minutes / 60;
|
int h = total_minutes / 60;
|
||||||
@ -316,13 +314,13 @@ private:
|
|||||||
int _offset_minutes;
|
int _offset_minutes;
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
|
|
||||||
int get_cached_offset(const log_msg& msg)
|
int get_cached_offset(const log_msg& msg, const std::tm& tm_time)
|
||||||
{
|
{
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
std::lock_guard<std::mutex> l(_mutex);
|
std::lock_guard<std::mutex> l(_mutex);
|
||||||
if (msg.time - _last_update >= cache_refresh)
|
if (msg.time - _last_update >= cache_refresh)
|
||||||
{
|
{
|
||||||
_offset_minutes = os::utc_minutes_offset(msg.tm_time);
|
_offset_minutes = os::utc_minutes_offset(tm_time);
|
||||||
_last_update = msg.time;
|
_last_update = msg.time;
|
||||||
}
|
}
|
||||||
return _offset_minutes;
|
return _offset_minutes;
|
||||||
@ -334,7 +332,7 @@ private:
|
|||||||
//Thread id
|
//Thread id
|
||||||
class t_formatter :public flag_formatter
|
class t_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
msg.formatted << std::hash<std::thread::id>()(std::this_thread::get_id());
|
msg.formatted << std::hash<std::thread::id>()(std::this_thread::get_id());
|
||||||
}
|
}
|
||||||
@ -343,7 +341,7 @@ class t_formatter :public flag_formatter
|
|||||||
|
|
||||||
class v_formatter :public flag_formatter
|
class v_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
|
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
|
||||||
}
|
}
|
||||||
@ -354,7 +352,7 @@ class ch_formatter :public flag_formatter
|
|||||||
public:
|
public:
|
||||||
explicit ch_formatter(char ch) : _ch(ch)
|
explicit ch_formatter(char ch) : _ch(ch)
|
||||||
{}
|
{}
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
msg.formatted << _ch;
|
msg.formatted << _ch;
|
||||||
}
|
}
|
||||||
@ -373,7 +371,7 @@ public:
|
|||||||
{
|
{
|
||||||
_str += ch;
|
_str += ch;
|
||||||
}
|
}
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm&) override
|
||||||
{
|
{
|
||||||
msg.formatted << _str;
|
msg.formatted << _str;
|
||||||
}
|
}
|
||||||
@ -385,31 +383,31 @@ private:
|
|||||||
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
|
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
|
||||||
class full_formatter :public flag_formatter
|
class full_formatter :public flag_formatter
|
||||||
{
|
{
|
||||||
void format(details::log_msg& msg) override
|
void format(details::log_msg& msg, const std::tm& tm_time) override
|
||||||
{
|
{
|
||||||
auto duration = msg.time.time_since_epoch();
|
auto duration = msg.time.time_since_epoch();
|
||||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
|
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
|
||||||
|
|
||||||
/* Slower version(while still very fast - about 3.2 million lines/sec under 10 threads),
|
/* Slower version(while still very fast - about 3.2 million lines/sec under 10 threads),
|
||||||
msg.formatted.write("[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] {} ",
|
msg.formatted.write("[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] {} ",
|
||||||
msg.tm_time.tm_year + 1900,
|
tm_time.tm_year + 1900,
|
||||||
msg.tm_time.tm_mon + 1,
|
tm_time.tm_mon + 1,
|
||||||
msg.tm_time.tm_mday,
|
tm_time.tm_mday,
|
||||||
msg.tm_time.tm_hour,
|
tm_time.tm_hour,
|
||||||
msg.tm_time.tm_min,
|
tm_time.tm_min,
|
||||||
msg.tm_time.tm_sec,
|
tm_time.tm_sec,
|
||||||
static_cast<int>(millis),
|
static_cast<int>(millis),
|
||||||
msg.logger_name,
|
msg.logger_name,
|
||||||
level::to_str(msg.level),
|
level::to_str(msg.level),
|
||||||
msg.raw.str());*/
|
msg.raw.str());*/
|
||||||
|
|
||||||
// Faster (albeit uglier) way to format the line (5.6 million lines/sec under 10 threads)
|
// Faster (albeit uglier) way to format the line (5.6 million lines/sec under 10 threads)
|
||||||
msg.formatted << '[' << static_cast<unsigned int>(msg.tm_time.tm_year + 1900) << '-'
|
msg.formatted << '[' << static_cast<unsigned int>(tm_time.tm_year + 1900) << '-'
|
||||||
<< fmt::pad(static_cast<unsigned int>(msg.tm_time.tm_mon + 1), 2, '0') << '-'
|
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_mon + 1), 2, '0') << '-'
|
||||||
<< fmt::pad(static_cast<unsigned int>(msg.tm_time.tm_mday), 2, '0') << ' '
|
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_mday), 2, '0') << ' '
|
||||||
<< fmt::pad(static_cast<unsigned int>(msg.tm_time.tm_hour), 2, '0') << ':'
|
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_hour), 2, '0') << ':'
|
||||||
<< fmt::pad(static_cast<unsigned int>(msg.tm_time.tm_min), 2, '0') << ':'
|
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_min), 2, '0') << ':'
|
||||||
<< fmt::pad(static_cast<unsigned int>(msg.tm_time.tm_sec), 2, '0') << '.'
|
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_sec), 2, '0') << '.'
|
||||||
<< fmt::pad(static_cast<unsigned int>(millis), 3, '0') << "] ";
|
<< fmt::pad(static_cast<unsigned int>(millis), 3, '0') << "] ";
|
||||||
|
|
||||||
msg.formatted << '[' << msg.logger_name << "] [" << level::to_str(msg.level) << "] ";
|
msg.formatted << '[' << msg.logger_name << "] [" << level::to_str(msg.level) << "] ";
|
||||||
@ -576,9 +574,10 @@ inline void spdlog::pattern_formatter::format(details::log_msg& msg)
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
auto tm_time = details::os::localtime(log_clock::to_time_t(msg.time));
|
||||||
for (auto &f : _formatters)
|
for (auto &f : _formatters)
|
||||||
{
|
{
|
||||||
f->format(msg);
|
f->format(msg, tm_time);
|
||||||
}
|
}
|
||||||
//write eol
|
//write eol
|
||||||
msg.formatted << details::os::eol();
|
msg.formatted << details::os::eol();
|
||||||
|
Loading…
Reference in New Issue
Block a user