2016-04-20 16:57:49 +08:00
|
|
|
/*
|
|
|
|
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
|
|
|
*/
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
TEST_CASE("simple_file_logger", "[simple_logger]]")
|
|
|
|
{
|
|
|
|
prepare_logdir();
|
2017-02-28 07:59:23 +08:00
|
|
|
std::string filename = "logs/simple_log";
|
2016-04-20 16:57:49 +08:00
|
|
|
|
|
|
|
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename);
|
|
|
|
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();
|
|
|
|
REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n"));
|
|
|
|
REQUIRE(count_lines(filename) == 2);
|
2016-09-18 05:43:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("flush_on", "[flush_on]]")
|
|
|
|
{
|
2016-10-13 04:08:44 +08:00
|
|
|
prepare_logdir();
|
2017-02-28 07:59:23 +08:00
|
|
|
std::string filename = "logs/simple_log";
|
2016-10-13 04:08:44 +08:00
|
|
|
|
|
|
|
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename);
|
|
|
|
logger->set_pattern("%v");
|
|
|
|
logger->set_level(spdlog::level::trace);
|
|
|
|
logger->flush_on(spdlog::level::info);
|
|
|
|
logger->trace("Should not be flushed");
|
|
|
|
REQUIRE(count_lines(filename) == 0);
|
|
|
|
|
|
|
|
logger->info("Test message {}", 1);
|
|
|
|
logger->info("Test message {}", 2);
|
|
|
|
logger->flush();
|
|
|
|
REQUIRE(file_contents(filename) == std::string("Should not be flushed\nTest message 1\nTest message 2\n"));
|
|
|
|
REQUIRE(count_lines(filename) == 3);
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
|
|
|
{
|
|
|
|
prepare_logdir();
|
2018-05-26 23:48:39 +08:00
|
|
|
size_t max_size = 1024 * 10;
|
2016-04-20 16:57:49 +08:00
|
|
|
std::string basename = "logs/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
|
|
|
|
2016-04-20 16:57:49 +08:00
|
|
|
for (int i = 0; i < 10; ++i)
|
2017-10-05 19:20:21 +08:00
|
|
|
{
|
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();
|
2017-02-28 07:59:23 +08:00
|
|
|
auto filename = basename;
|
2016-04-20 16:57:49 +08:00
|
|
|
REQUIRE(count_lines(filename) == 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
|
|
|
{
|
|
|
|
prepare_logdir();
|
2018-05-26 23:48:39 +08:00
|
|
|
size_t max_size = 1024 * 10;
|
2016-04-20 16:57:49 +08:00
|
|
|
std::string basename = "logs/rotating_log";
|
2018-05-24 05:18:55 +08:00
|
|
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 1);
|
2016-04-20 16:57:49 +08:00
|
|
|
for (int i = 0; i < 10; ++i)
|
|
|
|
logger->info("Test message {}", i);
|
|
|
|
|
|
|
|
logger->flush();
|
2017-02-28 07:59:23 +08:00
|
|
|
auto filename = basename;
|
2016-04-20 16:57:49 +08:00
|
|
|
REQUIRE(count_lines(filename) == 10);
|
|
|
|
for (int i = 0; i < 1000; i++)
|
2017-10-05 19:20:21 +08:00
|
|
|
{
|
2018-06-12 23:48:22 +08:00
|
|
|
|
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();
|
2018-05-24 05:18:55 +08:00
|
|
|
REQUIRE(get_filesize(filename) <= max_size);
|
2017-02-28 07:59:23 +08:00
|
|
|
auto filename1 = basename + ".1";
|
2018-05-24 05:18:55 +08:00
|
|
|
REQUIRE(get_filesize(filename1) <= max_size);
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("daily_logger", "[daily_logger]]")
|
|
|
|
{
|
|
|
|
prepare_logdir();
|
2018-03-09 21:26:33 +08:00
|
|
|
// calculate filename (time based)
|
2016-04-20 16:57:49 +08:00
|
|
|
std::string basename = "logs/daily_log";
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
2018-06-13 03:43:49 +08:00
|
|
|
fmt::memory_buffer w;
|
|
|
|
fmt::format_to(
|
|
|
|
w, "{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min);
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2016-09-02 21:59:55 +08:00
|
|
|
auto logger = spdlog::daily_logger_mt("logger", basename, 0, 0);
|
2016-09-02 22:06:00 +08:00
|
|
|
logger->flush_on(spdlog::level::info);
|
2016-04-20 16:57:49 +08:00
|
|
|
for (int i = 0; i < 10; ++i)
|
2017-10-05 19:20:21 +08:00
|
|
|
{
|
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
|
|
|
|
2018-06-13 03:43:49 +08:00
|
|
|
auto filename = fmt::to_string(w);
|
2016-04-20 16:57:49 +08:00
|
|
|
REQUIRE(count_lines(filename) == 10);
|
|
|
|
}
|
|
|
|
|
2016-05-03 21:20:28 +08:00
|
|
|
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
|
|
|
|
{
|
2018-03-09 21:26:33 +08:00
|
|
|
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::dateonly_daily_file_name_calculator>;
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2016-05-03 21:20:28 +08:00
|
|
|
prepare_logdir();
|
2018-03-09 21:26:33 +08:00
|
|
|
// calculate filename (time based)
|
2016-05-03 21:20:28 +08:00
|
|
|
std::string basename = "logs/daily_dateonly";
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
2018-06-13 03:43:49 +08:00
|
|
|
fmt::memory_buffer w;
|
|
|
|
fmt::format_to(w, "{}_{:04d}-{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2017-02-28 07:59:23 +08:00
|
|
|
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
2016-05-03 21:20:28 +08:00
|
|
|
for (int i = 0; i < 10; ++i)
|
2017-10-05 19:20:21 +08:00
|
|
|
{
|
2018-06-12 23:48:22 +08:00
|
|
|
|
2016-05-03 21:20:28 +08:00
|
|
|
logger->info("Test message {}", i);
|
2017-10-05 19:20:21 +08:00
|
|
|
}
|
2016-10-13 04:08:44 +08:00
|
|
|
logger->flush();
|
2018-06-13 03:43:49 +08:00
|
|
|
auto filename = fmt::to_string(w);
|
2016-05-03 21:20:28 +08:00
|
|
|
REQUIRE(count_lines(filename) == 10);
|
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2016-05-03 21:20:28 +08:00
|
|
|
struct custom_daily_file_name_calculator
|
|
|
|
{
|
2018-03-09 21:26:33 +08:00
|
|
|
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename)
|
2016-05-03 21:20:28 +08:00
|
|
|
{
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
2018-06-13 03:43:49 +08:00
|
|
|
fmt::memory_buffer w;
|
|
|
|
fmt::format_to(w, "{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
|
|
|
return fmt::to_string(w);
|
2016-05-03 21:20:28 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
|
|
|
|
{
|
2018-03-09 21:26:33 +08:00
|
|
|
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, custom_daily_file_name_calculator>;
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2016-05-03 21:20:28 +08:00
|
|
|
prepare_logdir();
|
2018-03-09 21:26:33 +08:00
|
|
|
// calculate filename (time based)
|
2016-05-03 21:20:28 +08:00
|
|
|
std::string basename = "logs/daily_dateonly";
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
2018-06-13 03:43:49 +08:00
|
|
|
fmt::memory_buffer w;
|
|
|
|
fmt::format_to(w, "{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2017-02-28 07:59:23 +08:00
|
|
|
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
2016-05-03 21:20:28 +08:00
|
|
|
for (int i = 0; i < 10; ++i)
|
2017-10-05 19:20:21 +08:00
|
|
|
{
|
2016-05-03 21:20:28 +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();
|
2018-06-13 03:43:49 +08:00
|
|
|
auto filename = fmt::to_string(w);
|
2016-05-03 21:20:28 +08:00
|
|
|
REQUIRE(count_lines(filename) == 10);
|
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2017-12-01 09:40:49 +08:00
|
|
|
/*
|
|
|
|
* File name calculations
|
|
|
|
*/
|
|
|
|
|
|
|
|
TEST_CASE("rotating_file_sink::calc_filename1", "[rotating_file_sink]]")
|
|
|
|
{
|
2017-12-01 09:46:19 +08:00
|
|
|
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 3);
|
|
|
|
REQUIRE(filename == "rotated.3.txt");
|
2017-12-01 09:40:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]]")
|
|
|
|
{
|
2017-12-01 09:46:19 +08:00
|
|
|
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated", 3);
|
|
|
|
REQUIRE(filename == "rotated.3");
|
2017-12-01 09:40:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]")
|
|
|
|
{
|
2017-12-01 09:46:19 +08:00
|
|
|
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 0);
|
|
|
|
REQUIRE(filename == "rotated.txt");
|
2017-12-01 09:40:49 +08:00
|
|
|
}
|
|
|
|
|
2017-12-01 10:35:23 +08:00
|
|
|
// regex supported only from gcc 4.9 and above
|
2018-03-09 21:26:33 +08:00
|
|
|
#if defined(_MSC_VER) || !(__GNUC__ <= 4 && __GNUC_MINOR__ < 9)
|
2017-12-01 10:42:23 +08:00
|
|
|
#include <regex>
|
2017-12-01 09:40:49 +08:00
|
|
|
TEST_CASE("daily_file_sink::default_daily_file_name_calculator1", "[daily_file_sink]]")
|
|
|
|
{
|
2017-12-01 09:59:35 +08:00
|
|
|
// daily_YYYY-MM-DD_hh-mm.txt
|
2017-12-01 09:46:19 +08:00
|
|
|
auto filename = spdlog::sinks::default_daily_file_name_calculator::calc_filename("daily.txt");
|
|
|
|
std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9].txt$)");
|
|
|
|
std::smatch match;
|
|
|
|
REQUIRE(std::regex_match(filename, match, re));
|
2017-12-01 09:40:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("daily_file_sink::default_daily_file_name_calculator2", "[daily_file_sink]]")
|
|
|
|
{
|
2017-12-01 09:59:35 +08:00
|
|
|
// daily_YYYY-MM-DD_hh-mm.txt
|
2017-12-01 09:46:19 +08:00
|
|
|
auto filename = spdlog::sinks::default_daily_file_name_calculator::calc_filename("daily");
|
|
|
|
std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9]$)");
|
|
|
|
std::smatch match;
|
|
|
|
REQUIRE(std::regex_match(filename, match, re));
|
2017-12-01 09:40:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("daily_file_sink::dateonly_daily_file_name_calculator", "[daily_file_sink]]")
|
|
|
|
{
|
2017-12-01 09:59:35 +08:00
|
|
|
// daily_YYYY-MM-DD_hh-mm.txt
|
2017-12-01 09:46:19 +08:00
|
|
|
auto filename = spdlog::sinks::dateonly_daily_file_name_calculator::calc_filename("daily.txt");
|
|
|
|
// date regex based on https://www.regular-expressions.info/dates.html
|
|
|
|
std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)");
|
|
|
|
std::smatch match;
|
|
|
|
REQUIRE(std::regex_match(filename, match, re));
|
2017-12-01 09:40:49 +08:00
|
|
|
}
|
2017-12-01 10:35:23 +08:00
|
|
|
#endif
|