2019-09-15 23:34:29 +08:00
|
|
|
/*
|
2023-09-25 21:05:07 +08:00
|
|
|
* This content is released under the MIT License as specified in
|
|
|
|
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
2019-09-15 23:34:29 +08:00
|
|
|
*/
|
|
|
|
#include "includes.h"
|
|
|
|
|
2022-04-22 10:43:13 +08:00
|
|
|
#ifdef SPDLOG_USE_STD_FORMAT
|
2021-11-14 00:29:05 +08:00
|
|
|
using filename_memory_buf_t = std::basic_string<spdlog::filename_t::value_type>;
|
2022-04-22 10:43:13 +08:00
|
|
|
#else
|
|
|
|
using filename_memory_buf_t = fmt::basic_memory_buffer<spdlog::filename_t::value_type, 250>;
|
|
|
|
#endif
|
2022-04-22 09:46:58 +08:00
|
|
|
|
2022-04-22 10:43:13 +08:00
|
|
|
#ifdef SPDLOG_WCHAR_FILENAMES
|
2023-09-25 21:05:07 +08:00
|
|
|
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w) {
|
2022-04-22 09:46:58 +08:00
|
|
|
spdlog::memory_buf_t buf;
|
2022-04-22 11:38:12 +08:00
|
|
|
spdlog::details::os::wstr_to_utf8buf(spdlog::wstring_view_t(w.data(), w.size()), buf);
|
2022-04-27 11:25:35 +08:00
|
|
|
return SPDLOG_BUF_TO_STRING(buf);
|
2022-04-22 09:46:58 +08:00
|
|
|
}
|
2021-11-14 00:29:05 +08:00
|
|
|
#else
|
2023-09-25 21:05:07 +08:00
|
|
|
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w) {
|
2022-04-27 11:25:35 +08:00
|
|
|
return SPDLOG_BUF_TO_STRING(w);
|
2022-04-22 09:46:58 +08:00
|
|
|
}
|
2021-11-14 00:29:05 +08:00
|
|
|
#endif
|
2021-01-11 07:19:28 +08:00
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
|
|
|
|
using sink_type =
|
|
|
|
spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>;
|
2019-09-15 23:34:29 +08:00
|
|
|
|
|
|
|
prepare_logdir();
|
|
|
|
|
|
|
|
// calculate filename (time based)
|
2021-01-06 16:55:46 +08:00
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
|
2019-09-15 23:34:29 +08:00
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
2021-01-11 07:19:28 +08:00
|
|
|
filename_memory_buf_t w;
|
2023-09-25 21:05:07 +08:00
|
|
|
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}"),
|
|
|
|
basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
2019-09-15 23:34:29 +08:00
|
|
|
|
|
|
|
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
2023-09-25 21:05:07 +08:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2019-09-15 23:34:29 +08:00
|
|
|
logger->info("Test message {}", i);
|
|
|
|
}
|
|
|
|
logger->flush();
|
|
|
|
|
2022-04-27 11:25:35 +08:00
|
|
|
require_message_count(filename_buf_to_utf8string(w), 10);
|
2019-09-15 23:34:29 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
struct custom_daily_file_name_calculator {
|
|
|
|
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm) {
|
2021-01-11 07:19:28 +08:00
|
|
|
filename_memory_buf_t w;
|
2023-09-25 21:05:07 +08:00
|
|
|
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"),
|
|
|
|
basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1,
|
|
|
|
now_tm.tm_mday);
|
2022-04-22 09:46:58 +08:00
|
|
|
|
2022-04-27 11:25:35 +08:00
|
|
|
return SPDLOG_BUF_TO_STRING(w);
|
2019-09-15 23:34:29 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("daily_logger with custom calculator", "[daily_logger]") {
|
2019-09-15 23:34:29 +08:00
|
|
|
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, custom_daily_file_name_calculator>;
|
|
|
|
|
|
|
|
prepare_logdir();
|
|
|
|
|
|
|
|
// calculate filename (time based)
|
2021-01-06 16:55:46 +08:00
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
|
2019-09-15 23:34:29 +08:00
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
2021-01-11 07:19:28 +08:00
|
|
|
filename_memory_buf_t w;
|
2023-09-25 21:05:07 +08:00
|
|
|
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"),
|
|
|
|
basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
2019-09-15 23:34:29 +08:00
|
|
|
|
|
|
|
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
2023-09-25 21:05:07 +08:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2019-09-15 23:34:29 +08:00
|
|
|
logger->info("Test message {}", i);
|
|
|
|
}
|
|
|
|
|
2020-02-05 05:23:36 +08:00
|
|
|
logger->flush();
|
2019-09-15 23:34:29 +08:00
|
|
|
|
2022-04-27 11:25:35 +08:00
|
|
|
require_message_count(filename_buf_to_utf8string(w), 10);
|
2019-09-15 23:34:29 +08:00
|
|
|
}
|
|
|
|
|
2020-06-07 19:38:16 +08:00
|
|
|
/*
|
|
|
|
* File name calculations
|
|
|
|
*/
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("rotating_file_sink::calc_filename1", "[rotating_file_sink]") {
|
|
|
|
auto filename =
|
|
|
|
spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated.txt"), 3);
|
2021-01-06 16:55:46 +08:00
|
|
|
REQUIRE(filename == SPDLOG_FILENAME_T("rotated.3.txt"));
|
2020-06-07 19:38:16 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]") {
|
|
|
|
auto filename =
|
|
|
|
spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated"), 3);
|
2021-01-06 16:55:46 +08:00
|
|
|
REQUIRE(filename == SPDLOG_FILENAME_T("rotated.3"));
|
2020-06-07 19:38:16 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]") {
|
|
|
|
auto filename =
|
|
|
|
spdlog::sinks::rotating_file_sink_st::calc_filename(SPDLOG_FILENAME_T("rotated.txt"), 0);
|
2021-01-06 16:55:46 +08:00
|
|
|
REQUIRE(filename == SPDLOG_FILENAME_T("rotated.txt"));
|
2020-06-07 19:38:16 +08:00
|
|
|
}
|
|
|
|
|
2019-09-15 23:34:29 +08:00
|
|
|
// regex supported only from gcc 4.9 and above
|
|
|
|
#if defined(_MSC_VER) || !(__GNUC__ <= 4 && __GNUC_MINOR__ < 9)
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
#include <regex>
|
2019-09-15 23:34:29 +08:00
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]") {
|
2019-09-15 23:34:29 +08:00
|
|
|
// daily_YYYY-MM-DD_hh-mm.txt
|
2023-09-25 21:05:07 +08:00
|
|
|
auto filename = spdlog::sinks::daily_filename_calculator::calc_filename(
|
|
|
|
SPDLOG_FILENAME_T("daily.txt"), spdlog::details::os::localtime());
|
2019-09-15 23:34:29 +08:00
|
|
|
// date regex based on https://www.regular-expressions.info/dates.html
|
2021-07-19 05:50:51 +08:00
|
|
|
std::basic_regex<spdlog::filename_t::value_type> re(
|
|
|
|
SPDLOG_FILENAME_T(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"));
|
2021-01-06 16:55:46 +08:00
|
|
|
std::match_results<spdlog::filename_t::const_iterator> match;
|
2019-09-15 23:34:29 +08:00
|
|
|
REQUIRE(std::regex_match(filename, match, re));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink]") {
|
2021-11-16 04:46:22 +08:00
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
|
|
|
// example-YYYY-MM-DD.log
|
2023-09-25 21:05:07 +08:00
|
|
|
auto filename = spdlog::sinks::daily_filename_format_calculator::calc_filename(
|
|
|
|
SPDLOG_FILENAME_T("example-%Y-%m-%d.log"), tm);
|
2021-11-16 04:46:22 +08:00
|
|
|
|
2021-11-17 05:44:35 +08:00
|
|
|
REQUIRE(filename ==
|
2023-09-25 21:05:07 +08:00
|
|
|
spdlog::fmt_lib::format(SPDLOG_FILENAME_T("example-{:04d}-{:02d}-{:02d}.log"),
|
|
|
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday));
|
2021-11-16 04:46:22 +08:00
|
|
|
}
|
|
|
|
|
2019-09-15 23:34:29 +08:00
|
|
|
/* Test removal of old files */
|
2023-09-25 21:05:07 +08:00
|
|
|
static spdlog::details::log_msg create_msg(std::chrono::seconds offset) {
|
2019-09-15 23:34:29 +08:00
|
|
|
using spdlog::log_clock;
|
2023-09-23 22:59:51 +08:00
|
|
|
spdlog::details::log_msg msg{"test", spdlog::level::info, "Hello Message"};
|
2019-09-15 23:34:29 +08:00
|
|
|
msg.time = log_clock::now() + offset;
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_files) {
|
2019-09-15 23:34:29 +08:00
|
|
|
using spdlog::log_clock;
|
|
|
|
using spdlog::details::log_msg;
|
|
|
|
using spdlog::sinks::daily_file_sink_st;
|
2020-04-10 20:35:58 +08:00
|
|
|
|
2019-09-15 23:34:29 +08:00
|
|
|
prepare_logdir();
|
|
|
|
|
2021-01-06 16:55:46 +08:00
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_rotate.txt");
|
2019-09-15 23:34:29 +08:00
|
|
|
daily_file_sink_st sink{basename, 2, 30, true, max_days};
|
|
|
|
|
|
|
|
// simulate messages with 24 intervals
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
for (int i = 0; i < days_to_run; i++) {
|
2019-09-15 23:34:29 +08:00
|
|
|
auto offset = std::chrono::seconds{24 * 3600 * i};
|
|
|
|
sink.log(create_msg(offset));
|
|
|
|
}
|
|
|
|
|
2019-10-20 22:40:56 +08:00
|
|
|
REQUIRE(count_files("test_logs") == static_cast<size_t>(expected_n_files));
|
2019-09-15 23:34:29 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("daily_logger rotate", "[daily_file_sink]") {
|
2019-09-15 23:34:29 +08:00
|
|
|
int days_to_run = 1;
|
|
|
|
test_rotate(days_to_run, 0, 1);
|
|
|
|
test_rotate(days_to_run, 1, 1);
|
|
|
|
test_rotate(days_to_run, 3, 1);
|
|
|
|
test_rotate(days_to_run, 10, 1);
|
|
|
|
|
|
|
|
days_to_run = 10;
|
|
|
|
test_rotate(days_to_run, 0, 10);
|
|
|
|
test_rotate(days_to_run, 1, 1);
|
|
|
|
test_rotate(days_to_run, 3, 3);
|
|
|
|
test_rotate(days_to_run, 9, 9);
|
|
|
|
test_rotate(days_to_run, 10, 10);
|
|
|
|
test_rotate(days_to_run, 11, 10);
|
|
|
|
test_rotate(days_to_run, 20, 10);
|
2021-11-17 07:04:27 +08:00
|
|
|
}
|