Skip to content

Commit

Permalink
simplifty log code in test_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Congyuwang committed Oct 22, 2023
1 parent 512d2b7 commit df6d4ea
Showing 1 changed file with 12 additions and 55 deletions.
67 changes: 12 additions & 55 deletions tests/test_utils.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "spdlog/common.h"
#undef NDEBUG
#ifndef SOCKET_MANAGER_TEST_UTILS_H
#define SOCKET_MANAGER_TEST_UTILS_H
Expand All @@ -15,66 +14,24 @@

class SpdLogger {
public:
static void init(SOCKET_MANAGER_C_API_TraceLevel level =
SOCKET_MANAGER_C_API_TraceLevel::Debug) {
switch (level) {
case SOCKET_MANAGER_C_API_TraceLevel::Trace: {
spdlog::set_level(spdlog::level::trace);
break;
}
case SOCKET_MANAGER_C_API_TraceLevel::Debug: {
spdlog::set_level(spdlog::level::debug);
break;
}
case SOCKET_MANAGER_C_API_TraceLevel::Info: {
spdlog::set_level(spdlog::level::info);
break;
}
case SOCKET_MANAGER_C_API_TraceLevel::Warn: {
spdlog::set_level(spdlog::level::warn);
break;
}
case SOCKET_MANAGER_C_API_TraceLevel::Error: {
spdlog::set_level(spdlog::level::err);
break;
}
case SOCKET_MANAGER_C_API_TraceLevel::Off: {
spdlog::set_level(spdlog::level::off);
break;
}
}
socket_manager::init_logger(print_log, level,
static void
init(spdlog::level::level_enum level = spdlog::level::level_enum::debug) {
spdlog::set_level(level);
SOCKET_MANAGER_C_API_TraceLevel log_level =
SOCKET_MANAGER_C_API_TraceLevel::Off;
// Socket Manager level is the same as spdlog level from trace to err
if (level <= spdlog::level::err) {
log_level = static_cast<SOCKET_MANAGER_C_API_TraceLevel>(level);
}
socket_manager::init_logger(print_log, log_level,
SOCKET_MANAGER_C_API_TraceLevel::Off);
};

private:
static void print_log(SOCKET_MANAGER_C_API_LogData log_data) {
socket_manager::LogData data = socket_manager::from_c_log_data(log_data);
switch (data.level) {
case SOCKET_MANAGER_C_API_TraceLevel::Trace: {
spdlog::trace("{}: {} {}", data.target, data.file, data.message);
break;
}
case SOCKET_MANAGER_C_API_TraceLevel::Debug: {
spdlog::debug("{}: {} {}", data.target, data.file, data.message);
break;
}
case SOCKET_MANAGER_C_API_TraceLevel::Info: {
spdlog::info("{}: {} {}", data.target, data.file, data.message);
break;
}
case SOCKET_MANAGER_C_API_TraceLevel::Warn: {
spdlog::warn("{}: {} {}", data.target, data.file, data.message);
break;
}
case SOCKET_MANAGER_C_API_TraceLevel::Error: {
spdlog::error("{}: {} {}", data.target, data.file, data.message);
break;
}
case SOCKET_MANAGER_C_API_TraceLevel::Off: {
break;
}
}
spdlog::log(static_cast<spdlog::level::level_enum>(data.level), "{}: {} {}",
data.target, data.file, data.message);
}
};

Expand Down

0 comments on commit df6d4ea

Please sign in to comment.