Skip to content

File error_util.hpp

File List > illixr > error_util.hpp

Go to the documentation of this file

#pragma once
#if defined(_WIN32) || defined(_WIN64)
#    include 
#endif

#include "global_module_defs.hpp"

#include 
#include 
#include 

#ifdef NDEBUG
#    include 
#    include 
#endif
#ifndef RAC_ERRNO
#    define RAC_ERRNO() ILLIXR::report_and_clear_errno(__FILE__, __LINE__, __func__)
#endif 

#ifndef RAC_ERRNO_MSG
#    define RAC_ERRNO_MSG(msg) ILLIXR::report_and_clear_errno(__FILE__, __LINE__, __func__, msg)
#endif 

namespace ILLIXR {
// can't use switchboard interface here
static const bool ENABLE_VERBOSE_ERRORS{getenv("ILLIXR_ENABLE_VERBOSE_ERRORS") != nullptr &&
                                        ILLIXR::str_to_bool(getenv("ILLIXR_ENABLE_VERBOSE_ERRORS"))};

inline void report_and_clear_errno([[maybe_unused]] const std::string& file, [[maybe_unused]] const int& line,
                                   [[maybe_unused]] const std::string& function, [[maybe_unused]] const std::string& msg = "") {
#ifndef NDEBUG
    if (errno > 0) {
        if (ILLIXR::ENABLE_VERBOSE_ERRORS) {
            spdlog::get("illixr")->error("[error_util] || Errno was set: {} @ {}:{} [{}]", errno, file, line, function);
            if (!msg.empty()) {
                spdlog::get("illixr")->error("[error_util]|> Message: {}", msg);
            }
        }
        errno = 0;
    }
#endif 
}

inline void abort(const std::string& msg = "", [[maybe_unused]] const int error_val = 1) {
    spdlog::get("illixr")->error("[error_util] ** ERROR ** {}", msg);
#ifndef NDEBUG
    std::abort();
#else  
    std::exit(error_val);
#endif 
}

} // namespace ILLIXR