Error system simplification

This commit is contained in:
NaifBanana 2026-06-26 02:43:16 -05:00
parent cff7164d27
commit e4b164f29a
9 changed files with 279 additions and 332 deletions

View File

@ -1,5 +1,5 @@
toAbsolutePath(NB_CORE_SOURCE
./src/Errors.cpp
./src/ErrorsImpl.cpp
./src/Logger.cpp
./src/Processes.cpp
./src/StringUtils.cpp
@ -10,6 +10,7 @@ toAbsolutePath(NB_CORE_INCLUDE
./ANSITerm.hpp
./DataSink.hpp
./Errors.hpp
./ErrorsImpl.hpp
./Logger.hpp
./Processes.hpp
./StringUtils.hpp

View File

@ -76,7 +76,7 @@ public:
~MultithreadedDataProcessor() { type_ptr->stop(); }
bool isRunning() const noexcept override {
return this->_running && (_runningThread!=nullptr);
return this->_running;
}
bool run() override {
if (!type_ptr->isRunning()) {

View File

@ -2,281 +2,11 @@
#ifndef _NB_ERROR
#define _NB_ERROR
#include <exception>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include "ErrorsImpl.hpp"
#include "Logger.hpp"
#include "StringUtils.hpp"
#include "TypeTraits.hpp"
namespace nb {
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
template<typename T>
constexpr bool IsValidException_v = std::is_base_of_v<std::exception, T>;
template <typename T>
using IsValidException = std::enable_if_t<IsValidException_v<T>, bool>;
template<class ErrorType=NoneType>
class ErrorBase;
template <class ErrorType=NoneType>
class Error;
template<typename T>
constexpr bool IsValidNBError_v = std::is_base_of_v<ErrorBase<NoneType>, T>;
template <typename T>
using IsValidNBError = std::enable_if_t<IsValidNBError_v<T>, bool>;
template<>
class ErrorBase<NoneType> {
public:
const unsigned int code;
const std::string msg;
const std::string type;
const std::shared_ptr<const void> trace;
const bool traceIsNBError;
virtual std::string what() const noexcept = 0;
virtual std::shared_ptr<ErrorBase> make_shared() const noexcept = 0;
protected:
template<typename T=NoneType>
ErrorBase(const ErrorBase<T>& err) : ErrorBase(std::move(err)) {}
template<typename T=NoneType>
ErrorBase(ErrorBase<T>&& err) : ErrorBase(
err.code,
err.msg,
err.type,
(err.traceIsNBError)
? static_cast<const ErrorBase*>(err.trace.get())
: static_cast<const std::exception*>(err.trace.get())
) {}
ErrorBase(
unsigned int code_,
std::string msg_,
std::string type_,
const std::exception* trace_
) noexcept :
code(code_),
msg(msg_),
type(type_),
trace{ trace_ ?
std::static_pointer_cast<const void>(
std::make_shared<Error<NoneType>>(*trace_))
: nullptr,
},
traceIsNBError(false)
{}
template<typename T, std::enable_if_t<IsValidNBError_v<T>, bool> = true>
ErrorBase(
unsigned int code_,
std::string msg_,
std::string type_,
const T* trace_
) noexcept :
code(code_),
msg(msg_),
type(type_),
trace{trace_ ? trace_->make_shared() : nullptr},
traceIsNBError(true)
{}
};
template <class ErrorType>
class ErrorBase : public ErrorBase<NoneType> {
private:
using Base = ErrorBase<NoneType>;
void inline check_asserts() {
static_assert(std::is_same<const std::unordered_map<unsigned int, const char*>, decltype(ErrorType::ErrorMessages)>::value,
"const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
"a class member."
);
static_assert(std::is_enum_v<typename ErrorType::Codes>, "enum Codes must be a class member.");
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::Codes>, unsigned int>::value,
"enum Codes must be of underlying type unsigned int."
);
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
"const std::string type must be a class member."
);
}
public:
template <typename T>
ErrorBase(const ErrorBase<T>& cpy) : Base(cpy) { check_asserts(); }
virtual std::shared_ptr<Base> make_shared() const noexcept override {
return std::static_pointer_cast<Base>(
std::make_shared<ErrorBase>(*this)
);
}
virtual std::string what() const noexcept override {
std::string ret = msg;
if (trace) {
std::string trace_msg;
if (traceIsNBError) {
trace_msg = std::static_pointer_cast<const Base>(trace)->what();
} else {
trace_msg = std::string(std::static_pointer_cast<const Base>(trace)->what());
}
ret += nb::NEWLINE + indent_strblock(
trace_msg,
nb::TABOVER,
nb::TABOVER+"Trace: "
);
}
return ret;
}
static const std::string type;
static const ErrorCodeMap ErrorMessages;
using Base::code;
using Base::msg;
using Base::trace;
using Base::traceIsNBError;
friend ErrorType;
friend Error<ErrorType>;
protected:
using Base::Base;
template <typename T>
ErrorBase(
unsigned int code_,
std::string msg_,
std::string type_,
const T* trace_
) : Base(
code_,
msg_,
type_,
trace_
) { check_asserts(); }
};
template <typename ErrorType>
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
template <typename ErrorType>
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
template <class ErrorType>
class Error : public ErrorBase<ErrorType> {
using Base = ErrorBase<ErrorType>;
public:
template <typename T>
Error(
unsigned int code_,
const T& trace_
) noexcept : Base(
code_,
ErrorType::ErrorMessages.at(code_),
ErrorType::type,
&trace_
) {}
template<typename ET>
Error(
std::string msg_,
const ErrorBase<ET>& trace_
) noexcept : Base(
0,
msg_,
ErrorType::type,
&trace_
) {}
Error(
std::string msg_,
const std::exception& trace_
) noexcept : Base(
0,
msg_,
ErrorType::type,
&trace_
) {}
Error(unsigned int code_) noexcept : Base(
code_,
ErrorType::ErrorMessages.at(code_),
ErrorType::type,
NULLPTR<std::exception>
) {}
Error(std::string msg_) noexcept : Base(
0,
msg_,
ErrorType::type,
NULLPTR<std::exception>
) {}
using Base::what;
using Base::code;
using Base::msg;
using Base::trace;
using Base::traceIsNBError;
using Base::type;
using Base::ErrorMessages;
protected:
using Base::Base;
};
template<>
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
using Base = ErrorBase<Error<NoneType>>;
public:
using Base::Base;
Error(unsigned int code_=1) noexcept : Base(
code_,
ErrorMessages.at(code_),
type,
NULLPTR<std::exception>
) {}
Error(const std::exception& err) : Base(
Codes::STANDARD,
std::string(err.what()),
"std::exception",
nullptr
) {}
Error(std::string msg_) noexcept : Base(
Codes::UNDEFINED,
msg_,
type,
NULLPTR<std::exception>
) {}
enum Codes : unsigned int {
STANDARD, UNDEFINED
};
using Base::what;
using Base::code;
using Base::msg;
using Base::trace;
using Base::traceIsNBError;
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
} // namespace nb
#ifdef _NB_AUTOLOG
#ifdef _NB_CODE_ERROR_LOCATIONS
#ifndef LOG
@ -303,10 +33,11 @@ public:
#ifndef THROW
#ifdef _NB_AUTOLOG
#define THROW(args...) ERROR(args); throw args
#define THROW(args...) ERROR(args); nb::logger.stop(); throw args
#else
#define THROW(args...) throw args
#endif // _NB_CODE_ERROR_LOCATIONS
#endif // THROW
} // namespace nb
#endif // _NB_ERROR

View File

@ -0,0 +1,175 @@
#pragma once
#ifndef _NB_ERRORS_IMPL
#define _NB_ERRORS_IMPL
#include <exception>
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include "StringUtils.hpp"
#include "TypeTraits.hpp"
namespace nb {
typedef std::unordered_map<unsigned int, std::string> ErrorCodeMap;
template <class ErrorType=NoneType>
class Error;
class ErrorBase {
protected:
public:
const unsigned int code;
const std::string msg;
const std::string type;
const std::shared_ptr<ErrorBase> trace;
ErrorBase(const ErrorBase&) = default;
ErrorBase(const std::exception&) noexcept;
virtual std::string what() const noexcept {
std::string ret = msg;
if (trace) {
std::string trace_msg = msg;
ret += nb::NEWLINE + indent_strblock(
trace_msg,
nb::TABOVER,
nb::TABOVER+"Trace: "
);
}
return ret;
}
protected:
ErrorBase(
unsigned int code_,
std::string msg_,
std::string type_,
std::shared_ptr<ErrorBase> trace_
) noexcept :
code(code_),
msg(msg_),
type(type_),
trace{trace_}
{}
};
template <class ErrorType>
class Error : public ErrorBase {
private:
void inline check_asserts() {
static_assert(std::is_same<const ErrorCodeMap, decltype(ErrorType::ErrorMessages)>::value,
"const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
"a class member."
);
static_assert(std::is_enum_v<typename ErrorType::Codes>, "enum Codes must be a class member.");
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::Codes>, unsigned int>::value,
"enum Codes must be of underlying type unsigned int."
);
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
"const std::string type must be a class member."
);
}
protected:
using ErrorBase::ErrorBase;
public:
Error(
unsigned int code_,
const ErrorBase& trace_
) noexcept : ErrorBase(
code_,
ErrorType::ErrorMessages.at(code_),
ErrorType::type,
std::make_shared<ErrorBase>(trace_)
) { check_asserts(); }
Error(
std::string msg_,
const ErrorBase& trace_
) noexcept : ErrorBase(
0,
msg_,
ErrorType::type,
std::make_shared<ErrorBase>(trace_)
) { check_asserts(); }
Error(unsigned int code_) noexcept : ErrorBase(
code_,
ErrorType::ErrorMessages.at(code_),
ErrorType::type,
nullptr
) { check_asserts(); }
Error(std::string msg_) noexcept : ErrorBase(
0,
msg_,
ErrorType::type,
nullptr
) { check_asserts(); }
Error(
unsigned int code_,
const std::string& note_,
const ErrorBase& trace_
) noexcept : ErrorBase(
code_,
ErrorType::ErrorMessages.at(code_) + " (" + note_ + ")",
ErrorType::type,
std::make_shared<ErrorBase>(trace_)
) { check_asserts(); }
Error(unsigned int code_, const std::string& note_) noexcept : ErrorBase(
code_,
ErrorType::ErrorMessages.at(code_) + " (" + note_ + ")",
ErrorType::type,
nullptr
) { check_asserts(); }
using ErrorBase::what;
using ErrorBase::code;
using ErrorBase::msg;
using ErrorBase::trace;
using ErrorBase::type;
};
template<>
class Error<NoneType> : public Error<Error<NoneType>> {
using Base = Error<Error<NoneType>>;
public:
using Base::Base;
Error(unsigned int code_=1) noexcept : Base(code_) {}
Error(const std::exception& err) : Base(
Codes::STANDARD,
std::string(err.what()),
"std::exception",
nullptr
) {}
// fix
Error(std::string msg_) noexcept : Base(
Codes::UNDEFINED,
msg_
) {}
enum Codes : unsigned int {
STANDARD, UNDEFINED
};
using Base::what;
using Base::code;
using Base::msg;
using Base::trace;
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
} // namespace nb
#endif // _NB_ERRORS_IMPL

View File

@ -9,13 +9,13 @@
#include <vector>
#include "DataSink.hpp"
#include "ErrorsImpl.hpp"
#include "Processes.hpp"
#include "ThreadSafeQueue.hpp"
#include "TypeTraits.hpp"
namespace nb {
template <typename T>
class ErrorBase;
typedef std::chrono::time_point<
@ -32,6 +32,7 @@ class LoggerBase
using StreamType = ST;
using LoggerType = Logger;
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
public:
bool run() override {
if (!static_cast<LoggerType*>(this)->isRunning()) {
@ -45,6 +46,8 @@ public:
}
return static_cast<LoggerType*>(this)->isRunning();
}
using Base::flush;
protected:
LoggerBase() = default;
@ -73,7 +76,33 @@ public:
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
template<typename U>
void log(
U val,
std::string file="",
unsigned int line=0
) { static_cast<LoggerType*>(this)->write_message(val, 0x00, file, line); }
template<typename U>
void warn(
U val,
uint8_t lvl=0x01,
std::string file="",
unsigned int line=0
) { static_cast<LoggerType*>(this)->write_message(val, lvl, file, line); }
void error(
const ErrorBase& val,
std::string file="",
unsigned int line=0
) {
static_cast<LoggerType*>(this)->write_message(val, 0xFF, file, line);
static_cast<LoggerType*>(this)->flush();
}
protected:
std::vector<std::ostream*> _ostream;
void write_message(
std::string msg,
uint8_t lvl=0x00,
std::string file="",
@ -91,56 +120,24 @@ public:
}
template <size_t N>
void log(
void write_message(
char const(&msg) [N],
uint8_t lvl=0x00,
std::string file="",
unsigned int line=0
) {
static_cast<LoggerType*>(this)->log(std::string(msg), lvl, file, line);
static_cast<LoggerType*>(this)->write_message(std::string(msg), lvl, file, line);
}
template <typename T>
void log(
const ErrorBase<T>& err,
uint8_t lvl=0xFF,
void write_message(
const ErrorBase& err,
uint8_t lvl=0x00,
std::string file="",
unsigned int line=0
) {
static_cast<LoggerType*>(this)->log(err.what(), lvl, file, line);
static_cast<LoggerType*>(this)->write_message(err.what(), lvl, file, line);
}
template <typename T>
std::enable_if_t<std::is_base_of_v<std::exception, T>, void> log(
const T& err,
uint8_t lvl=0xFF,
std::string file="",
unsigned int line=0
) {
static_cast<LoggerType*>(this)->log(std::string(err.what()), lvl, file, line);
}
template<typename U>
void warn(
U val,
uint8_t lvl=0x01,
std::string file="",
unsigned int line=0
) { static_cast<LoggerType*>(this)->log(val, lvl, file, line); }
template<typename U>
void error(
U val,
std::string file="",
unsigned int line=0
) {
static_cast<LoggerType*>(this)->log(val, 0xFF, file, line);
static_cast<LoggerType*>(this)->flush();
}
protected:
std::vector<std::ostream*> _ostream;
};
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
@ -166,6 +163,8 @@ protected:
bool process(const LogEvent& msg);
};
extern const bool LOGGER_RUNNING;
#ifndef _NB_NO_LOGGER
extern DefaultDebugLogger logger;
#endif // _NB_NO_LOGGER

View File

@ -9,6 +9,12 @@
namespace nb {
template<typename T>
using SharedVector = std::vector<std::shared_ptr<T>>;
template<typename T>
using RValueVector = std::vector<T&&>;
using ByteVector = std::vector<uint8_t>;
class ObjectManagerError : public Error<ObjectManagerError> {
@ -25,6 +31,38 @@ class ObjectManagerError : public Error<ObjectManagerError> {
static const ErrorCodeMap ErrorMessages;
};
template<typename T>
ByteVector vectorToBytes(const std::vector<T>& vec) {
unsigned int num_bytes = vec.size() * sizeof(T);
ByteVector ret(num_bytes);
memcpy(ret.data(), vec.data(), num_bytes);
return ret;
}
template<typename T, typename S>
ByteVector concatVectorBytes(const std::vector<T>& vec1, const std::vector<S>& vec2) {
ByteVector vec1_raw = vectorToBytes<T>(vec1);
ByteVector vec2_raw = vectorToBytes<S>(vec2);
unsigned int vec1_raw_size = vec1_raw.size();
unsigned int vec2_raw_size = vec2_raw.size();
ByteVector ret(vec1_raw_size + vec2_raw_size);
memcpy(ret.data(), vec1_raw.data(), vec1_raw_size);
memcpy(ret.data() + vec1_raw_size, vec2_raw.data(), vec2_raw_size);
return ret;
}
template<typename T>
std::vector<T> bytesToVector(const ByteVector& vec) {
if (vec.size() % sizeof(T) != 0) {
THROW(std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">."));
}
unsigned int num_elmts = vec.size() / sizeof(T);
std::vector<T> ret(num_elmts);
memcpy(ret.data(), vec.data(), vec.size());
return ret;
}
template <typename T>
class ThreadsafeObjectLock;

View File

@ -1,13 +0,0 @@
#include "Errors.hpp"
#include "TypeTraits.hpp"
namespace nb {
const std::string Error<>::type = "nb::Error";
const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = {
{Error::Codes::STANDARD, "std::exception"},
{Error::Codes::UNDEFINED, "Error"}
};
}

View File

@ -0,0 +1,16 @@
#include "ErrorsImpl.hpp"
namespace nb {
const std::string Error<>::type = "nb::Error";
const ErrorCodeMap Error<>::ErrorMessages = {
{Error::Codes::STANDARD, "std::exception"},
{Error::Codes::UNDEFINED, "Error"}
};
//ErrorBase::
ErrorBase::ErrorBase(const std::exception& exception_) noexcept
: ErrorBase(Error<NoneType>(exception_)) {}
}

View File

@ -33,6 +33,6 @@ static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
return log.run();
}
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
const bool LOGGER_RUNNING = RUN_LOGGER(logger);
} // namespace nb