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 {
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<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);
}
@ -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<logger::ERROR>(
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_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)

View File

@ -20,6 +20,7 @@
*/
#include <cstring>
#include <exception>
#include <iterator>
#include <stdexcept>
@ -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<std::string_view> 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) {