mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-09 12:28:13 -04:00
refactor(logger): add run-time threshold check
This commit is contained in:
parent
5c5905ea02
commit
6770180a86
@ -64,6 +64,7 @@ class logger {
|
|||||||
|
|
||||||
virtual void write(level_type level, std::string_view output,
|
virtual void write(level_type level, std::string_view output,
|
||||||
std::source_location loc) = 0;
|
std::source_location loc) = 0;
|
||||||
|
virtual level_type threshold() const = 0;
|
||||||
|
|
||||||
std::string_view policy_name() const { return policy_name_; }
|
std::string_view policy_name() const { return policy_name_; }
|
||||||
|
|
||||||
@ -100,6 +101,7 @@ class stream_logger : public logger {
|
|||||||
|
|
||||||
void write(level_type level, std::string_view output,
|
void write(level_type level, std::string_view output,
|
||||||
std::source_location loc) override;
|
std::source_location loc) override;
|
||||||
|
level_type threshold() const override;
|
||||||
|
|
||||||
void set_threshold(level_type threshold);
|
void set_threshold(level_type threshold);
|
||||||
void set_with_context(bool with_context) { with_context_ = with_context; }
|
void set_with_context(bool with_context) { with_context_ = with_context; }
|
||||||
@ -130,6 +132,7 @@ class null_logger : public logger {
|
|||||||
null_logger();
|
null_logger();
|
||||||
|
|
||||||
void write(level_type, std::string_view, std::source_location) override {}
|
void write(level_type, std::string_view, std::source_location) override {}
|
||||||
|
level_type threshold() const override { return FATAL; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class level_log_entry {
|
class level_log_entry {
|
||||||
@ -249,12 +252,17 @@ template <typename LogPolicy>
|
|||||||
class log_proxy {
|
class log_proxy {
|
||||||
public:
|
public:
|
||||||
log_proxy(logger& lgr)
|
log_proxy(logger& lgr)
|
||||||
: lgr_(lgr) {}
|
: lgr_(lgr)
|
||||||
|
, threshold_(lgr.threshold()) {}
|
||||||
|
|
||||||
static constexpr bool is_enabled_for(logger::level_type level) {
|
static constexpr bool policy_is_enabled_for(logger::level_type level) {
|
||||||
return LogPolicy::is_enabled_for(level);
|
return LogPolicy::is_enabled_for(level);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool logger_is_enabled_for(logger::level_type level) const {
|
||||||
|
return level <= threshold_;
|
||||||
|
}
|
||||||
|
|
||||||
auto fatal(std::source_location loc) const {
|
auto fatal(std::source_location loc) const {
|
||||||
return level_log_entry(lgr_, logger::FATAL, loc);
|
return level_log_entry(lgr_, logger::FATAL, loc);
|
||||||
}
|
}
|
||||||
@ -353,11 +361,13 @@ class log_proxy {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
logger& lgr_;
|
logger& lgr_;
|
||||||
|
logger::level_type threshold_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define LOG_DETAIL_LEVEL(level, lgr, method) \
|
#define LOG_DETAIL_LEVEL(level, lgr, method) \
|
||||||
if constexpr (std::decay_t<decltype(lgr)>::is_enabled_for( \
|
if constexpr (std::decay_t<decltype(lgr)>::policy_is_enabled_for( \
|
||||||
::dwarfs::logger::level)) \
|
::dwarfs::logger::level)) \
|
||||||
|
if (lgr.logger_is_enabled_for(::dwarfs::logger::level)) \
|
||||||
lgr.method(std::source_location::current())
|
lgr.method(std::source_location::current())
|
||||||
|
|
||||||
#define LOG_PROXY(policy, lgr) ::dwarfs::log_proxy<policy> log_(lgr)
|
#define LOG_PROXY(policy, lgr) ::dwarfs::log_proxy<policy> log_(lgr)
|
||||||
|
@ -158,6 +158,10 @@ void stream_logger::write_nolock(std::string_view output) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger::level_type stream_logger::threshold() const {
|
||||||
|
return threshold_.load();
|
||||||
|
}
|
||||||
|
|
||||||
void stream_logger::write(level_type level, std::string_view output,
|
void stream_logger::write(level_type level, std::string_view output,
|
||||||
std::source_location loc) {
|
std::source_location loc) {
|
||||||
if (level <= threshold_ || level == FATAL) {
|
if (level <= threshold_ || level == FATAL) {
|
||||||
|
@ -59,6 +59,8 @@ class test_logger : public ::dwarfs::logger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
level_type threshold() const override { return threshold_; }
|
||||||
|
|
||||||
void write(level_type level, std::string_view output,
|
void write(level_type level, std::string_view output,
|
||||||
std::source_location loc) override {
|
std::source_location loc) override {
|
||||||
if (output_ && level <= output_threshold_) {
|
if (output_ && level <= output_threshold_) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user