mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-28 17:19:34 +08:00
static lib wip
This commit is contained in:
@@ -11,7 +11,6 @@
|
||||
#include <string>
|
||||
|
||||
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
|
||||
#define SPDLOG_ENABLE_MESSAGE_COUNTER
|
||||
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "spdlog/async.h"
|
||||
|
@@ -41,13 +41,18 @@ TEST_CASE("default_error_handler", "[errors]]")
|
||||
struct custom_ex
|
||||
{
|
||||
};
|
||||
|
||||
static void custom_handler(const std::string &msg)
|
||||
{
|
||||
throw custom_ex();
|
||||
}
|
||||
TEST_CASE("custom_error_handler", "[errors]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
std::string filename = "logs/simple_log.txt";
|
||||
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
|
||||
logger->flush_on(spdlog::level::info);
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
logger->set_error_handler(custom_handler);
|
||||
logger->info("Good message #1");
|
||||
|
||||
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
|
||||
@@ -59,7 +64,7 @@ TEST_CASE("default_error_handler2", "[errors]]")
|
||||
{
|
||||
spdlog::drop_all();
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
logger->set_error_handler(custom_handler);
|
||||
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
|
||||
}
|
||||
|
||||
@@ -67,26 +72,26 @@ TEST_CASE("flush_error_handler", "[errors]]")
|
||||
{
|
||||
spdlog::drop_all();
|
||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||
logger->set_error_handler(custom_handler);
|
||||
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("async_error_handler", "[errors]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
std::string err_msg("log failed with some msg");
|
||||
|
||||
|
||||
std::string filename = "logs/simple_async_log.txt";
|
||||
{
|
||||
spdlog::init_thread_pool(128, 1);
|
||||
auto logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("logger", filename, true);
|
||||
logger->set_error_handler([=](const std::string &) {
|
||||
logger->set_error_handler([](const std::string &) {
|
||||
std::ofstream ofs("logs/custom_err.txt");
|
||||
if (!ofs)
|
||||
{
|
||||
throw std::runtime_error("Failed open logs/custom_err.txt");
|
||||
}
|
||||
ofs << err_msg;
|
||||
ofs << "log failed with some msg";
|
||||
});
|
||||
logger->info("Good message #1");
|
||||
logger->info("Bad format msg {} {}", "xxx");
|
||||
@@ -95,27 +100,26 @@ TEST_CASE("async_error_handler", "[errors]]")
|
||||
}
|
||||
spdlog::init_thread_pool(128, 1);
|
||||
REQUIRE(count_lines(filename) == 2);
|
||||
REQUIRE(file_contents("logs/custom_err.txt") == err_msg);
|
||||
REQUIRE(file_contents("logs/custom_err.txt") == "log failed with some msg");
|
||||
}
|
||||
|
||||
// Make sure async error handler is executed
|
||||
TEST_CASE("async_error_handler2", "[errors]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
std::string err_msg("This is async handler error message");
|
||||
prepare_logdir();
|
||||
{
|
||||
spdlog::init_thread_pool(128, 1);
|
||||
auto logger = spdlog::create_async<failing_sink>("failed_logger");
|
||||
logger->set_error_handler([=](const std::string &) {
|
||||
logger->set_error_handler([](const std::string &) {
|
||||
std::ofstream ofs("logs/custom_err2.txt");
|
||||
if (!ofs)
|
||||
throw std::runtime_error("Failed open logs/custom_err2.txt");
|
||||
ofs << err_msg;
|
||||
ofs << "handler error message";
|
||||
});
|
||||
logger->info("Hello failure");
|
||||
spdlog::drop("failed_logger"); // force logger to drain the queue and shutdown
|
||||
}
|
||||
|
||||
spdlog::init_thread_pool(128, 1);
|
||||
REQUIRE(file_contents("logs/custom_err2.txt") == err_msg);
|
||||
REQUIRE(file_contents("logs/custom_err2.txt") == "handler error message");
|
||||
}
|
||||
|
@@ -107,8 +107,7 @@ TEST_CASE("clone-logger", "[clone]")
|
||||
cloned->info("Some message 2");
|
||||
|
||||
auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(cloned->sinks()[0]);
|
||||
REQUIRE(test_sink->msg_counter() == 2);
|
||||
|
||||
|
||||
spdlog::drop_all();
|
||||
}
|
||||
|
||||
@@ -129,8 +128,7 @@ TEST_CASE("clone async", "[clone]")
|
||||
|
||||
spdlog::details::os::sleep_for_millis(10);
|
||||
|
||||
auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(cloned->sinks()[0]);
|
||||
REQUIRE(test_sink->msg_counter() == 2);
|
||||
auto test_sink = std::static_pointer_cast<sinks::test_sink_mt>(cloned->sinks()[0]);
|
||||
|
||||
spdlog::drop_all();
|
||||
}
|
||||
@@ -176,22 +174,6 @@ TEST_CASE("to_hex_no_delimiter", "[to_hex]")
|
||||
REQUIRE(ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol)));
|
||||
}
|
||||
|
||||
TEST_CASE("message_counter", "[message_counter]")
|
||||
{
|
||||
std::ostringstream oss;
|
||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||
spdlog::logger oss_logger("oss", oss_sink);
|
||||
oss_logger.set_pattern("%i %v");
|
||||
|
||||
oss_logger.info("Hello");
|
||||
REQUIRE(oss.str() == "000001 Hello" + std::string(spdlog::details::os::default_eol));
|
||||
|
||||
oss.str("");
|
||||
oss_logger.info("Hello again");
|
||||
|
||||
REQUIRE(oss.str() == "000002 Hello again" + std::string(spdlog::details::os::default_eol));
|
||||
}
|
||||
|
||||
TEST_CASE("default logger API", "[default logger]")
|
||||
{
|
||||
std::ostringstream oss;
|
||||
|
Reference in New Issue
Block a user