diff options
Diffstat (limited to 'src/common/logging.h')
-rw-r--r-- | src/common/logging.h | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/src/common/logging.h b/src/common/logging.h index 9564b33c..127a9700 100644 --- a/src/common/logging.h +++ b/src/common/logging.h @@ -3,21 +3,20 @@ #include "spdlog/spdlog.h" /** - * @brief Prints logging message into stderr and a file specified with `--log` - * option. + * Prints logging message into stderr and a file specified with `--log` option. * * Example usage: `LOG(info, "[data] Vocab size: {}", vocabSize)` * - * A good practise is to put `[namespace]` at the beginning of your message. + * A good practice is to put `[namespace]` at the beginning of the message. * * @param level Logging level: trace, debug, info, warn, error, critical - * @param ... + * @param ... Message text and variables */ #define LOG(level, ...) checkedLog("general", #level, __VA_ARGS__) /** - * @brief Prints logging message regarding validation into stderr and a file - * specified with `--valid-log` option. + * Prints logging message regarding validation into stderr and a file specified + * with `--valid-log` option. * * The message is automatically preceded by "[valid] ". * @@ -25,6 +24,34 @@ */ #define LOG_VALID(level, ...) checkedLog("valid", #level, __VA_ARGS__) +/** + * Prints critical error message and causes abnormal program termination by + * calling std::abort(). + * + * @param ... Message text and variables + */ +#define ABORT(...) \ + do { \ + checkedLog("general", "critical", __VA_ARGS__); \ + std::abort(); \ + } while(0) + +/** + * Prints critical error message and causes abnormal program termination if + * conditions is true. + * + * @param condition Condition expression + * @param ... Message text and variables + * + * @see \def ABORT(...) + */ +#define ABORT_IF(condition, ...) \ + do { \ + if(condition) { \ + ABORT(__VA_ARGS__); \ + } \ + } while(0) + typedef std::shared_ptr<spdlog::logger> Logger; Logger stderrLogger(const std::string&, const std::string&, @@ -38,8 +65,13 @@ class Config; template <class... Args> void checkedLog(std::string logger, std::string level, Args... args) { Logger log = spdlog::get(logger); - if(!log) + if(!log) { + if(level == "critical") { + auto stderr = stderrLogger("error", "Error: %v - aborting"); + stderr->critical(args...); + } return; + } if(level == "trace") log->trace(args...); |