Determine log level dynamically
All checks were successful
continuous-integration/drone/push Build is passing

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.
This commit is contained in:
Armin Friedl 2020-08-01 11:39:40 +02:00
parent c842c02c6f
commit 53a15e5a7f
Signed by: armin
GPG key ID: 48C726EEE7FBCBC8
4 changed files with 83 additions and 9 deletions

View file

@ -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.

View file

@ -1,5 +1,4 @@
#include <spdlog/common.h>
#include <spdlog/spdlog.h>
namespace logger = spdlog;
#include <iostream>
@ -7,12 +6,13 @@ namespace logger = spdlog;
#include <string>
#include <list>
#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]};

View file

@ -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)

79
src/util/log.hpp Normal file
View file

@ -0,0 +1,79 @@
#pragma once
#include <spdlog/common.h>
#include <spdlog/spdlog.h>
#include <cstdlib>
#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<spdlog::level::level_enum>(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