From e045a420c5e88b77a4808a5780c7e4ecdbc54c56 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 14 Jan 2024 19:10:09 +0100 Subject: [PATCH] chore(logger): support LOG_FATAL --- include/dwarfs/logger.h | 17 +++++++++++++++-- src/dwarfs/logger.cpp | 11 +++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/include/dwarfs/logger.h b/include/dwarfs/logger.h index 67cc7dc2..7029b4b2 100644 --- a/include/dwarfs/logger.h +++ b/include/dwarfs/logger.h @@ -48,10 +48,18 @@ class terminal; class logger { public: - enum level_type : unsigned { ERROR, WARN, INFO, VERBOSE, DEBUG, TRACE }; + enum level_type : unsigned { + FATAL, + ERROR, + WARN, + INFO, + VERBOSE, + DEBUG, + TRACE + }; static char level_char(level_type level) { - static std::array lchars = {{'E', 'W', 'I', 'V', 'D', 'T'}}; + static std::array lchars = {{'F', 'E', 'W', 'I', 'V', 'D', 'T'}}; return lchars.at(level); } @@ -248,6 +256,10 @@ class log_proxy { return LogPolicy::is_enabled_for(level); } + auto fatal(char const* file, int line) const { + return level_logger(lgr_, logger::FATAL, file, line); + } + auto error(char const* file, int line) const { return typename LogPolicy::template logger_type( lgr_, logger::ERROR, file, line); @@ -353,6 +365,7 @@ class log_proxy { #define LOG_PROXY_DECL(policy) ::dwarfs::log_proxy log_ #define LOG_PROXY_INIT(lgr) log_(lgr) #define LOG_GET_LOGGER log_.get_logger() +#define LOG_FATAL log_.fatal(__FILE__, __LINE__) #define LOG_ERROR LOG_DETAIL_LEVEL(ERROR, log_, error) #define LOG_WARN LOG_DETAIL_LEVEL(WARN, log_, warn) #define LOG_INFO LOG_DETAIL_LEVEL(INFO, log_, info) diff --git a/src/dwarfs/logger.cpp b/src/dwarfs/logger.cpp index 400e4034..efcaed4c 100644 --- a/src/dwarfs/logger.cpp +++ b/src/dwarfs/logger.cpp @@ -20,6 +20,7 @@ */ #include +#include #include #include @@ -44,6 +45,7 @@ namespace dwarfs { logger::level_type logger::parse_level(std::string_view level) { + // don't parse FATAL here, it's a special case if (level == "error") { return ERROR; } @@ -86,7 +88,7 @@ std::string_view stream_logger::get_newline() const { return "\n"; } void stream_logger::write(level_type level, const std::string& output, char const* file, int line) { - if (level <= threshold_) { + if (level <= threshold_ || level == FATAL) { auto t = get_current_time_string(); std::string_view prefix; std::string_view suffix; @@ -94,6 +96,7 @@ void stream_logger::write(level_type level, const std::string& output, if (color_) { switch (level) { + case FATAL: case ERROR: prefix = term_->color(termcolor::BOLD_RED); suffix = term_->color(termcolor::NORMAL); @@ -128,7 +131,7 @@ void stream_logger::write(level_type level, const std::string& output, std::string stacktrace; std::vector st_lines; - if (enable_stack_trace_) { + if (enable_stack_trace_ || level == FATAL) { using namespace folly::symbolizer; Symbolizer symbolizer(LocationInfoMode::FULL); FrameArray<8> addresses; @@ -191,6 +194,10 @@ void stream_logger::write(level_type level, const std::string& output, postamble(); } + + if (level == FATAL) { + std::abort(); + } } void stream_logger::set_threshold(level_type threshold) {