spdlog/tests/test_file_logging.cpp

96 lines
2.8 KiB
C++
Raw Normal View History

2016-04-20 04:57:49 -04: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();
std::string filename = "logs/simple_log";
2016-04-20 04:57:49 -04:00
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
2016-04-20 04:57:49 -04:00
logger->set_pattern("%v");
logger->info("Test message {}", 1);
logger->info("Test message {}", 2);
2018-06-12 11:48:22 -04:00
2016-04-20 04:57:49 -04:00
logger->flush();
REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n"));
REQUIRE(count_lines(filename) == 2);
}
TEST_CASE("flush_on", "[flush_on]]")
{
2016-10-12 16:08:44 -04:00
prepare_logdir();
std::string filename = "logs/simple_log";
2016-10-12 16:08:44 -04:00
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
2016-10-12 16:08:44 -04:00
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);
2016-10-12 16:08:44 -04:00
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 04:57:49 -04:00
}
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
2016-04-20 04:57:49 -04:00
{
prepare_logdir();
2018-05-26 11:48:39 -04:00
size_t max_size = 1024 * 10;
2016-04-20 04:57:49 -04:00
std::string basename = "logs/rotating_log";
2018-05-23 17:18:55 -04:00
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0);
2016-10-12 16:08:44 -04:00
2016-04-20 04:57:49 -04:00
for (int i = 0; i < 10; ++i)
{
2016-04-20 04:57:49 -04:00
logger->info("Test message {}", i);
}
2016-04-20 04:57:49 -04:00
2016-10-12 16:08:44 -04:00
logger->flush();
auto filename = basename;
2016-04-20 04:57:49 -04:00
REQUIRE(count_lines(filename) == 10);
}
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
2016-04-20 04:57:49 -04:00
{
prepare_logdir();
2018-05-26 11:48:39 -04:00
size_t max_size = 1024 * 10;
2016-04-20 04:57:49 -04:00
std::string basename = "logs/rotating_log";
{
// make an initial logger to create the first output file
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
for (int i = 0; i < 10; ++i)
{
logger->info("Test message {}", i);
}
// drop causes the logger destructor to be called, which is required so the
2019-03-23 10:39:32 -04:00
// next logger can rename the first output file.
spdlog::drop(logger->name());
}
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
2016-04-20 04:57:49 -04:00
for (int i = 0; i < 10; ++i)
2018-11-22 14:35:11 -05:00
{
2016-04-20 04:57:49 -04:00
logger->info("Test message {}", i);
2018-11-22 14:35:11 -05:00
}
2016-04-20 04:57:49 -04:00
logger->flush();
auto filename = basename;
2016-04-20 04:57:49 -04:00
REQUIRE(count_lines(filename) == 10);
for (int i = 0; i < 1000; i++)
{
2018-06-12 11:48:22 -04:00
2016-04-20 04:57:49 -04:00
logger->info("Test message {}", i);
}
2016-04-20 04:57:49 -04:00
logger->flush();
2018-05-23 17:18:55 -04:00
REQUIRE(get_filesize(filename) <= max_size);
auto filename1 = basename + ".1";
2018-05-23 17:18:55 -04:00
REQUIRE(get_filesize(filename1) <= max_size);
2016-04-20 04:57:49 -04:00
}