chore(logger): support LOG_FATAL

This commit is contained in:
Marcus Holland-Moritz 2024-01-14 19:10:09 +01:00
parent 0446513c06
commit e045a420c5
2 changed files with 24 additions and 4 deletions

View File

@ -48,10 +48,18 @@ class terminal;
class logger { class logger {
public: 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 char level_char(level_type level) {
static std::array<char, 6> lchars = {{'E', 'W', 'I', 'V', 'D', 'T'}}; static std::array<char, 7> lchars = {{'F', 'E', 'W', 'I', 'V', 'D', 'T'}};
return lchars.at(level); return lchars.at(level);
} }
@ -248,6 +256,10 @@ class log_proxy {
return LogPolicy::is_enabled_for(level); 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 { auto error(char const* file, int line) const {
return typename LogPolicy::template logger_type<logger::ERROR>( return typename LogPolicy::template logger_type<logger::ERROR>(
lgr_, logger::ERROR, file, line); lgr_, logger::ERROR, file, line);
@ -353,6 +365,7 @@ class log_proxy {
#define LOG_PROXY_DECL(policy) ::dwarfs::log_proxy<policy> log_ #define LOG_PROXY_DECL(policy) ::dwarfs::log_proxy<policy> log_
#define LOG_PROXY_INIT(lgr) log_(lgr) #define LOG_PROXY_INIT(lgr) log_(lgr)
#define LOG_GET_LOGGER log_.get_logger() #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_ERROR LOG_DETAIL_LEVEL(ERROR, log_, error)
#define LOG_WARN LOG_DETAIL_LEVEL(WARN, log_, warn) #define LOG_WARN LOG_DETAIL_LEVEL(WARN, log_, warn)
#define LOG_INFO LOG_DETAIL_LEVEL(INFO, log_, info) #define LOG_INFO LOG_DETAIL_LEVEL(INFO, log_, info)

View File

@ -20,6 +20,7 @@
*/ */
#include <cstring> #include <cstring>
#include <exception>
#include <iterator> #include <iterator>
#include <stdexcept> #include <stdexcept>
@ -44,6 +45,7 @@
namespace dwarfs { namespace dwarfs {
logger::level_type logger::parse_level(std::string_view level) { logger::level_type logger::parse_level(std::string_view level) {
// don't parse FATAL here, it's a special case
if (level == "error") { if (level == "error") {
return 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, void stream_logger::write(level_type level, const std::string& output,
char const* file, int line) { char const* file, int line) {
if (level <= threshold_) { if (level <= threshold_ || level == FATAL) {
auto t = get_current_time_string(); auto t = get_current_time_string();
std::string_view prefix; std::string_view prefix;
std::string_view suffix; std::string_view suffix;
@ -94,6 +96,7 @@ void stream_logger::write(level_type level, const std::string& output,
if (color_) { if (color_) {
switch (level) { switch (level) {
case FATAL:
case ERROR: case ERROR:
prefix = term_->color(termcolor::BOLD_RED); prefix = term_->color(termcolor::BOLD_RED);
suffix = term_->color(termcolor::NORMAL); suffix = term_->color(termcolor::NORMAL);
@ -128,7 +131,7 @@ void stream_logger::write(level_type level, const std::string& output,
std::string stacktrace; std::string stacktrace;
std::vector<std::string_view> st_lines; std::vector<std::string_view> st_lines;
if (enable_stack_trace_) { if (enable_stack_trace_ || level == FATAL) {
using namespace folly::symbolizer; using namespace folly::symbolizer;
Symbolizer symbolizer(LocationInfoMode::FULL); Symbolizer symbolizer(LocationInfoMode::FULL);
FrameArray<8> addresses; FrameArray<8> addresses;
@ -191,6 +194,10 @@ void stream_logger::write(level_type level, const std::string& output,
postamble(); postamble();
} }
if (level == FATAL) {
std::abort();
}
} }
void stream_logger::set_threshold(level_type threshold) { void stream_logger::set_threshold(level_type threshold) {