mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
clang format
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
* This content is released under the MIT License as specified in
|
||||
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
*/
|
||||
#include "includes.h"
|
||||
|
||||
@@ -10,22 +11,20 @@ using filename_memory_buf_t = fmt::basic_memory_buffer<spdlog::filename_t::value
|
||||
#endif
|
||||
|
||||
#ifdef SPDLOG_WCHAR_FILENAMES
|
||||
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w)
|
||||
{
|
||||
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w) {
|
||||
spdlog::memory_buf_t buf;
|
||||
spdlog::details::os::wstr_to_utf8buf(spdlog::wstring_view_t(w.data(), w.size()), buf);
|
||||
return SPDLOG_BUF_TO_STRING(buf);
|
||||
}
|
||||
#else
|
||||
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w)
|
||||
{
|
||||
std::string filename_buf_to_utf8string(const filename_memory_buf_t &w) {
|
||||
return SPDLOG_BUF_TO_STRING(w);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
|
||||
{
|
||||
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>;
|
||||
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
|
||||
using sink_type =
|
||||
spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>;
|
||||
|
||||
prepare_logdir();
|
||||
|
||||
@@ -33,12 +32,11 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
filename_memory_buf_t w;
|
||||
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);
|
||||
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);
|
||||
|
||||
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
@@ -47,20 +45,18 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]")
|
||||
require_message_count(filename_buf_to_utf8string(w), 10);
|
||||
}
|
||||
|
||||
struct custom_daily_file_name_calculator
|
||||
{
|
||||
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm)
|
||||
{
|
||||
struct custom_daily_file_name_calculator {
|
||||
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm) {
|
||||
filename_memory_buf_t w;
|
||||
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);
|
||||
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);
|
||||
|
||||
return SPDLOG_BUF_TO_STRING(w);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
|
||||
{
|
||||
TEST_CASE("daily_logger with custom calculator", "[daily_logger]") {
|
||||
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, custom_daily_file_name_calculator>;
|
||||
|
||||
prepare_logdir();
|
||||
@@ -69,12 +65,11 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
filename_memory_buf_t w;
|
||||
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);
|
||||
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);
|
||||
|
||||
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
|
||||
@@ -87,34 +82,33 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]")
|
||||
* File name calculations
|
||||
*/
|
||||
|
||||
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);
|
||||
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);
|
||||
REQUIRE(filename == SPDLOG_FILENAME_T("rotated.3.txt"));
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
REQUIRE(filename == SPDLOG_FILENAME_T("rotated.3"));
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
REQUIRE(filename == SPDLOG_FILENAME_T("rotated.txt"));
|
||||
}
|
||||
|
||||
// regex supported only from gcc 4.9 and above
|
||||
#if defined(_MSC_VER) || !(__GNUC__ <= 4 && __GNUC_MINOR__ < 9)
|
||||
|
||||
# include <regex>
|
||||
#include <regex>
|
||||
|
||||
TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]")
|
||||
{
|
||||
TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]") {
|
||||
// daily_YYYY-MM-DD_hh-mm.txt
|
||||
auto filename =
|
||||
spdlog::sinks::daily_filename_calculator::calc_filename(SPDLOG_FILENAME_T("daily.txt"), spdlog::details::os::localtime());
|
||||
auto filename = spdlog::sinks::daily_filename_calculator::calc_filename(
|
||||
SPDLOG_FILENAME_T("daily.txt"), spdlog::details::os::localtime());
|
||||
// date regex based on https://www.regular-expressions.info/dates.html
|
||||
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$)"));
|
||||
@@ -123,27 +117,26 @@ TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]")
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink]")
|
||||
{
|
||||
TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink]") {
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
// example-YYYY-MM-DD.log
|
||||
auto filename = spdlog::sinks::daily_filename_format_calculator::calc_filename(SPDLOG_FILENAME_T("example-%Y-%m-%d.log"), tm);
|
||||
auto filename = spdlog::sinks::daily_filename_format_calculator::calc_filename(
|
||||
SPDLOG_FILENAME_T("example-%Y-%m-%d.log"), tm);
|
||||
|
||||
REQUIRE(filename ==
|
||||
spdlog::fmt_lib::format(SPDLOG_FILENAME_T("example-{:04d}-{:02d}-{:02d}.log"), tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday));
|
||||
spdlog::fmt_lib::format(SPDLOG_FILENAME_T("example-{:04d}-{:02d}-{:02d}.log"),
|
||||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday));
|
||||
}
|
||||
|
||||
/* Test removal of old files */
|
||||
static spdlog::details::log_msg create_msg(std::chrono::seconds offset)
|
||||
{
|
||||
static spdlog::details::log_msg create_msg(std::chrono::seconds offset) {
|
||||
using spdlog::log_clock;
|
||||
spdlog::details::log_msg msg{"test", spdlog::level::info, "Hello Message"};
|
||||
msg.time = log_clock::now() + offset;
|
||||
return msg;
|
||||
}
|
||||
|
||||
static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_files)
|
||||
{
|
||||
static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_files) {
|
||||
using spdlog::log_clock;
|
||||
using spdlog::details::log_msg;
|
||||
using spdlog::sinks::daily_file_sink_st;
|
||||
@@ -155,8 +148,7 @@ static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_
|
||||
|
||||
// simulate messages with 24 intervals
|
||||
|
||||
for (int i = 0; i < days_to_run; i++)
|
||||
{
|
||||
for (int i = 0; i < days_to_run; i++) {
|
||||
auto offset = std::chrono::seconds{24 * 3600 * i};
|
||||
sink.log(create_msg(offset));
|
||||
}
|
||||
@@ -164,8 +156,7 @@ static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_
|
||||
REQUIRE(count_files("test_logs") == static_cast<size_t>(expected_n_files));
|
||||
}
|
||||
|
||||
TEST_CASE("daily_logger rotate", "[daily_file_sink]")
|
||||
{
|
||||
TEST_CASE("daily_logger rotate", "[daily_file_sink]") {
|
||||
int days_to_run = 1;
|
||||
test_rotate(days_to_run, 0, 1);
|
||||
test_rotate(days_to_run, 1, 1);
|
||||
|
Reference in New Issue
Block a user