Added cmd arg handling to set log level
All checks were successful
PR Check / on-push-commit-check (push) Successful in 15m30s
All checks were successful
PR Check / on-push-commit-check (push) Successful in 15m30s
This commit is contained in:
parent
36fdeebff5
commit
6c7d43916c
6 changed files with 116 additions and 33 deletions
|
@ -134,8 +134,8 @@ jobs:
|
|||
-GNinja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DENABLE_EXAMPLES=ON \
|
||||
-DENABLE_TESTS=ON \
|
||||
-DLOG_LEVEL=0 \
|
||||
-DTEST_LOG_LEVEL=0 \
|
||||
-DMAX_LOG_LEVEL=0 \
|
||||
-DTEST_MAX_LOG_LEVEL=0 \
|
||||
-DCMAKE_CXX_FLAGS="-ftemplate-backtrace-limit=0"
|
||||
cmake --build ${{ github.workspace }}/build_gcc --parallel `nproc`
|
||||
|
||||
|
@ -144,7 +144,7 @@ jobs:
|
|||
continue-on-error: true
|
||||
run: |
|
||||
cd ${{ github.workspace }}/build_gcc
|
||||
./larra_xmpp_tests
|
||||
./larra_xmpp_tests --log_level=0
|
||||
|
||||
- name: Clang build (only configuring)
|
||||
id: clang_build
|
||||
|
@ -162,8 +162,8 @@ jobs:
|
|||
-GNinja -DCMAKE_BUILD_TYPE=Release \
|
||||
-DENABLE_EXAMPLES=ON \
|
||||
-DENABLE_TESTS=ON \
|
||||
-DLOG_LEVEL=0 \
|
||||
-DTEST_LOG_LEVEL=0 \
|
||||
-DMAX_LOG_LEVEL=0 \
|
||||
-DTEST_MAX_LOG_LEVEL=0 \
|
||||
-DCMAKE_CXX_FLAGS="-stdlib=libc++ -I/home/LLVM-${LLVM_VER}/include/c++/v1 -fno-modules"
|
||||
|
||||
echo "::group::compile_commands.json content"
|
||||
|
|
6
.vscode/launch.json
vendored
6
.vscode/launch.json
vendored
|
@ -9,7 +9,10 @@
|
|||
"request": "launch",
|
||||
"name": "Debug: connect",
|
||||
"program": "${workspaceFolder}/build/examples/output/connect",
|
||||
"args": [],
|
||||
"args": [
|
||||
"--log_level=0"
|
||||
// --gtest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS]
|
||||
],
|
||||
"cwd": "${workspaceFolder}",
|
||||
"preLaunchTask": "GCC: Build"
|
||||
},
|
||||
|
@ -19,6 +22,7 @@
|
|||
"name": "Debug: tests",
|
||||
"program": "${workspaceFolder}/build/larra_xmpp_tests",
|
||||
"args": [
|
||||
"--log_level=0"
|
||||
// --gtest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS]
|
||||
// "--gtest_filter=Roster*"
|
||||
],
|
||||
|
|
2
.vscode/tasks.json
vendored
2
.vscode/tasks.json
vendored
|
@ -79,7 +79,7 @@
|
|||
"command": [
|
||||
"cd ${workspaceFolder} &&",
|
||||
"mkdir -p build && cd build &&",
|
||||
"cmake cmake -Wno-dev ",
|
||||
"cmake -Wno-dev ",
|
||||
" -DCMAKE_BUILD_TYPE=Debug -DENABLE_EXAMPLES=ON -DENABLE_TESTS=ON -DLOG_LEVEL=0 -DTEST_LOG_LEVEL=0 .."
|
||||
],
|
||||
"options": {
|
||||
|
|
|
@ -18,12 +18,12 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
|||
set(FMT_MODULE OFF)
|
||||
set(UTEMPL_MODULE OFF)
|
||||
set(CXX_EXTENSIONS NO)
|
||||
set(BOOST_INCLUDE_LIBRARIES "pfr;asio;serialization")
|
||||
set(BOOST_INCLUDE_LIBRARIES "pfr;asio;serialization;program_options")
|
||||
option(CPM_USE_LOCAL_PACKAGES "Use local packages" ON)
|
||||
option(UTEMPL_USE_LOCAL_PACKAGE "Use utempl local package" OFF)
|
||||
option(BUILD_EXECUTABLE ON)
|
||||
set(LOG_LEVEL 2 CACHE STRING "Available log levels: 0=TRACE, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=CRITICAL, 6=OFF")
|
||||
set(TEST_LOG_LEVEL 2 CACHE STRING "Available log levels: 0=TRACE, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=CRITICAL, 6=OFF")
|
||||
set(MAX_LOG_LEVEL 2 CACHE STRING "Available log levels: 0=TRACE, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=CRITICAL, 6=OFF")
|
||||
set(TEST_MAX_LOG_LEVEL 2 CACHE STRING "Available log levels: 0=TRACE, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=CRITICAL, 6=OFF")
|
||||
set(UTEMPL_URL
|
||||
"https://sha512sum.xyz/git/sha512sum/utempl"
|
||||
CACHE STRING "utempl repository URL")
|
||||
|
@ -177,11 +177,11 @@ target_include_directories(larra_xmpp PUBLIC
|
|||
|
||||
if(TARGET Boost::pfr)
|
||||
target_link_libraries(larra_xmpp PUBLIC
|
||||
Boost::asio Boost::serialization utempl::utempl
|
||||
Boost::asio Boost::serialization Boost::program_options utempl::utempl
|
||||
OpenSSL::SSL nameof::nameof fmt::fmt
|
||||
OpenSSL::Crypto spdlog xmlplusplus ${LIBXML2_LIBRARIES})
|
||||
else()
|
||||
find_package(Boost 1.85.0 COMPONENTS serialization REQUIRED)
|
||||
find_package(Boost 1.85.0 COMPONENTS serialization program_options REQUIRED)
|
||||
target_link_libraries(larra_xmpp PUBLIC
|
||||
utempl::utempl ${Boost_LIBRARIES} OpenSSL::SSL
|
||||
nameof::nameof fmt::fmt
|
||||
|
@ -261,7 +261,7 @@ if(ENABLE_TESTS)
|
|||
target_sources(larra_xmpp_tests PUBLIC ${SOURCES})
|
||||
target_link_libraries(larra_xmpp_tests GTest::gtest_main
|
||||
larra_xmpp)
|
||||
target_compile_definitions(larra_xmpp_tests PRIVATE SPDLOG_ACTIVE_LEVEL=${TEST_LOG_LEVEL})
|
||||
target_compile_definitions(larra_xmpp_tests PRIVATE SPDLOG_ACTIVE_LEVEL=${TEST_MAX_LOG_LEVEL})
|
||||
set_property(TARGET larra_xmpp_tests PROPERTY CXX_STANDARD 23)
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(larra_xmpp_tests)
|
||||
|
@ -273,7 +273,7 @@ if(ENABLE_EXAMPLES)
|
|||
get_filename_component(EXAMPLE_NAME ${EXAMPLE_SRC} NAME_WE)
|
||||
add_executable(${EXAMPLE_NAME} ${EXAMPLE_SRC})
|
||||
target_link_libraries(${EXAMPLE_NAME} larra_xmpp)
|
||||
target_compile_definitions(${EXAMPLE_NAME} PRIVATE SPDLOG_ACTIVE_LEVEL=${TEST_LOG_LEVEL})
|
||||
target_compile_definitions(${EXAMPLE_NAME} PRIVATE SPDLOG_ACTIVE_LEVEL=${TEST_MAX_LOG_LEVEL})
|
||||
set_property(TARGET ${EXAMPLE_NAME} PROPERTY CXX_STANDARD 23)
|
||||
set_target_properties(${EXAMPLE_NAME} PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/examples/output")
|
||||
|
|
|
@ -3,11 +3,28 @@
|
|||
|
||||
#include <boost/asio/co_spawn.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/program_options.hpp>
|
||||
#include <larra/client/client.hpp>
|
||||
#include <larra/presence.hpp>
|
||||
#include <larra/printer_stream.hpp>
|
||||
#include <print>
|
||||
|
||||
// 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";
|
||||
}
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
namespace iq = larra::xmpp::iq;
|
||||
|
||||
auto Coroutine() -> boost::asio::awaitable<void> {
|
||||
|
@ -50,16 +67,38 @@ auto Coroutine() -> boost::asio::awaitable<void> {
|
|||
SPDLOG_INFO("Done connecting client!");
|
||||
}
|
||||
|
||||
auto main() -> int {
|
||||
spdlog::set_level(spdlog::level::trace);
|
||||
namespace po = boost::program_options;
|
||||
|
||||
#ifdef SPDLOG_ACTIVE_LEVEL
|
||||
std::println("\n\tCompiled max available log level: {}\n\tCurrently set log level: {}",
|
||||
SPDLOG_ACTIVE_LEVEL,
|
||||
std::to_underlying(spdlog::get_level()));
|
||||
#else
|
||||
std::println("\n\tCurrently set log level: {}", std::to_underlying(spdlog::get_level()));
|
||||
#endif
|
||||
auto main(int argc, char** argv) -> int {
|
||||
// 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_INFO),
|
||||
"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
|
||||
spdlog::set_level(static_cast<spdlog::level::level_enum>(vm["log_level"].as<int>()));
|
||||
std::println("\nEnvironment setup:\n\tCurrently set log level: {}\n", ToString(spdlog::get_level()));
|
||||
|
||||
boost::asio::io_context io_context;
|
||||
boost::asio::co_spawn(io_context, Coroutine(), boost::asio::detached);
|
||||
|
|
|
@ -1,21 +1,61 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
#include <exception>
|
||||
#include <print>
|
||||
#include <utility>
|
||||
#include <stdexcept>
|
||||
|
||||
class PreconfigureEnvironment : public ::testing::Environment {
|
||||
public:
|
||||
void SetUp() override {
|
||||
spdlog::set_level(spdlog::level::trace);
|
||||
std::println("\nPreconfigureEnvironment setup:\n\tCompiled max availabel log level: {}\n\tCurrently set log level: {}",
|
||||
SPDLOG_ACTIVE_LEVEL,
|
||||
std::to_underlying(spdlog::get_level()));
|
||||
// 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";
|
||||
}
|
||||
};
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
namespace po = boost::program_options;
|
||||
|
||||
auto main(int argc, char** argv) -> int {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
::testing::AddGlobalTestEnvironment(new PreconfigureEnvironment); // NOLINT GTest takes ownership
|
||||
|
||||
// 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_INFO),
|
||||
"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
|
||||
spdlog::set_level(static_cast<spdlog::level::level_enum>(vm["log_level"].as<int>()));
|
||||
std::println("\nEnvironment setup:\n\tCurrently set log level: {}\n", ToString(spdlog::get_level()));
|
||||
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
Loading…
Reference in a new issue