Improve the test logger

This commit is contained in:
Marcus Holland-Moritz 2023-08-13 20:34:01 +02:00
parent 89cc6a9b87
commit fbc11291fa

View File

@ -23,12 +23,14 @@
#include <cstdlib> #include <cstdlib>
#include <mutex> #include <mutex>
#include <optional>
#include <string> #include <string>
#include <vector> #include <vector>
#include <folly/Conv.h> #include <folly/Conv.h>
#include "dwarfs/logger.h" #include "dwarfs/logger.h"
#include "dwarfs/util.h"
namespace dwarfs::test { namespace dwarfs::test {
@ -48,9 +50,9 @@ class test_logger : public ::dwarfs::logger {
int line; int line;
}; };
test_logger(level_type threshold = TRACE) test_logger(std::optional<level_type> threshold = std::nullopt)
: threshold_{threshold} : threshold_{threshold ? *threshold : default_threshold()}
, output_{debug_output_enabled()} { , output_{::dwarfs::getenv_is_enabled("DWARFS_TEST_LOGGER_OUTPUT")} {
if (output_ || threshold > level_type::INFO) { if (output_ || threshold > level_type::INFO) {
set_policy<debug_logger_policy>(); set_policy<debug_logger_policy>();
} else { } else {
@ -79,13 +81,11 @@ class test_logger : public ::dwarfs::logger {
void clear() { log_.clear(); } void clear() { log_.clear(); }
private: private:
static bool debug_output_enabled() { static level_type default_threshold() {
if (auto var = std::getenv("DWARFS_TEST_LOGGER_OUTPUT")) { if (auto var = std::getenv("DWARFS_TEST_LOGGER_LEVEL")) {
if (auto val = folly::tryTo<bool>(var); val && *val) { return ::dwarfs::logger::parse_level(var);
return true;
}
} }
return false; return level_type::INFO;
} }
std::mutex mx_; std::mutex mx_;