From 5d9856c658d0c072e6f8ab18187f9f55cc864f44 Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Tue, 27 Feb 2024 21:48:07 +0100 Subject: [PATCH] Control log level via `-v` option Allow the user to control the log level via the conventional `-v` command line option. Multiplying this option-as per convention-increases the verbosity. This is an additional way to set the log level, in addition to the existing: - environment variable - build type (debug vs release) Given it is the most intentional and direct way to set the log level it has precedence over all other options. --- src/UserOpt.cpp | 4 ++++ src/UserOpt.hpp | 1 + src/main.cpp | 11 ++++++----- src/util/Log.hpp | 23 ++++++++++++++++++++++- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/UserOpt.cpp b/src/UserOpt.cpp index e77982d..7466d5e 100644 --- a/src/UserOpt.cpp +++ b/src/UserOpt.cpp @@ -28,6 +28,9 @@ UserOpt::UserOpt(int argc, char** argv) { TCLAP::ValueArg arg_outfile {"o", "out", "Out ", false, fs::path{}, "A path on the filesystem", cmd}; + TCLAP::MultiSwitchArg arg_verbose + {"v", "verbose", "Verbosity level", cmd, 0}; + TCLAP::UnlabeledMultiArg arg_paths {"files", "Archive(s) to extract or file(s) to compress", true, "A path on the filesystem", cmd}; // clang-format on @@ -38,6 +41,7 @@ UserOpt::UserOpt(int argc, char** argv) { if (arg_extract.isSet()) this->extract = arg_extract.getValue(); if (arg_outfile.isSet()) this->out = arg_outfile.getValue(); + this->verbosity = arg_verbose.getValue(); this->interactive = arg_extract.getValue(); if (arg_paths.isSet()) { diff --git a/src/UserOpt.hpp b/src/UserOpt.hpp index 8be169f..216cb95 100644 --- a/src/UserOpt.hpp +++ b/src/UserOpt.hpp @@ -13,6 +13,7 @@ struct UserOpt { optional compress; optional extract; bool interactive; + int verbosity; std::optional out; std::set paths; diff --git a/src/main.cpp b/src/main.cpp index 42b8187..b7027e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,12 +14,13 @@ using namespace std; int main(int argc, char** argv) { log::init(); - UserOpt user_opt = UserOpt{argc, argv}; + log::init(user_opt.verbosity); + try { - unique_ptr user_intent = make_intent(user_opt); - user_intent->execute(); - } catch(XwimError& e) { - spdlog::error(e.what()); + unique_ptr user_intent = make_intent(user_opt); + user_intent->execute(); + } catch (XwimError& e) { + spdlog::error(e.what()); } } \ No newline at end of file diff --git a/src/util/Log.hpp b/src/util/Log.hpp index fd9db5a..7de6a7e 100644 --- a/src/util/Log.hpp +++ b/src/util/Log.hpp @@ -1,6 +1,7 @@ #pragma once #include #include + #include #ifdef NDEBUG #define XWIM_LOGLEVEL SPDLOG_LEVEL_ERROR @@ -57,7 +58,27 @@ spdlog::level::level_enum _init_from_compile() { * The determined level is then set for the default logger via * `spdlog::set_level`. */ -void init(spdlog::level::level_enum level = spdlog::level::level_enum::off) { +void init(int verbosity = -1, + spdlog::level::level_enum level = spdlog::level::level_enum::off) { + if (verbosity != -1) { + switch (verbosity) { + case 0: + spdlog::set_level(spdlog::level::off); + break; + case 1: + spdlog::set_level(spdlog::level::info); + break; + case 2: + spdlog::set_level(spdlog::level::debug); + break; + case 3: + default: + spdlog::set_level(spdlog::level::trace); + break; + } + return; + } + if (spdlog::level::level_enum::off != level) { spdlog::set_level(level); return;