WIP: Custom Erroring system #1
@ -1,5 +1,5 @@
|
|||||||
toAbsolutePath(NB_CORE_SOURCE
|
toAbsolutePath(NB_CORE_SOURCE
|
||||||
./src/Errors.cpp
|
./src/ErrorsImpl.cpp
|
||||||
./src/Logger.cpp
|
./src/Logger.cpp
|
||||||
./src/Processes.cpp
|
./src/Processes.cpp
|
||||||
./src/StringUtils.cpp
|
./src/StringUtils.cpp
|
||||||
@ -10,6 +10,7 @@ toAbsolutePath(NB_CORE_INCLUDE
|
|||||||
./ANSITerm.hpp
|
./ANSITerm.hpp
|
||||||
./DataSink.hpp
|
./DataSink.hpp
|
||||||
./Errors.hpp
|
./Errors.hpp
|
||||||
|
./ErrorsImpl.hpp
|
||||||
./Logger.hpp
|
./Logger.hpp
|
||||||
./Processes.hpp
|
./Processes.hpp
|
||||||
./StringUtils.hpp
|
./StringUtils.hpp
|
||||||
|
|||||||
@ -72,11 +72,11 @@ template<typename DataType, typename ProcessorType>
|
|||||||
class MultithreadedDataProcessor
|
class MultithreadedDataProcessor
|
||||||
: public BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType> {
|
: public BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType> {
|
||||||
using Base = BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType>;
|
using Base = BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType>;
|
||||||
public:
|
public:
|
||||||
~MultithreadedDataProcessor() { type_ptr->stop(); }
|
~MultithreadedDataProcessor() { type_ptr->stop(); }
|
||||||
|
|
||||||
bool isRunning() const noexcept override {
|
bool isRunning() const noexcept override {
|
||||||
return this->_running && (_runningThread!=nullptr);
|
return this->_running;
|
||||||
}
|
}
|
||||||
bool run() override {
|
bool run() override {
|
||||||
if (!type_ptr->isRunning()) {
|
if (!type_ptr->isRunning()) {
|
||||||
@ -98,7 +98,7 @@ public:
|
|||||||
return !type_ptr->isRunning();
|
return !type_ptr->isRunning();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
using Base::Base;
|
using Base::Base;
|
||||||
|
|
||||||
unsigned int count() const {
|
unsigned int count() const {
|
||||||
|
|||||||
@ -2,281 +2,11 @@
|
|||||||
#ifndef _NB_ERROR
|
#ifndef _NB_ERROR
|
||||||
#define _NB_ERROR
|
#define _NB_ERROR
|
||||||
|
|
||||||
#include <exception>
|
#include "ErrorsImpl.hpp"
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <type_traits>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
#include "StringUtils.hpp"
|
|
||||||
#include "TypeTraits.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
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_AUTOLOG
|
||||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||||
#ifndef LOG
|
#ifndef LOG
|
||||||
@ -303,10 +33,11 @@ public:
|
|||||||
|
|
||||||
#ifndef THROW
|
#ifndef THROW
|
||||||
#ifdef _NB_AUTOLOG
|
#ifdef _NB_AUTOLOG
|
||||||
#define THROW(args...) ERROR(args); throw args
|
#define THROW(args...) ERROR(args); nb::logger.stop(); throw args
|
||||||
#else
|
#else
|
||||||
#define THROW(args...) throw args
|
#define THROW(args...) throw args
|
||||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||||
#endif // THROW
|
#endif // THROW
|
||||||
|
} // namespace nb
|
||||||
|
|
||||||
#endif // _NB_ERROR
|
#endif // _NB_ERROR
|
||||||
175
engine/NBCore/ErrorsImpl.hpp
Normal file
175
engine/NBCore/ErrorsImpl.hpp
Normal 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
|
||||||
@ -9,13 +9,13 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "DataSink.hpp"
|
#include "DataSink.hpp"
|
||||||
|
#include "ErrorsImpl.hpp"
|
||||||
#include "Processes.hpp"
|
#include "Processes.hpp"
|
||||||
#include "ThreadSafeQueue.hpp"
|
#include "ThreadSafeQueue.hpp"
|
||||||
#include "TypeTraits.hpp"
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
class ErrorBase;
|
class ErrorBase;
|
||||||
|
|
||||||
typedef std::chrono::time_point<
|
typedef std::chrono::time_point<
|
||||||
@ -32,7 +32,8 @@ class LoggerBase
|
|||||||
using StreamType = ST;
|
using StreamType = ST;
|
||||||
using LoggerType = Logger;
|
using LoggerType = Logger;
|
||||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||||
public:
|
|
||||||
|
public:
|
||||||
bool run() override {
|
bool run() override {
|
||||||
if (!static_cast<LoggerType*>(this)->isRunning()) {
|
if (!static_cast<LoggerType*>(this)->isRunning()) {
|
||||||
this->_running = true;
|
this->_running = true;
|
||||||
@ -45,7 +46,9 @@ public:
|
|||||||
}
|
}
|
||||||
return static_cast<LoggerType*>(this)->isRunning();
|
return static_cast<LoggerType*>(this)->isRunning();
|
||||||
}
|
}
|
||||||
protected:
|
using Base::flush;
|
||||||
|
|
||||||
|
protected:
|
||||||
LoggerBase() = default;
|
LoggerBase() = default;
|
||||||
|
|
||||||
StreamType _ostream;
|
StreamType _ostream;
|
||||||
@ -73,7 +76,33 @@ public:
|
|||||||
|
|
||||||
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
|
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
|
||||||
|
|
||||||
|
template<typename U>
|
||||||
void log(
|
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,
|
std::string msg,
|
||||||
uint8_t lvl=0x00,
|
uint8_t lvl=0x00,
|
||||||
std::string file="",
|
std::string file="",
|
||||||
@ -91,56 +120,24 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
void log(
|
void write_message(
|
||||||
char const(&msg) [N],
|
char const(&msg) [N],
|
||||||
uint8_t lvl=0x00,
|
uint8_t lvl=0x00,
|
||||||
std::string file="",
|
std::string file="",
|
||||||
unsigned int line=0
|
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 write_message(
|
||||||
void log(
|
const ErrorBase& err,
|
||||||
const ErrorBase<T>& err,
|
uint8_t lvl=0x00,
|
||||||
uint8_t lvl=0xFF,
|
|
||||||
std::string file="",
|
std::string file="",
|
||||||
unsigned int line=0
|
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> {
|
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||||
@ -150,7 +147,7 @@ class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
|||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
struct LogRow;
|
struct LogRow;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Base::Base;
|
using Base::Base;
|
||||||
|
|
||||||
~DefaultDebugLogger() { stop(); }
|
~DefaultDebugLogger() { stop(); }
|
||||||
@ -166,6 +163,8 @@ protected:
|
|||||||
bool process(const LogEvent& msg);
|
bool process(const LogEvent& msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern const bool LOGGER_RUNNING;
|
||||||
|
|
||||||
#ifndef _NB_NO_LOGGER
|
#ifndef _NB_NO_LOGGER
|
||||||
extern DefaultDebugLogger logger;
|
extern DefaultDebugLogger logger;
|
||||||
#endif // _NB_NO_LOGGER
|
#endif // _NB_NO_LOGGER
|
||||||
|
|||||||
@ -9,6 +9,12 @@
|
|||||||
|
|
||||||
namespace nb {
|
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>;
|
using ByteVector = std::vector<uint8_t>;
|
||||||
|
|
||||||
class ObjectManagerError : public Error<ObjectManagerError> {
|
class ObjectManagerError : public Error<ObjectManagerError> {
|
||||||
@ -25,6 +31,38 @@ class ObjectManagerError : public Error<ObjectManagerError> {
|
|||||||
static const ErrorCodeMap ErrorMessages;
|
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>
|
template <typename T>
|
||||||
class ThreadsafeObjectLock;
|
class ThreadsafeObjectLock;
|
||||||
|
|
||||||
|
|||||||
@ -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"}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
16
engine/NBCore/src/ErrorsImpl.cpp
Normal file
16
engine/NBCore/src/ErrorsImpl.cpp
Normal 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_)) {}
|
||||||
|
|
||||||
|
}
|
||||||
@ -33,6 +33,6 @@ static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
|||||||
return log.run();
|
return log.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
Loading…
x
Reference in New Issue
Block a user