2016-04-20 16:57:49 +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
|
2016-04-20 16:57:49 +08:00
|
|
|
*/
|
|
|
|
#include "includes.h"
|
|
|
|
|
2021-01-06 16:55:46 +08:00
|
|
|
#define SIMPLE_LOG "test_logs/simple_log"
|
|
|
|
#define ROTATING_LOG "test_logs/rotating_log"
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("simple_file_logger", "[simple_logger]") {
|
2016-04-20 16:57:49 +08:00
|
|
|
prepare_logdir();
|
2021-01-06 16:55:46 +08:00
|
|
|
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2018-07-07 17:12:45 +08:00
|
|
|
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
|
2016-04-20 16:57:49 +08:00
|
|
|
logger->set_pattern("%v");
|
|
|
|
|
|
|
|
logger->info("Test message {}", 1);
|
|
|
|
logger->info("Test message {}", 2);
|
2018-06-12 23:48:22 +08:00
|
|
|
|
2016-04-20 16:57:49 +08:00
|
|
|
logger->flush();
|
2021-01-06 16:55:46 +08:00
|
|
|
require_message_count(SIMPLE_LOG, 2);
|
2020-02-05 05:23:36 +08:00
|
|
|
using spdlog::details::os::default_eol;
|
2023-09-25 21:05:07 +08:00
|
|
|
REQUIRE(file_contents(SIMPLE_LOG) ==
|
|
|
|
spdlog::fmt_lib::format("Test message 1{}Test message 2{}", default_eol, default_eol));
|
2016-09-18 05:43:42 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("flush_on", "[flush_on]") {
|
2016-10-13 04:08:44 +08:00
|
|
|
prepare_logdir();
|
2021-01-06 16:55:46 +08:00
|
|
|
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
2016-10-13 04:08:44 +08:00
|
|
|
|
2018-07-07 17:12:45 +08:00
|
|
|
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
|
2016-10-13 04:08:44 +08:00
|
|
|
logger->set_pattern("%v");
|
2023-09-23 22:59:51 +08:00
|
|
|
logger->set_level(spdlog::level::trace);
|
|
|
|
logger->flush_on(spdlog::level::info);
|
2016-10-13 04:08:44 +08:00
|
|
|
logger->trace("Should not be flushed");
|
2021-01-06 16:55:46 +08:00
|
|
|
REQUIRE(count_lines(SIMPLE_LOG) == 0);
|
2016-10-13 04:08:44 +08:00
|
|
|
|
|
|
|
logger->info("Test message {}", 1);
|
|
|
|
logger->info("Test message {}", 2);
|
2018-08-13 15:30:02 +08:00
|
|
|
|
2021-01-06 16:55:46 +08:00
|
|
|
require_message_count(SIMPLE_LOG, 3);
|
2020-02-05 05:23:36 +08:00
|
|
|
using spdlog::details::os::default_eol;
|
2021-01-06 16:55:46 +08:00
|
|
|
REQUIRE(file_contents(SIMPLE_LOG) ==
|
2023-09-25 21:05:07 +08:00
|
|
|
spdlog::fmt_lib::format("Should not be flushed{}Test message 1{}Test message 2{}",
|
|
|
|
default_eol, default_eol, default_eol));
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("rotating_file_logger1", "[rotating_logger]") {
|
2016-04-20 16:57:49 +08:00
|
|
|
prepare_logdir();
|
2018-05-26 23:48:39 +08:00
|
|
|
size_t max_size = 1024 * 10;
|
2021-01-06 16:55:46 +08:00
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
2018-05-24 05:18:55 +08:00
|
|
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0);
|
2016-10-13 04:08:44 +08:00
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2016-04-20 16:57:49 +08:00
|
|
|
logger->info("Test message {}", i);
|
2017-10-05 19:20:21 +08:00
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2016-10-13 04:08:44 +08:00
|
|
|
logger->flush();
|
2021-01-06 16:55:46 +08:00
|
|
|
require_message_count(ROTATING_LOG, 10);
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("rotating_file_logger2", "[rotating_logger]") {
|
2016-04-20 16:57:49 +08:00
|
|
|
prepare_logdir();
|
2018-05-26 23:48:39 +08:00
|
|
|
size_t max_size = 1024 * 10;
|
2021-01-06 16:55:46 +08:00
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
2019-01-24 13:06:15 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
// make an initial logger to create the first output file
|
|
|
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
|
2023-09-25 21:05:07 +08:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2019-01-24 13:06:15 +08:00
|
|
|
logger->info("Test message {}", i);
|
|
|
|
}
|
|
|
|
// drop causes the logger destructor to be called, which is required so the
|
2019-03-23 22:39:32 +08:00
|
|
|
// next logger can rename the first output file.
|
2019-01-24 13:06:15 +08:00
|
|
|
spdlog::drop(logger->name());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
|
2023-09-25 21:05:07 +08:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2016-04-20 16:57:49 +08:00
|
|
|
logger->info("Test message {}", i);
|
2018-11-23 03:35:11 +08:00
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
|
|
|
logger->flush();
|
2020-04-08 23:17:21 +08:00
|
|
|
|
2021-01-06 16:55:46 +08:00
|
|
|
require_message_count(ROTATING_LOG, 10);
|
2020-02-05 05:23:36 +08:00
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
for (int i = 0; i < 1000; i++) {
|
2016-04-20 16:57:49 +08:00
|
|
|
logger->info("Test message {}", i);
|
2017-10-05 19:20:21 +08:00
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
|
|
|
logger->flush();
|
2021-01-06 16:55:46 +08:00
|
|
|
REQUIRE(get_filesize(ROTATING_LOG) <= max_size);
|
|
|
|
REQUIRE(get_filesize(ROTATING_LOG ".1") <= max_size);
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
2022-02-05 23:37:55 +08:00
|
|
|
|
|
|
|
// test that passing max_size=0 throws
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("rotating_file_logger3", "[rotating_logger]") {
|
2022-02-05 23:37:55 +08:00
|
|
|
prepare_logdir();
|
|
|
|
size_t max_size = 0;
|
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
2023-09-25 21:05:07 +08:00
|
|
|
REQUIRE_THROWS_AS(spdlog::rotating_logger_mt("logger", basename, max_size, 0),
|
|
|
|
spdlog::spdlog_ex);
|
2022-02-05 23:37:55 +08:00
|
|
|
}
|