2024-12-06 08:07:06 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include "c_testcase.h"
|
|
|
|
#include "logging/interface.h"
|
|
|
|
#include "logging/logger.hpp"
|
|
|
|
|
|
|
|
using namespace logging;
|
|
|
|
|
2024-12-10 07:53:48 +00:00
|
|
|
static auto& module_logger = *get_logger("test_logging");
|
|
|
|
|
2024-12-06 08:07:06 +00:00
|
|
|
TEST_CASE(test_init) {
|
|
|
|
log_info("test_init before");
|
|
|
|
log_init(NULL);
|
|
|
|
assert_eq(log_level(), LOG_LEVEL_INFO);
|
|
|
|
log_info("test_init after");
|
|
|
|
|
|
|
|
log_debug("test_init debug");
|
|
|
|
log_warn("test_init warn");
|
|
|
|
log_error("test_init error");
|
|
|
|
log_fatal("test_init fatal");
|
|
|
|
|
|
|
|
log_init("debug");
|
|
|
|
assert_eq(log_level(), LOG_LEVEL_DEBUG);
|
|
|
|
log_warn_with_source("test_init warn with source");
|
|
|
|
log_error_from_errno("TestError");
|
|
|
|
END_TEST;
|
|
|
|
}
|
|
|
|
|
2024-12-10 07:53:48 +00:00
|
|
|
TEST_CASE(test_module_logger)
|
|
|
|
{
|
|
|
|
module_logger.info("test_module_logger1");
|
|
|
|
log_init("debug");
|
|
|
|
module_logger.info("test_module_logger2");
|
|
|
|
END_TEST;
|
|
|
|
}
|
|
|
|
|
2024-12-06 08:07:06 +00:00
|
|
|
TEST_CASE(test_add_stream) {
|
|
|
|
log_init(NULL);
|
2024-12-18 15:08:16 +00:00
|
|
|
auto& global_logger = get_global_logger();
|
|
|
|
global_logger.add_stream(std::stringstream());
|
|
|
|
auto& ss = dynamic_cast<std::stringstream&>(*global_logger.streams()[0]);
|
2024-12-06 08:07:06 +00:00
|
|
|
|
|
|
|
log_info("test_stream");
|
|
|
|
auto s = ss.str();
|
|
|
|
assert_str_eq(s.c_str() + (s.length() - 19), "[INFO] test_stream\n");
|
|
|
|
ss.str("");
|
|
|
|
|
|
|
|
log_debug("test_stream debug");
|
|
|
|
s = ss.str();
|
|
|
|
assert_eq(s.length(), 0);
|
|
|
|
ss.str("");
|
|
|
|
|
|
|
|
log_set_level(LOG_LEVEL_DEBUG);
|
|
|
|
|
|
|
|
log_debug("test_stream debug");
|
|
|
|
s = ss.str();
|
|
|
|
assert_str_eq(s.c_str() + (s.length() - 26), "[DEBUG] test_stream debug\n");
|
|
|
|
ss.str("");
|
|
|
|
|
|
|
|
log_warn("test_stream warn");
|
|
|
|
s = ss.str();
|
|
|
|
assert_str_eq(s.c_str() + (s.length() - 24), "[WARN] test_stream warn\n");
|
|
|
|
ss.str("");
|
|
|
|
|
|
|
|
log_error("test_stream error");
|
|
|
|
s = ss.str();
|
|
|
|
assert_str_eq(s.c_str() + (s.length() - 26), "[ERROR] test_stream error\n");
|
|
|
|
ss.str("");
|
|
|
|
|
|
|
|
log_fatal("test_stream fatal");
|
|
|
|
s = ss.str();
|
|
|
|
assert_str_eq(s.c_str() + (s.length() - 26), "[FATAL] test_stream fatal\n");
|
|
|
|
ss.str("");
|
|
|
|
|
2024-12-10 07:53:48 +00:00
|
|
|
get_global_logger().log<logging::Logger::Level::DEBUG>("test_stream debug");
|
2024-12-06 08:07:06 +00:00
|
|
|
s = ss.str();
|
|
|
|
assert_str_eq(s.c_str() + (s.length() - 26), "[DEBUG] test_stream debug\n");
|
|
|
|
|
|
|
|
END_TEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(test_log_stream) {
|
|
|
|
log_init(NULL);
|
2024-12-18 15:08:16 +00:00
|
|
|
auto& global_logger = get_global_logger();
|
|
|
|
global_logger.add_stream(std::stringstream());
|
|
|
|
auto& ss = dynamic_cast<std::stringstream&>(*global_logger.streams()[0]);
|
2024-12-06 08:07:06 +00:00
|
|
|
|
2024-12-18 15:08:16 +00:00
|
|
|
global_logger[LOG_LEVEL_INFO] << "test_log_stream" << std::endl;
|
2024-12-06 08:07:06 +00:00
|
|
|
auto s = ss.str();
|
|
|
|
assert_str_eq(s.c_str() + (s.length() - 23), "[INFO] test_log_stream\n");
|
|
|
|
ss.str("");
|
|
|
|
|
2024-12-18 15:08:16 +00:00
|
|
|
global_logger[LOG_LEVEL_DEBUG] << "test_log_stream debug" << std::endl;
|
2024-12-06 08:07:06 +00:00
|
|
|
s = ss.str();
|
|
|
|
assert_eq(s.length(), 0);
|
|
|
|
|
|
|
|
END_TEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(test_sub_logger) {
|
|
|
|
log_init(NULL);
|
|
|
|
auto sublogger = get_logger("sublogger");
|
|
|
|
assert_eq(sublogger->level(), LogLevel::INFO);
|
|
|
|
assert_eq(sublogger->name(), "sublogger");
|
2024-12-10 07:53:48 +00:00
|
|
|
assert_eq(sublogger->parent(), &get_global_logger());
|
2024-12-06 08:07:06 +00:00
|
|
|
|
|
|
|
auto subsublogger = get_logger("sublogger::subsublogger");
|
|
|
|
assert_eq(subsublogger->level(), LogLevel::INFO);
|
|
|
|
assert_eq(subsublogger->name(), "sublogger::subsublogger");
|
|
|
|
assert_eq(subsublogger->parent(), sublogger);
|
|
|
|
|
|
|
|
auto sub1sub1logger = get_logger("sublogger1::subsublogger1");
|
|
|
|
assert_eq(sub1sub1logger->level(), LogLevel::INFO);
|
|
|
|
assert_eq(sub1sub1logger->name(), "sublogger1::subsublogger1");
|
|
|
|
auto sub1logger = get_logger("sublogger1");
|
|
|
|
assert_eq(sub1logger->level(), LogLevel::INFO);
|
|
|
|
assert_eq(sub1logger->name(), "sublogger1");
|
2024-12-10 07:53:48 +00:00
|
|
|
assert_eq(sub1logger->parent(), &get_global_logger());
|
2024-12-06 08:07:06 +00:00
|
|
|
assert_eq(sub1sub1logger->parent(), sub1logger);
|
|
|
|
|
|
|
|
auto root_logger = get_logger("");
|
2024-12-10 07:53:48 +00:00
|
|
|
assert_eq(root_logger, &get_global_logger());
|
2024-12-06 08:07:06 +00:00
|
|
|
auto root_logger1 = get_logger("::");
|
|
|
|
assert_eq(root_logger1, root_logger);
|
|
|
|
auto subsublogger1 = get_logger("sublogger::subsublogger");
|
|
|
|
assert_eq(subsublogger1, subsublogger);
|
|
|
|
auto subsublogger2 = get_logger("::sublogger::subsublogger");
|
|
|
|
assert_eq(subsublogger2, subsublogger);
|
|
|
|
auto subsublogger3 = get_logger("sublogger::subsublogger::");
|
|
|
|
assert_eq(subsublogger3, subsublogger);
|
|
|
|
auto subsublogger4 = get_logger("sublogger::::::subsublogger");
|
|
|
|
assert_eq(subsublogger4, subsublogger);
|
|
|
|
|
2024-12-10 07:53:48 +00:00
|
|
|
get_global_logger().set_level(LogLevel::DEBUG);
|
|
|
|
assert_eq(get_global_logger().level(), LogLevel::DEBUG);
|
2024-12-06 08:07:06 +00:00
|
|
|
assert_eq(sublogger->level(), LogLevel::DEBUG);
|
|
|
|
assert_eq(subsublogger->level(), LogLevel::DEBUG);
|
|
|
|
sublogger->set_level(LogLevel::WARN);
|
2024-12-10 07:53:48 +00:00
|
|
|
assert_eq(get_global_logger().level(), LogLevel::DEBUG);
|
2024-12-06 08:07:06 +00:00
|
|
|
assert_eq(sublogger->level(), LogLevel::WARN);
|
|
|
|
assert_eq(subsublogger->level(), LogLevel::WARN);
|
|
|
|
END_TEST;
|
|
|
|
}
|