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();
|
2017-02-27 18:59:23 -05:00
|
|
|
std::string filename = "logs/simple_log";
|
2016-04-20 04:57:49 -04:00
|
|
|
|
|
|
|
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename);
|
|
|
|
logger->set_pattern("%v");
|
|
|
|
|
2017-10-05 07:20:21 -04:00
|
|
|
#if !defined(SPDLOG_FMT_PRINTF)
|
2016-04-20 04:57:49 -04:00
|
|
|
logger->info("Test message {}", 1);
|
|
|
|
logger->info("Test message {}", 2);
|
2017-10-05 07:20:21 -04:00
|
|
|
#else
|
|
|
|
logger->info("Test message %d", 1);
|
|
|
|
logger->info("Test message %d", 2);
|
|
|
|
#endif
|
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);
|
2016-09-17 17:43:42 -04:00
|
|
|
}
|
|
|
|
|
2016-04-20 04:57:49 -04:00
|
|
|
|
2016-09-17 17:43:42 -04:00
|
|
|
TEST_CASE("flush_on", "[flush_on]]")
|
|
|
|
{
|
2016-10-12 16:08:44 -04:00
|
|
|
prepare_logdir();
|
2017-02-27 18:59:23 -05:00
|
|
|
std::string filename = "logs/simple_log";
|
2016-10-12 16:08:44 -04: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);
|
|
|
|
|
2017-10-05 07:20:21 -04:00
|
|
|
#if !defined(SPDLOG_FMT_PRINTF)
|
2016-10-12 16:08:44 -04:00
|
|
|
logger->info("Test message {}", 1);
|
|
|
|
logger->info("Test message {}", 2);
|
2017-10-05 07:20:21 -04:00
|
|
|
#else
|
|
|
|
logger->info("Test message %d", 1);
|
|
|
|
logger->info("Test message %d", 2);
|
|
|
|
#endif
|
2016-10-12 16:08:44 -04:00
|
|
|
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 04:57:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
|
|
|
{
|
|
|
|
prepare_logdir();
|
|
|
|
std::string basename = "logs/rotating_log";
|
2016-09-02 09:59:55 -04:00
|
|
|
auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 0);
|
2016-10-12 16:08:44 -04:00
|
|
|
|
2016-04-20 04:57:49 -04:00
|
|
|
for (int i = 0; i < 10; ++i)
|
2017-10-05 07:20:21 -04:00
|
|
|
{
|
|
|
|
#if !defined(SPDLOG_FMT_PRINTF)
|
2016-04-20 04:57:49 -04:00
|
|
|
logger->info("Test message {}", i);
|
2017-10-05 07:20:21 -04:00
|
|
|
#else
|
|
|
|
logger->info("Test message %d", i);
|
|
|
|
#endif
|
|
|
|
}
|
2016-04-20 04:57:49 -04:00
|
|
|
|
2016-10-12 16:08:44 -04:00
|
|
|
logger->flush();
|
2017-02-27 18:59:23 -05:00
|
|
|
auto filename = basename;
|
2016-04-20 04:57:49 -04:00
|
|
|
REQUIRE(count_lines(filename) == 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
|
|
|
{
|
|
|
|
prepare_logdir();
|
|
|
|
std::string basename = "logs/rotating_log";
|
2016-09-02 09:59:55 -04:00
|
|
|
auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 1);
|
2016-04-20 04:57:49 -04:00
|
|
|
for (int i = 0; i < 10; ++i)
|
|
|
|
logger->info("Test message {}", i);
|
|
|
|
|
|
|
|
logger->flush();
|
2017-02-27 18:59:23 -05:00
|
|
|
auto filename = basename;
|
2016-04-20 04:57:49 -04:00
|
|
|
REQUIRE(count_lines(filename) == 10);
|
|
|
|
for (int i = 0; i < 1000; i++)
|
2017-10-05 07:20:21 -04:00
|
|
|
{
|
|
|
|
#if !defined(SPDLOG_FMT_PRINTF)
|
2016-04-20 04:57:49 -04:00
|
|
|
logger->info("Test message {}", i);
|
2017-10-05 07:20:21 -04:00
|
|
|
#else
|
|
|
|
logger->info("Test message %d", i);
|
|
|
|
#endif
|
|
|
|
}
|
2016-04-20 04:57:49 -04:00
|
|
|
|
|
|
|
logger->flush();
|
|
|
|
REQUIRE(get_filesize(filename) <= 1024);
|
2017-02-27 18:59:23 -05:00
|
|
|
auto filename1 = basename + ".1";
|
2016-04-20 04:57:49 -04:00
|
|
|
REQUIRE(get_filesize(filename1) <= 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE("daily_logger", "[daily_logger]]")
|
|
|
|
{
|
|
|
|
prepare_logdir();
|
|
|
|
//calculate filename (time based)
|
|
|
|
std::string basename = "logs/daily_log";
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
|
|
|
fmt::MemoryWriter w;
|
2017-02-27 18:59:23 -05:00
|
|
|
w.write("{}_{: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 04:57:49 -04:00
|
|
|
|
2016-09-02 09:59:55 -04:00
|
|
|
auto logger = spdlog::daily_logger_mt("logger", basename, 0, 0);
|
2016-09-02 10:06:00 -04:00
|
|
|
logger->flush_on(spdlog::level::info);
|
2016-04-20 04:57:49 -04:00
|
|
|
for (int i = 0; i < 10; ++i)
|
2017-10-05 07:20:21 -04:00
|
|
|
{
|
|
|
|
#if !defined(SPDLOG_FMT_PRINTF)
|
2016-04-20 04:57:49 -04:00
|
|
|
logger->info("Test message {}", i);
|
2017-10-05 07:20:21 -04:00
|
|
|
#else
|
|
|
|
logger->info("Test message %d", i);
|
|
|
|
#endif
|
|
|
|
}
|
2016-04-20 04:57:49 -04:00
|
|
|
|
|
|
|
auto filename = w.str();
|
|
|
|
REQUIRE(count_lines(filename) == 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-03 09:20:28 -04:00
|
|
|
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
|
|
|
|
{
|
|
|
|
using sink_type = spdlog::sinks::daily_file_sink<
|
2016-05-14 18:01:01 -04:00
|
|
|
std::mutex,
|
|
|
|
spdlog::sinks::dateonly_daily_file_name_calculator>;
|
2016-04-20 04:57:49 -04:00
|
|
|
|
2016-05-03 09:20:28 -04:00
|
|
|
prepare_logdir();
|
|
|
|
//calculate filename (time based)
|
|
|
|
std::string basename = "logs/daily_dateonly";
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
|
|
|
fmt::MemoryWriter w;
|
2017-02-27 18:59:23 -05:00
|
|
|
w.write("{}_{:04d}-{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
2016-04-20 04:57:49 -04:00
|
|
|
|
2017-02-27 18:59:23 -05:00
|
|
|
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
2016-05-03 09:20:28 -04:00
|
|
|
for (int i = 0; i < 10; ++i)
|
2017-10-05 07:20:21 -04:00
|
|
|
{
|
|
|
|
#if !defined(SPDLOG_FMT_PRINTF)
|
2016-05-03 09:20:28 -04:00
|
|
|
logger->info("Test message {}", i);
|
2017-10-05 07:20:21 -04:00
|
|
|
#else
|
|
|
|
logger->info("Test message %d", i);
|
|
|
|
#endif
|
|
|
|
}
|
2016-10-12 16:08:44 -04:00
|
|
|
logger->flush();
|
2016-05-03 09:20:28 -04:00
|
|
|
auto filename = w.str();
|
|
|
|
REQUIRE(count_lines(filename) == 10);
|
|
|
|
}
|
2016-04-20 04:57:49 -04:00
|
|
|
|
2016-05-03 09:20:28 -04:00
|
|
|
struct custom_daily_file_name_calculator
|
|
|
|
{
|
2017-02-27 18:59:23 -05:00
|
|
|
static spdlog::filename_t calc_filename(const spdlog::filename_t& basename)
|
2016-05-03 09:20:28 -04:00
|
|
|
{
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
|
|
|
fmt::MemoryWriter w;
|
2017-02-27 18:59:23 -05:00
|
|
|
w.write("{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
2016-05-03 09:20:28 -04:00
|
|
|
return w.str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
|
|
|
|
{
|
|
|
|
using sink_type = spdlog::sinks::daily_file_sink<
|
2016-05-14 18:01:01 -04:00
|
|
|
std::mutex,
|
|
|
|
custom_daily_file_name_calculator>;
|
2016-04-20 04:57:49 -04:00
|
|
|
|
2016-05-03 09:20:28 -04:00
|
|
|
prepare_logdir();
|
|
|
|
//calculate filename (time based)
|
|
|
|
std::string basename = "logs/daily_dateonly";
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
|
|
|
fmt::MemoryWriter w;
|
2017-02-27 18:59:23 -05:00
|
|
|
w.write("{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
|
2016-04-20 04:57:49 -04:00
|
|
|
|
2017-02-27 18:59:23 -05:00
|
|
|
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
2016-05-03 09:20:28 -04:00
|
|
|
for (int i = 0; i < 10; ++i)
|
2017-10-05 07:20:21 -04:00
|
|
|
{
|
|
|
|
#if !defined(SPDLOG_FMT_PRINTF)
|
2016-05-03 09:20:28 -04:00
|
|
|
logger->info("Test message {}", i);
|
2017-10-05 07:20:21 -04:00
|
|
|
#else
|
|
|
|
logger->info("Test message %d", i);
|
|
|
|
#endif
|
|
|
|
}
|
2016-04-20 04:57:49 -04:00
|
|
|
|
2016-10-12 16:08:44 -04:00
|
|
|
logger->flush();
|
|
|
|
auto filename = w.str();
|
2016-05-03 09:20:28 -04:00
|
|
|
REQUIRE(count_lines(filename) == 10);
|
|
|
|
}
|
2016-04-20 04:57:49 -04:00
|
|
|
|