larra/tests/main.cpp

64 lines
2.2 KiB
C++
Raw Permalink Normal View History

2024-12-19 17:16:29 +00:00
#include <gtest/gtest.h>
#include <spdlog/spdlog.h>
#include <boost/program_options.hpp>
#include <exception>
2024-12-19 17:16:29 +00:00
#include <print>
#include <stdexcept>
// clang-format off
constexpr auto ToString(spdlog::level::level_enum e) {
switch (e) {
case spdlog::level::trace: return "TRACE";
case spdlog::level::debug: return "DEBUG";
case spdlog::level::info: return "INFO";
case spdlog::level::warn: return "WARNING";
case spdlog::level::err: return "ERROR";
case spdlog::level::critical: return "CRITICAL";
case spdlog::level::off: return "OFF";
default:
return "INVALID";
2024-12-19 17:16:29 +00:00
}
}
// clang-format on
namespace po = boost::program_options;
2024-12-19 17:16:29 +00:00
auto main(int argc, char** argv) -> int {
::testing::InitGoogleTest(&argc, argv);
// Define options
po::options_description desc("Allowed options");
desc.add_options()("help,h", "Print help message")("log_level,l",
po::value<int>()->default_value(SPDLOG_LEVEL_OFF),
"Set log level: 0=TRACE, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=CRITICAL, 6=OFF");
// Parse command-line arguments
po::variables_map vm;
try {
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if(vm["log_level"].as<int>() < spdlog::level::level_enum::trace || vm["log_level"].as<int>() > spdlog::level::level_enum::off) {
throw std::invalid_argument{
std::format("Invalid argument value for '--log_level' option. Check option description for more details")};
}
if(vm["log_level"].as<int>() < SPDLOG_ACTIVE_LEVEL) {
SPDLOG_WARN("Specified log_level '{}' is lower than max available one '{}'. Log level will be changed according to the maximum one",
vm["log_level"].as<int>(),
SPDLOG_ACTIVE_LEVEL);
}
} catch(const std::exception& e) {
SPDLOG_CRITICAL("Cmd parse error: {}", e.what());
return 1;
}
// Cmd options handling
auto level = static_cast<spdlog::level::level_enum>(vm["log_level"].as<int>());
spdlog::set_level(level);
if(level != spdlog::level::level_enum::off) {
std::println("\nEnvironment setup:\n\tCurrently set log level: {}\n", ToString(spdlog::get_level()));
}
2024-12-19 17:16:29 +00:00
return RUN_ALL_TESTS();
}