From affb3b53c8786ea7416ff634c995c300fd5f6d7d Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Thu, 25 Jul 2024 09:57:40 +0200 Subject: [PATCH 01/18] Add debug/release build-and-test GitHub actions workflow --- .github/workflows/build_and_test.yml | 40 ++++++++++++++++++++++++++++ src/cli/start.cpp | 4 +-- src/uenv/lex.cpp | 3 +++ test/unit/envvars.cpp | 3 ++- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/build_and_test.yml diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml new file mode 100644 index 0000000..1205f10 --- /dev/null +++ b/.github/workflows/build_and_test.yml @@ -0,0 +1,40 @@ +name: Build and test + +on: + merge_group: + pull_request: + push: + branches: [main] + +jobs: + build-and-test: + strategy: + matrix: + buildtype: [debug, release] + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + - name: Update apt repositories for ccache + run: sudo apt update + - name: Set up ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ccache-linux-${{ matrix.buildtype }} + - name: Setup compiler and build tools + run: sudo apt install --no-install-recommends --yes g++-12 meson ninja-build + - name: Configure + run: | + CXX="ccache g++-12" meson setup \ + --buildtype ${{ matrix.buildtype }} \ + --warnlevel 3 \ + --werror \ + -Dcatch2:werror=false \ + -Dcli11:werror=false \ + -Dfmt:werror=false \ + -Dnlohmann_json:werror=false \ + -Dsqlite3:werror=false \ + build . + - name: Build + run: ninja -C build + - name: Test + run: ./build/unit diff --git a/src/cli/start.cpp b/src/cli/start.cpp index 6e5632b..42c2e71 100644 --- a/src/cli/start.cpp +++ b/src/cli/start.cpp @@ -22,7 +22,7 @@ void start_help() { fmt::println("start help"); } -void start_args::add_cli(CLI::App& cli, global_settings& settings) { +void start_args::add_cli(CLI::App& cli, [[maybe_unused]] global_settings& settings) { auto* start_cli = cli.add_subcommand("start", "start a uenv session"); start_cli->add_option("-v,--view", view_description, "comma separated list of views to load"); @@ -33,7 +33,7 @@ void start_args::add_cli(CLI::App& cli, global_settings& settings) { start_cli->callback([&settings]() { settings.mode = uenv::mode_start; }); } -int start(const start_args& args, const global_settings& globals) { +int start(const start_args& args, [[maybe_unused]] const global_settings& globals) { fmt::println("running start with options {}", args); const auto env = concretise_env(args.uenv_description, args.view_description); diff --git a/src/uenv/lex.cpp b/src/uenv/lex.cpp index ca995fd..d97e8ec 100644 --- a/src/uenv/lex.cpp +++ b/src/uenv/lex.cpp @@ -110,9 +110,12 @@ class lexer_impl { ++stream_; return; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" case 'a' ... 'z': case 'A' ... 'Z': case '0' ... '9': +#pragma GCC diagnostic pop case '_': token_ = symbol(); return; diff --git a/test/unit/envvars.cpp b/test/unit/envvars.cpp index 0e350cf..6d6002b 100644 --- a/test/unit/envvars.cpp +++ b/test/unit/envvars.cpp @@ -93,7 +93,8 @@ TEST_CASE("envvarset get final values", "[envvars]") { ev.update_prefix_path("C", {uenv::update_kind::append, {"c", "d"}}); { - auto getenv = [](const std::string& name) -> const char* { + auto getenv = + []([[maybe_unused]] const std::string& name) -> const char* { return nullptr; }; REQUIRE_THAT(ev.get_values(getenv), From f8b591b34c031e2d01543078e723613afcf3e653 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Thu, 25 Jul 2024 10:16:44 +0200 Subject: [PATCH 02/18] Check formatting with GitHub actions --- .github/workflows/clang_format.yml | 17 +++++++++++++++++ src/cli/start.cpp | 6 ++++-- src/util/expected.h | 2 ++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/clang_format.yml diff --git a/.github/workflows/clang_format.yml b/.github/workflows/clang_format.yml new file mode 100644 index 0000000..3b2e492 --- /dev/null +++ b/.github/workflows/clang_format.yml @@ -0,0 +1,17 @@ +name: clang-format + +on: + merge_group: + pull_request: + push: + branches: [main] + +jobs: + formatting-check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Check formatting + uses: jidicula/clang-format-action@v4.13.0 + with: + clang-format-version: '14' # TODO? diff --git a/src/cli/start.cpp b/src/cli/start.cpp index 42c2e71..0cf2bc9 100644 --- a/src/cli/start.cpp +++ b/src/cli/start.cpp @@ -22,7 +22,8 @@ void start_help() { fmt::println("start help"); } -void start_args::add_cli(CLI::App& cli, [[maybe_unused]] global_settings& settings) { +void start_args::add_cli(CLI::App& cli, + [[maybe_unused]] global_settings& settings) { auto* start_cli = cli.add_subcommand("start", "start a uenv session"); start_cli->add_option("-v,--view", view_description, "comma separated list of views to load"); @@ -33,7 +34,8 @@ void start_args::add_cli(CLI::App& cli, [[maybe_unused]] global_settings& settin start_cli->callback([&settings]() { settings.mode = uenv::mode_start; }); } -int start(const start_args& args, [[maybe_unused]] const global_settings& globals) { +int start(const start_args& args, + [[maybe_unused]] const global_settings& globals) { fmt::println("running start with options {}", args); const auto env = concretise_env(args.uenv_description, args.view_description); diff --git a/src/util/expected.h b/src/util/expected.h index 04d2ffc..7f77ce0 100644 --- a/src/util/expected.h +++ b/src/util/expected.h @@ -1,5 +1,7 @@ #pragma once +// clang-format off + // Taken from https://github.com/eth-cscs/arbor, https://arbor-sim.org/ /* Copyright 2016-2020 Eidgenössische Technische Hochschule Zürich and From c4922ac0b6c5cb9a1d1e3e4ee32e3bc499905c30 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Thu, 25 Jul 2024 10:17:24 +0200 Subject: [PATCH 03/18] Add dependabot workflow to update GitHub actions automatically --- .github/dependabot.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..1230149 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" From d24bb4a522c69554a8b43a2301cb67d64f145397 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Wed, 24 Jul 2024 16:04:37 +0200 Subject: [PATCH 04/18] Add spdlog as a dependency --- meson.build | 5 +++-- subprojects/spdlog.wrap | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 subprojects/spdlog.wrap diff --git a/meson.build b/meson.build index 1224c7d..355b27b 100644 --- a/meson.build +++ b/meson.build @@ -18,6 +18,7 @@ conf_data = configuration_data() catch_dep = subproject('catch2').get_variable('catch2_with_main_dep') cli11_dep = subproject('cli11').get_variable('CLI11_dep') fmt_dep = subproject('fmt').get_variable('fmt_dep') +spdlog_dep = subproject('spdlog').get_variable('spdlog_dep') json_dep = subproject('nlohmann_json').get_variable('nlohmann_json_dep') sqlite3_dep = subproject('sqlite3').get_variable('sqlite3_dep') @@ -37,7 +38,7 @@ lib_inc = include_directories('src') lib_dep = declare_dependency( sources: lib_src, - dependencies: [sqlite3_dep, fmt_dep, json_dep], + dependencies: [sqlite3_dep, fmt_dep, spdlog_dep, json_dep], include_directories: lib_inc ) @@ -51,7 +52,7 @@ uenv_dep = [sqlite3_dep] uenv = executable('uenv', sources: uenv_src, - dependencies: [uenv_dep, lib_dep, fmt_dep, cli11_dep], + dependencies: [uenv_dep, lib_dep, fmt_dep, spdlog_dep, cli11_dep], c_args: ['-DVERSION="@0@"'.format(version)], install: true) diff --git a/subprojects/spdlog.wrap b/subprojects/spdlog.wrap new file mode 100644 index 0000000..af00d5a --- /dev/null +++ b/subprojects/spdlog.wrap @@ -0,0 +1,13 @@ +[wrap-file] +directory = spdlog-1.14.1 +source_url = https://github.com/gabime/spdlog/archive/refs/tags/v1.14.1.tar.gz +source_filename = spdlog-1.14.1.tar.gz +source_hash = 1586508029a7d0670dfcb2d97575dcdc242d3868a259742b69f100801ab4e16b +patch_filename = spdlog_1.14.1-1_patch.zip +patch_url = https://wrapdb.mesonbuild.com/v2/spdlog_1.14.1-1/get_patch +patch_hash = ae878e732330ea1048f90d7e117c40c0cd2a6fb8ae5492c7955818ce3aaade6c +source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/spdlog_1.14.1-1/spdlog-1.14.1.tar.gz +wrapdb_version = 1.14.1-1 + +[provide] +spdlog = spdlog_dep From 299bbdece01d5ee7e60eba6d5674fefe9a45d4fe Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Wed, 24 Jul 2024 17:04:24 +0200 Subject: [PATCH 05/18] Log messages using default spdlog logger --- src/cli/start.cpp | 16 +++++++++------- src/cli/uenv.cpp | 3 ++- src/uenv/env.cpp | 11 ++++++----- src/util/shell.cpp | 5 +++-- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/cli/start.cpp b/src/cli/start.cpp index 0cf2bc9..bcf1256 100644 --- a/src/cli/start.cpp +++ b/src/cli/start.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -36,12 +37,12 @@ void start_args::add_cli(CLI::App& cli, int start(const start_args& args, [[maybe_unused]] const global_settings& globals) { - fmt::println("running start with options {}", args); + spdlog::debug("running start with options {}", args); const auto env = concretise_env(args.uenv_description, args.view_description); if (!env) { - fmt::print("[error] {}\n", env.error()); + spdlog::error("{}", env.error()); return 1; } @@ -49,7 +50,7 @@ int start(const start_args& args, auto env_vars = uenv::getenv(*env); if (auto rval = uenv::setenv(env_vars); !rval) { - fmt::print("[error] setting environment variables {}\n", rval.error()); + spdlog::error("setting environment variables {}", rval.error()); return 1; } @@ -63,15 +64,16 @@ int start(const start_args& args, // find the current shell (zsh, bash, etc) auto shell = util::current_shell(); if (!shell) { - fmt::print("[error] unable to determine the current shell because {}\n", - shell.error()); + spdlog::error("unable to determine the current shell because {}", + shell.error()); + // TODO: return? } - fmt::println("[log] shell found: {}", shell->string()); + spdlog::info("shell found: {}", shell->string()); commands.push_back("--"); commands.push_back(shell->string()); - fmt::print("[log] exec {}\n", fmt::join(commands, " ")); + spdlog::info("exec {}", fmt::join(commands, " ")); return util::exec(commands); } diff --git a/src/cli/uenv.cpp b/src/cli/uenv.cpp index fbff942..1911c3b 100644 --- a/src/cli/uenv.cpp +++ b/src/cli/uenv.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "start.h" #include "uenv.h" @@ -23,7 +24,7 @@ int main(int argc, char** argv) { CLI11_PARSE(cli, argc, argv); - fmt::print(fmt::emphasis::bold | fg(fmt::color::orange), "{}\n", settings); + spdlog::debug("{}", settings); switch (settings.mode) { case uenv::mode_start: diff --git a/src/uenv/env.cpp b/src/uenv/env.cpp index 0cd277b..196b731 100644 --- a/src/uenv/env.cpp +++ b/src/uenv/env.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -61,7 +62,7 @@ concretise_env(const std::string& uenv_args, fmt::format("no mount point provided for {}", desc)); } } - fmt::println("{} will be mounted at {}", desc, mount); + spdlog::info("{} will be mounted at {}", desc, mount); // check that the mount point exists if (!fs::exists(mount)) { @@ -111,13 +112,13 @@ concretise_env(const std::string& uenv_args, name = std::move(result->name); description = std::move(result->description); views = std::move(result->views); - fmt::println("loaded meta with name {}", name); + spdlog::debug("loaded meta with name {}", name); } else { - fmt::println("error loading the uenv meta data in {}: {}", - *env_meta_path, result.error()); + spdlog::error("error loading the uenv meta data in {}: {}", + *env_meta_path, result.error()); } } else { - fmt::println("the meta data file {} does not exist", meta_path); + spdlog::debug("the meta data file {} does not exist", meta_path); description = ""; // generate a unique name for the uenv name = "anonymous"; diff --git a/src/util/shell.cpp b/src/util/shell.cpp index 47c7106..40b8e13 100644 --- a/src/util/shell.cpp +++ b/src/util/shell.cpp @@ -6,6 +6,7 @@ #include #include +#include #include @@ -41,11 +42,11 @@ int exec(const std::vector& args) { } argv.push_back(nullptr); - fmt::println("running {}", argv[0]); + spdlog::info("running {}", argv[0]); int r = execvp(argv[0], argv.data()); // } // end unsafe - fmt::print("[error] unable to launch a new shell\n"); + spdlog::error("[error] unable to launch a new shell"); return r; } From ba024fa348361f28a9798a51a44f0fc52b34938f Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Thu, 25 Jul 2024 11:28:39 +0200 Subject: [PATCH 06/18] Set default spdlog logger to log to syslog and stdout --- meson.build | 1 + src/cli/uenv.cpp | 2 ++ src/uenv/log.cpp | 21 +++++++++++++++++++++ src/uenv/log.h | 3 +++ 4 files changed, 27 insertions(+) create mode 100644 src/uenv/log.cpp create mode 100644 src/uenv/log.h diff --git a/meson.build b/meson.build index 355b27b..c9602f0 100644 --- a/meson.build +++ b/meson.build @@ -28,6 +28,7 @@ lib_src = [ 'src/uenv/env.cpp', 'src/uenv/envvars.cpp', 'src/uenv/lex.cpp', + 'src/uenv/log.cpp', 'src/uenv/meta.cpp', 'src/uenv/parse.cpp', 'src/util/strings.cpp', diff --git a/src/cli/uenv.cpp b/src/cli/uenv.cpp index 1911c3b..2e8ef69 100644 --- a/src/cli/uenv.cpp +++ b/src/cli/uenv.cpp @@ -5,6 +5,8 @@ #include #include +#include + #include "start.h" #include "uenv.h" diff --git a/src/uenv/log.cpp b/src/uenv/log.cpp new file mode 100644 index 0000000..ea05427 --- /dev/null +++ b/src/uenv/log.cpp @@ -0,0 +1,21 @@ +#include + +#include "spdlog/sinks/stdout_color_sinks.h" +#include "spdlog/sinks/syslog_sink.h" +#include + +#include "log.h" + +namespace uenv { +void init_log() { + auto console_sink = std::make_shared(); + console_sink->set_level(spdlog::level::warn); + + auto syslog_sink = std::make_shared( + "uenv", LOG_PID, 0, false); + syslog_sink->set_level(spdlog::level::trace); + + spdlog::set_default_logger(std::make_shared( + "uenv", spdlog::sinks_init_list({console_sink, syslog_sink}))); +} +} // namespace uenv diff --git a/src/uenv/log.h b/src/uenv/log.h new file mode 100644 index 0000000..5f76688 --- /dev/null +++ b/src/uenv/log.h @@ -0,0 +1,3 @@ +namespace uenv { +void init_log(); +} From f719f3c5cf4295d5abcad934fba2472e9ee59876 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Thu, 25 Jul 2024 11:36:22 +0200 Subject: [PATCH 07/18] Ignore spdlog subproject directory in .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b88e88c..068c010 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ subprojects/Catch2-* subprojects/CLI11-* subprojects/fmt-* subprojects/packagecache +subprojects/spdlog-* subprojects/sqlite-amalgamation-* subprojects/nlohmann_json-* From eec1df95c287132f92cb542200be56777ba8ad7f Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Thu, 25 Jul 2024 15:42:35 +0200 Subject: [PATCH 08/18] Initialize logging in uenv entry point --- src/cli/uenv.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cli/uenv.cpp b/src/cli/uenv.cpp index 2e8ef69..0e6c3b3 100644 --- a/src/cli/uenv.cpp +++ b/src/cli/uenv.cpp @@ -26,6 +26,8 @@ int main(int argc, char** argv) { CLI11_PARSE(cli, argc, argv); + uenv::init_log(); + spdlog::debug("{}", settings); switch (settings.mode) { From d162e2fbb5b45d5b71dba5d12b00da97eb08371d Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Thu, 25 Jul 2024 15:43:23 +0200 Subject: [PATCH 09/18] Remove duplicate [error] output when failing to launch shell --- src/util/shell.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/shell.cpp b/src/util/shell.cpp index 40b8e13..d35165f 100644 --- a/src/util/shell.cpp +++ b/src/util/shell.cpp @@ -46,7 +46,7 @@ int exec(const std::vector& args) { int r = execvp(argv[0], argv.data()); // } // end unsafe - spdlog::error("[error] unable to launch a new shell"); + spdlog::error("unable to launch a new shell"); return r; } From 08f66fc8a922ef3ea35c76921d4a2e46e20edbfd Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 26 Jul 2024 09:47:09 +0200 Subject: [PATCH 10/18] Log to stderr instead of stdout --- src/uenv/log.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uenv/log.cpp b/src/uenv/log.cpp index ea05427..f4127b7 100644 --- a/src/uenv/log.cpp +++ b/src/uenv/log.cpp @@ -8,7 +8,7 @@ namespace uenv { void init_log() { - auto console_sink = std::make_shared(); + auto console_sink = std::make_shared(); console_sink->set_level(spdlog::level::warn); auto syslog_sink = std::make_shared( From 20caa3a239c5bf6f8ee4971c626bb025b4856df4 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 26 Jul 2024 09:48:36 +0200 Subject: [PATCH 11/18] Use single-threaded loggers --- src/uenv/log.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uenv/log.cpp b/src/uenv/log.cpp index f4127b7..376e5f4 100644 --- a/src/uenv/log.cpp +++ b/src/uenv/log.cpp @@ -8,10 +8,10 @@ namespace uenv { void init_log() { - auto console_sink = std::make_shared(); + auto console_sink = std::make_shared(); console_sink->set_level(spdlog::level::warn); - auto syslog_sink = std::make_shared( + auto syslog_sink = std::make_shared( "uenv", LOG_PID, 0, false); syslog_sink->set_level(spdlog::level::trace); From bce136b8238f7a5b9d780535e555e37594c0f124 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 26 Jul 2024 11:48:31 +0200 Subject: [PATCH 12/18] Initialize logs for unit tests --- test/unit/main.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/unit/main.cpp b/test/unit/main.cpp index 2c344d5..f5bf5d8 100644 --- a/test/unit/main.cpp +++ b/test/unit/main.cpp @@ -1,2 +1,10 @@ #define CATCH_CONFIG_MAIN #include + +#include + +static struct unit_init_log { + unit_init_log() { + uenv::init_log(); + } +} uil{}; From 96f098dc0bb265ffeb983faff654a048cf2ca6d1 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 26 Jul 2024 12:17:42 +0200 Subject: [PATCH 13/18] Explicitly use default spdlog pattern for logs --- src/uenv/log.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/uenv/log.cpp b/src/uenv/log.cpp index 376e5f4..23c53d2 100644 --- a/src/uenv/log.cpp +++ b/src/uenv/log.cpp @@ -17,5 +17,6 @@ void init_log() { spdlog::set_default_logger(std::make_shared( "uenv", spdlog::sinks_init_list({console_sink, syslog_sink}))); + spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%^%l%$] %v"); } } // namespace uenv From f7087b6202dda95a041876b35c1abc3b70240e76 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 26 Jul 2024 12:35:15 +0200 Subject: [PATCH 14/18] Return error when unable to determine shell --- src/cli/start.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/start.cpp b/src/cli/start.cpp index b71f258..55cc35b 100644 --- a/src/cli/start.cpp +++ b/src/cli/start.cpp @@ -67,7 +67,7 @@ int start(const start_args& args, if (!shell) { spdlog::error("unable to determine the current shell because {}", shell.error()); - // TODO: return? + return 1; } spdlog::info("shell found: {}", shell->string()); From ce51e1a35094c62e8f6b4872d8e9c398af603355 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 26 Jul 2024 12:38:24 +0200 Subject: [PATCH 15/18] Update init_log to accept logging levels for syslog and console logs --- src/cli/uenv.cpp | 2 +- src/uenv/log.cpp | 7 ++++--- src/uenv/log.h | 5 ++++- test/unit/main.cpp | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/cli/uenv.cpp b/src/cli/uenv.cpp index 0e6c3b3..c4838ea 100644 --- a/src/cli/uenv.cpp +++ b/src/cli/uenv.cpp @@ -26,7 +26,7 @@ int main(int argc, char** argv) { CLI11_PARSE(cli, argc, argv); - uenv::init_log(); + uenv::init_log(spdlog::level::warn, spdlog::level::trace); spdlog::debug("{}", settings); diff --git a/src/uenv/log.cpp b/src/uenv/log.cpp index 23c53d2..e6df3a0 100644 --- a/src/uenv/log.cpp +++ b/src/uenv/log.cpp @@ -7,13 +7,14 @@ #include "log.h" namespace uenv { -void init_log() { +void init_log(spdlog::level::level_enum console_log_level, + spdlog::level::level_enum syslog_log_level) { auto console_sink = std::make_shared(); - console_sink->set_level(spdlog::level::warn); + console_sink->set_level(console_log_level); auto syslog_sink = std::make_shared( "uenv", LOG_PID, 0, false); - syslog_sink->set_level(spdlog::level::trace); + syslog_sink->set_level(syslog_log_level); spdlog::set_default_logger(std::make_shared( "uenv", spdlog::sinks_init_list({console_sink, syslog_sink}))); diff --git a/src/uenv/log.h b/src/uenv/log.h index 5f76688..c060f51 100644 --- a/src/uenv/log.h +++ b/src/uenv/log.h @@ -1,3 +1,6 @@ +#include + namespace uenv { -void init_log(); +void init_log(spdlog::level::level_enum console_log_level, + spdlog::level::level_enum syslog_log_level); } diff --git a/test/unit/main.cpp b/test/unit/main.cpp index f5bf5d8..9f94c54 100644 --- a/test/unit/main.cpp +++ b/test/unit/main.cpp @@ -5,6 +5,6 @@ static struct unit_init_log { unit_init_log() { - uenv::init_log(); + uenv::init_log(spdlog::level::trace, spdlog::level::off); } } uil{}; From 1578fb6e60a0f7309a4bc237616236fe4a8446b3 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 26 Jul 2024 13:07:09 +0200 Subject: [PATCH 16/18] Allow repeating --verbose/-v for increased verbosity in logs --- src/cli/uenv.cpp | 12 +++++++++++- src/cli/uenv.h | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/cli/uenv.cpp b/src/cli/uenv.cpp index c4838ea..4963025 100644 --- a/src/cli/uenv.cpp +++ b/src/cli/uenv.cpp @@ -26,7 +26,17 @@ int main(int argc, char** argv) { CLI11_PARSE(cli, argc, argv); - uenv::init_log(spdlog::level::warn, spdlog::level::trace); + // Warnings and errors are always logged. The verbosity level is increased + // with repeated uses of --verbose. + spdlog::level::level_enum console_log_level = spdlog::level::warn; + if (settings.verbose == 1) { + console_log_level = spdlog::level::info; + } else if (settings.verbose == 2) { + console_log_level = spdlog::level::debug; + } else if (settings.verbose >= 3) { + console_log_level = spdlog::level::trace; + } + uenv::init_log(console_log_level, spdlog::level::trace); spdlog::debug("{}", settings); diff --git a/src/cli/uenv.h b/src/cli/uenv.h index aaaab72..f741862 100644 --- a/src/cli/uenv.h +++ b/src/cli/uenv.h @@ -15,7 +15,7 @@ constexpr int mode_start = 1; // constexpr int mode_run = 3; struct global_settings { - bool verbose = false; + int verbose = false; bool no_color = false; int mode = mode_none; From 4e0cb6a4e1d4ee36cbefe1440f29726f1e4a8789 Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Fri, 26 Jul 2024 13:07:34 +0200 Subject: [PATCH 17/18] Explicitly set log level of combined default logger to trace to pass through all messages to subloggers --- src/uenv/log.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/uenv/log.cpp b/src/uenv/log.cpp index e6df3a0..a9d599d 100644 --- a/src/uenv/log.cpp +++ b/src/uenv/log.cpp @@ -16,8 +16,13 @@ void init_log(spdlog::level::level_enum console_log_level, "uenv", LOG_PID, 0, false); syslog_sink->set_level(syslog_log_level); + // The default logger is a combined logger that dispatches to the console + // and syslog. We explicitly set the level to trace to allow all messages to + // pass through the combined logger. The lower level loggers will filter + // messages at their level. spdlog::set_default_logger(std::make_shared( "uenv", spdlog::sinks_init_list({console_sink, syslog_sink}))); + spdlog::set_level(spdlog::level::trace); spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%^%l%$] %v"); } } // namespace uenv From c03807ebee10b106ce4077f17c8422123956668c Mon Sep 17 00:00:00 2001 From: Mikael Simberg Date: Wed, 14 Aug 2024 16:44:50 +0200 Subject: [PATCH 18/18] Initialize verbose flag with integer 0, not false --- src/cli/uenv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/uenv.h b/src/cli/uenv.h index f741862..ae39398 100644 --- a/src/cli/uenv.h +++ b/src/cli/uenv.h @@ -15,7 +15,7 @@ constexpr int mode_start = 1; // constexpr int mode_run = 3; struct global_settings { - int verbose = false; + int verbose = 0; bool no_color = false; int mode = mode_none;