From 53a15e5a7fd889f01ba100d64f86c0b963bf00b5 Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Sat, 1 Aug 2020 11:39:40 +0200 Subject: [PATCH] Determine log level dynamically The log level can now be set by environment variable `XWIM_LOGLEVEL`. Alternatively a default log level per build type (determined by NDEBUG flag) is used. --- README.md | 2 +- src/main.cpp | 4 +-- src/meson.build | 7 +---- src/util/log.hpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 src/util/log.hpp diff --git a/README.md b/README.md index fb656ba..6f7c049 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ likely move to GitHub as it's main repository. If you want to contribute, you can either issue a pull request on it's Github mirror (will be cherry picked into the main repository) or send patches to -`dev [at] friedl [dot] net`. +dev[at]friedl[dot]net. If you are interested in a long-term co-maintainership you can also drop me a mail for an account on https://git.friedl.net. diff --git a/src/main.cpp b/src/main.cpp index a5e64c9..5d4cf5b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,4 @@ #include -#include namespace logger = spdlog; #include @@ -7,12 +6,13 @@ namespace logger = spdlog; #include #include +#include "util/log.hpp" #include "archive.hpp" #include "spec.hpp" #include "fileformats.hpp" int main(int argc, char** argv) { - logger::set_level(logger::level::trace); + xwim::log::init(); try { std::filesystem::path filepath{argv[1]}; diff --git a/src/meson.build b/src/meson.build index a7d650e..3889453 100644 --- a/src/meson.build +++ b/src/meson.build @@ -2,13 +2,8 @@ xwim_src = ['main.cpp', 'archive.cpp', 'archive_sys.cpp'] -xwim_inc = ['archive.hpp', - 'spec.hpp', - 'archive_sys.hpp', - 'fileformats.hpp'] - xwim_libs = [dependency('libarchive', required: true), dependency('fmt', required: true), dependency('spdlog', required: true)] -executable('xwim', xwim_src, xwim_inc, dependencies: xwim_libs) +executable('xwim', xwim_src, dependencies: xwim_libs) diff --git a/src/util/log.hpp b/src/util/log.hpp new file mode 100644 index 0000000..29d1c50 --- /dev/null +++ b/src/util/log.hpp @@ -0,0 +1,79 @@ +#pragma once + +#include +#include +#include + +#ifdef NDEBUG +#define XWIM_LOGLEVEL SPDLOG_LEVEL_ERROR +#else +#define XWIM_LOGLEVEL SPDLOG_LEVEL_DEBUG +#endif + +namespace xwim { +namespace log { + +/** + * Get log level from XWIM_LOGLEVEL environment variable. + * For valid values see SPDLOG_LEVEL_NAMES in spdlog/common.h + * + * @returns spdlog::level::level_enum::off if no valid XWIM_LOGLEVEL defined + */ +spdlog::level::level_enum _init_from_env() { + char* env_lvl = std::getenv("XWIM_LOGLEVEL"); + if (!env_lvl) { + return spdlog::level::level_enum::off; + } + + spdlog::level::level_enum lvl = spdlog::level::from_str(env_lvl); + + //`::from_str` returns `off` if no match found + if (spdlog::level::level_enum::off == lvl) { + spdlog::debug("No environment definition for log level"); // uses default + // logger/level + } + + return lvl; +}; + +/** + * Get log level from compile time definition. + * + * @return spdlog::level::level_enum::error for release builds (-DNDEBUG) + * spdlog::level::level_enum::debug for debug builds + */ +spdlog::level::level_enum _init_from_compile() { + return static_cast(XWIM_LOGLEVEL); +} + +/** + * Determine the log level from various sources at runtime. + * + * The log level is determined from sources in the following order (first + * wins): + * 1. The `level` argument + * 2. The XWIM_LOGLEVEL environment variable + * 3. The default for the build type (-DNDEBUG) + * -> ERROR for release builds + * -> DEBUG for debug builds + * + * 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) { + if (spdlog::level::level_enum::off != level) { + spdlog::set_level(level); + return; + } + + level = _init_from_env(); + if (spdlog::level::level_enum::off != level) { + spdlog::set_level(level); + return; + } + + return spdlog::set_level(_init_from_compile()); +} + +} // namespace log +} // namespace xwim