133 lines
4.2 KiB
C++
133 lines
4.2 KiB
C++
|
#include <sstream>
|
||
|
#include "c_testcase.h"
|
||
|
#include "logging/interface.h"
|
||
|
#include "logging/logger.hpp"
|
||
|
|
||
|
using namespace logging;
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
TEST_CASE(test_add_stream) {
|
||
|
std::stringstream ss;
|
||
|
log_init(NULL);
|
||
|
logging::global_logger->add_stream(ss);
|
||
|
|
||
|
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("");
|
||
|
|
||
|
logging::global_logger->log<logging::Logger::Level::DEBUG>("test_stream debug");
|
||
|
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);
|
||
|
auto& logger = *logging::global_logger;
|
||
|
|
||
|
std::stringstream ss;
|
||
|
logger.add_stream(ss);
|
||
|
|
||
|
logger[LOG_LEVEL_INFO] << "test_log_stream" << std::endl;
|
||
|
auto s = ss.str();
|
||
|
assert_str_eq(s.c_str() + (s.length() - 23), "[INFO] test_log_stream\n");
|
||
|
ss.str("");
|
||
|
|
||
|
logger[LOG_LEVEL_DEBUG] << "test_log_stream debug" << std::endl;
|
||
|
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");
|
||
|
assert_eq(sublogger->parent(), logging::global_logger.get());
|
||
|
|
||
|
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");
|
||
|
assert_eq(sub1logger->parent(), logging::global_logger.get());
|
||
|
assert_eq(sub1sub1logger->parent(), sub1logger);
|
||
|
|
||
|
auto root_logger = get_logger("");
|
||
|
assert_eq(root_logger, logging::global_logger.get());
|
||
|
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);
|
||
|
|
||
|
global_logger->set_level(LogLevel::DEBUG);
|
||
|
assert_eq(global_logger->level(), LogLevel::DEBUG);
|
||
|
assert_eq(sublogger->level(), LogLevel::DEBUG);
|
||
|
assert_eq(subsublogger->level(), LogLevel::DEBUG);
|
||
|
sublogger->set_level(LogLevel::WARN);
|
||
|
assert_eq(global_logger->level(), LogLevel::DEBUG);
|
||
|
assert_eq(sublogger->level(), LogLevel::WARN);
|
||
|
assert_eq(subsublogger->level(), LogLevel::WARN);
|
||
|
END_TEST;
|
||
|
}
|