Control log level via -v option
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone Build is running

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.
This commit is contained in:
Armin Friedl 2024-02-27 21:48:07 +01:00
parent 65053bbcb9
commit 5d9856c658
Signed by: armin
GPG key ID: 48C726EEE7FBCBC8
4 changed files with 33 additions and 6 deletions

View file

@ -28,6 +28,9 @@ UserOpt::UserOpt(int argc, char** argv) {
TCLAP::ValueArg<fs::path> arg_outfile
{"o", "out", "Out <file-or-path>", false, fs::path{}, "A path on the filesystem", cmd};
TCLAP::MultiSwitchArg arg_verbose
{"v", "verbose", "Verbosity level", cmd, 0};
TCLAP::UnlabeledMultiArg<fs::path> 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()) {

View file

@ -13,6 +13,7 @@ struct UserOpt {
optional<bool> compress;
optional<bool> extract;
bool interactive;
int verbosity;
std::optional<fs::path> out;
std::set<fs::path> paths;

View file

@ -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<UserIntent> user_intent = make_intent(user_opt);
user_intent->execute();
} catch(XwimError& e) {
spdlog::error(e.what());
unique_ptr<UserIntent> user_intent = make_intent(user_opt);
user_intent->execute();
} catch (XwimError& e) {
spdlog::error(e.what());
}
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <spdlog/common.h>
#include <spdlog/spdlog.h>
#include <cstdlib>
#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;