WIP: Custom Erroring system #1
@ -5,7 +5,6 @@
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include "TypeTraits.hpp"
|
||||
#include <unordered_map>
|
||||
@ -15,103 +14,87 @@ 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 has_type = decltype(T::type);
|
||||
using IsValidException = std::enable_if_t<IsValidException_v<T>, bool>;
|
||||
|
||||
template<class ErrorType = NoneType>
|
||||
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 std::exception {
|
||||
class ErrorBase<NoneType> {
|
||||
public:
|
||||
const unsigned int code;
|
||||
const std::string msg;
|
||||
const std::shared_ptr<const std::exception> trace;
|
||||
const std::string type;
|
||||
const std::shared_ptr<const void> trace;
|
||||
const bool traceIsNBError;
|
||||
|
||||
virtual inline std::string str() const noexcept = 0;
|
||||
|
||||
virtual const char* what() const noexcept override final {
|
||||
const std::string& ret = str();
|
||||
return ret.c_str();
|
||||
}
|
||||
virtual std::string what() const noexcept = 0;
|
||||
virtual std::shared_ptr<ErrorBase> make_shared() const noexcept = 0;
|
||||
|
||||
protected:
|
||||
template <
|
||||
typename TraceType = std::exception,
|
||||
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||
>
|
||||
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(
|
||||
const unsigned int& code_,
|
||||
const std::string& msg_,
|
||||
const std::shared_ptr<TraceType> trace_,
|
||||
const unsigned int& line_,
|
||||
std::string filename_
|
||||
unsigned int code_,
|
||||
std::string msg_,
|
||||
std::string type_,
|
||||
const std::exception* trace_
|
||||
) noexcept :
|
||||
code(code_),
|
||||
msg(msg_),
|
||||
trace{std::dynamic_pointer_cast<std::exception>(trace_)},
|
||||
traceIsNBError( trace_ ? is_detected<has_type, TraceType>::value : false)
|
||||
{
|
||||
static_assert(std::is_base_of<std::exception, TraceType>::value,
|
||||
"`trace_` must be a pointer to a child object of std::exception."
|
||||
);
|
||||
}
|
||||
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>;
|
||||
public:
|
||||
ErrorBase(const ErrorBase&) = default;
|
||||
ErrorBase& operator=(const ErrorBase&) = delete;
|
||||
|
||||
virtual inline std::string str() const noexcept override {
|
||||
std::string ret = msg + nb::NEWLINE;
|
||||
if (trace) {
|
||||
const std::string tabover = " ";
|
||||
std::string trace_msg;
|
||||
if (traceIsNBError) {
|
||||
trace_msg = std::static_pointer_cast<const ErrorBase>(trace)->str();
|
||||
trace_msg = std::string(std::string_view(trace_msg).substr(
|
||||
0, trace_msg.length()-nb::NEWLINE.length()
|
||||
));
|
||||
} else {
|
||||
trace_msg = std::string(trace->what());
|
||||
}
|
||||
ret += indent_strblock(trace_msg, tabover, tabover+"Trace: ")+nb::NEWLINE;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
using Base::traceIsNBError;
|
||||
|
||||
protected:
|
||||
using Base::Base;
|
||||
|
||||
template <
|
||||
typename TraceType = std::exception,
|
||||
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||
>
|
||||
ErrorBase(
|
||||
const unsigned int code_,
|
||||
std::string msg_,
|
||||
const TraceType* trace_=nullptr,
|
||||
unsigned int line_=0,
|
||||
std::string filename_=""
|
||||
) : Base(
|
||||
code_,
|
||||
msg_,
|
||||
trace_ ? std::make_shared<TraceType>(*trace_) : nullptr,
|
||||
line_,
|
||||
filename_
|
||||
) {
|
||||
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."
|
||||
@ -125,83 +108,117 @@ protected:
|
||||
);
|
||||
}
|
||||
|
||||
template <
|
||||
typename TraceType,
|
||||
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||
>
|
||||
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(
|
||||
const unsigned int& code_,
|
||||
const TraceType& trace_,
|
||||
unsigned int line_=0,
|
||||
std::string filename_=""
|
||||
) noexcept : ErrorBase(
|
||||
unsigned int code_,
|
||||
std::string msg_,
|
||||
std::string type_,
|
||||
const T* trace_
|
||||
) : Base(
|
||||
code_,
|
||||
ErrorType::ErrorMessages.at(code_),
|
||||
&trace_,
|
||||
line_,
|
||||
filename_
|
||||
) {}
|
||||
|
||||
template <
|
||||
typename TraceType,
|
||||
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||
>
|
||||
ErrorBase(
|
||||
const std::string& msg_,
|
||||
const TraceType& trace_,
|
||||
unsigned int line_=0,
|
||||
std::string filename_=""
|
||||
) noexcept : ErrorBase(
|
||||
0,
|
||||
msg_,
|
||||
&trace_,
|
||||
line_,
|
||||
filename_
|
||||
) {}
|
||||
|
||||
ErrorBase(
|
||||
const unsigned int& code_,
|
||||
unsigned int line_=0,
|
||||
std::string filename_=""
|
||||
) noexcept : ErrorBase(
|
||||
code_,
|
||||
ErrorType::ErrorMessages.at(code_),
|
||||
static_cast<std::exception*>(nullptr),
|
||||
line_,
|
||||
filename_
|
||||
) {}
|
||||
|
||||
ErrorBase(
|
||||
const std::string& msg_,
|
||||
unsigned int line_=0,
|
||||
std::string filename_=""
|
||||
) noexcept : ErrorBase(
|
||||
0,
|
||||
msg_,
|
||||
static_cast<std::exception*>(nullptr),
|
||||
line_,
|
||||
filename_
|
||||
) {}
|
||||
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 = NoneType>
|
||||
class Error;
|
||||
|
||||
template<>
|
||||
class Error<NoneType>;
|
||||
|
||||
template <class ErrorType>
|
||||
class Error : public ErrorBase<ErrorType> {
|
||||
using Base = ErrorBase<ErrorType>;
|
||||
public:
|
||||
template <typename... Args>
|
||||
Error(Args... args) : Base(args...) {}
|
||||
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::str;
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
@ -210,24 +227,43 @@ public:
|
||||
using Base::type;
|
||||
using Base::ErrorMessages;
|
||||
|
||||
friend ErrorBase<Error>;
|
||||
protected:
|
||||
using Base::Base;
|
||||
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
|
||||
using Base = ErrorBase<Error<NoneType>>;
|
||||
public:
|
||||
using Base::Base;
|
||||
Error() : Base(0) {}
|
||||
|
||||
template <typename... Args>
|
||||
Error(Args... args) : Base(args...) {}
|
||||
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 {
|
||||
GENERAL, UNDEFINED, BADERRORCODE
|
||||
STANDARD, UNDEFINED
|
||||
};
|
||||
|
||||
using Base::str;
|
||||
using Base::what;
|
||||
|
||||
using Base::code;
|
||||
@ -238,6 +274,7 @@ public:
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#endif // _NB_ERROR
|
||||
@ -9,6 +9,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "DataSink.hpp"
|
||||
#include "Errors.hpp"
|
||||
#include "Processes.hpp"
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
#include "TypeTraits.hpp"
|
||||
@ -31,24 +32,21 @@ class LoggerBase
|
||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||
public:
|
||||
bool run() override {
|
||||
if (!type_ptr->isRunning()) {
|
||||
if (!static_cast<LoggerType*>(this)->isRunning()) {
|
||||
this->_running = true;
|
||||
this->_runningThread = std::make_shared<std::thread>([&]{
|
||||
while(type_ptr->isRunning()) {
|
||||
type_ptr->flush();
|
||||
while(static_cast<LoggerType*>(this)->isRunning()) {
|
||||
static_cast<LoggerType*>(this)->flush();
|
||||
}
|
||||
type_ptr->flush();
|
||||
static_cast<LoggerType*>(this)->flush();
|
||||
});
|
||||
}
|
||||
return type_ptr->isRunning();
|
||||
return static_cast<LoggerType*>(this)->isRunning();
|
||||
}
|
||||
protected:
|
||||
LoggerBase() = default;
|
||||
|
||||
StreamType _ostream;
|
||||
|
||||
private:
|
||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||
};
|
||||
|
||||
struct LogEvent{
|
||||
@ -57,6 +55,8 @@ struct LogEvent{
|
||||
const std::string msg;
|
||||
const std::thread::id tid;
|
||||
const uint64_t pid;
|
||||
const std::string file="";
|
||||
const unsigned int line=0;
|
||||
};
|
||||
|
||||
template <typename LT>
|
||||
@ -69,38 +69,73 @@ public:
|
||||
template<typename... ST>
|
||||
DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec<std::ostream, ST...>(streams...).vec) {}
|
||||
|
||||
~DebugLogger() { type_ptr->stop(); }
|
||||
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
|
||||
|
||||
void log(const std::string& msg, const uint8_t& lvl=0x00) {
|
||||
type_ptr->push(LogEvent{
|
||||
void log(
|
||||
std::string msg,
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->push(LogEvent{
|
||||
std::chrono::system_clock::now(),
|
||||
lvl,
|
||||
msg,
|
||||
std::this_thread::get_id(),
|
||||
GetPID(),
|
||||
file,
|
||||
line
|
||||
});
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
void log(char const(&msg) [N], const uint8_t& lvl=0x00) {
|
||||
type_ptr->log(std::string(msg), lvl);
|
||||
void log(
|
||||
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);
|
||||
}
|
||||
|
||||
void log(const std::exception& err, const uint8_t& lvl=0xFF) {
|
||||
type_ptr->log(err.what(), lvl);
|
||||
template <typename T>
|
||||
void log(
|
||||
const ErrorBase<T>& err,
|
||||
uint8_t lvl=0xFF,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->log(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(const U& val, const uint8_t& lvl=0x01) { type_ptr->log(val, lvl); }
|
||||
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(const U& val) { type_ptr->log(val, 0xFF); }
|
||||
void error(
|
||||
U val,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) { static_cast<LoggerType*>(this)->log(val, 0xFF, file, line); }
|
||||
|
||||
protected:
|
||||
std::vector<std::ostream*> _ostream;
|
||||
|
||||
private:
|
||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||
};
|
||||
|
||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||
@ -132,22 +167,103 @@ extern DefaultDebugLogger logger;
|
||||
|
||||
// Taking Charge of Adult ADHD by Russell Barkley
|
||||
|
||||
/* template <typename T=NoneType>
|
||||
struct NB_DEFAULT_LOGGER_THROW;
|
||||
|
||||
template<>
|
||||
struct NB_DEFAULT_LOGGER_THROW<NoneType> {
|
||||
template <typename T>
|
||||
NB_DEFAULT_LOGGER_THROW(T arg) {
|
||||
#ifdef _NB_AUTOLOG
|
||||
logger.error(arg);
|
||||
#endif // _NB_AUTLOG
|
||||
throw arg;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct NB_DEFAULT_LOGGER_THROW {
|
||||
template <typename... Args>
|
||||
NB_DEFAULT_LOGGER_THROW(Args&&... args) {
|
||||
std::shared_ptr<T> error_ptr;
|
||||
#ifdef _NB_AUTOLOG
|
||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||
error_ptr = std::make_shared<T>(args..., __LINE__, __FILE__);
|
||||
#else
|
||||
error_ptr = std::make_shared<T>(args...);
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
logger.error(*error_ptr);
|
||||
#endif // _NB_AUTLOG
|
||||
throw *error_ptr;
|
||||
}
|
||||
}; */
|
||||
|
||||
/* template <typename Arg1=NoneType, typename...Args>
|
||||
void NB_DEFAULT_LOGGER_THROW(Args&& ... args) {
|
||||
std::shared_ptr<Arg1> error_ptr;
|
||||
#ifdef _NB_AUTOLOG
|
||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||
error_ptr = std::make_shared<Arg1>(args..., __LINE__, __FILE__);
|
||||
#else
|
||||
error_ptr = std::make_shared<Arg1>(args...);
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
logger.error(*error_ptr);
|
||||
#endif // _NB_AUTLOG
|
||||
throw *error_ptr;
|
||||
}
|
||||
|
||||
template <typename Arg>
|
||||
void NB_DEFAULT_LOGGER_THROW<NoneType>(const Arg& arg) {
|
||||
#ifdef _NB_AUTOLOG
|
||||
logger.error(arg);
|
||||
#endif // _NB_AUTLOG
|
||||
throw arg;
|
||||
} */
|
||||
|
||||
template <typename T>
|
||||
void NB_DEFAULT_LOGGER_THROW(const T& err, std::string file="", unsigned int line = 0) {
|
||||
#ifdef _NB_AUTOLOG
|
||||
if (file.empty()) {
|
||||
logger.error(err);
|
||||
} else {
|
||||
logger.error(err, file, line);
|
||||
}
|
||||
#endif // _NB_AUTLOG
|
||||
throw err;
|
||||
}
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#ifndef CONSTRUCT_ERROR
|
||||
#ifdef _NB_AUTOLOG
|
||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||
#define CONSTRUCT_ERROR(type, ...) type(# __VA_ARGS__, __LINE__, __FILE__)
|
||||
#ifndef LOG
|
||||
#define LOG(args...) nb::logger.log(args, __FILE__, __LINE__)
|
||||
#endif // LOG
|
||||
#ifndef WARN
|
||||
#define WARN(args...) nb::logger.warn(args, __FILE__, __LINE__)
|
||||
#endif // WARN
|
||||
#ifndef ERROR
|
||||
#define ERROR(args...) nb::logger.error(args, __FILE__, __LINE__)
|
||||
#endif // ERROR
|
||||
#else
|
||||
#define CONSTRUCT_ERROR(type, ...) type(__VA_ARGS__)
|
||||
#ifndef LOG
|
||||
#define LOG(args...) nb::logger.log(args)
|
||||
#endif // LOG
|
||||
#ifndef WARN
|
||||
#define WARN(args...) nb::logger.warn(args)
|
||||
#endif // WARN
|
||||
#ifndef ERROR
|
||||
#define ERROR(args...) nb::logger.error(args)
|
||||
#endif // ERROR
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
#endif // CONSTRUCT_ERROR
|
||||
#endif // _NB_AUTOLOG
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef _NB_AUTOLOG
|
||||
#define THROW(type, ...) {auto x = CONSTRUCT_ERROR(type, ## __VA_ARGS__); nb::logger.error(x); throw x;}
|
||||
#define THROW(args...) ERROR(args); throw args
|
||||
#else
|
||||
#define THROW(type, ...) throw CONSTRUCT_ERROR(type, __VA_ARGS__)
|
||||
#endif // _NB_AUTOLOG
|
||||
#define THROW(args...) throw args
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
#endif // THROW
|
||||
|
||||
#endif // _NB_LOGGER
|
||||
@ -4,7 +4,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <string_view>
|
||||
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
@ -19,53 +19,17 @@ namespace nb {
|
||||
const std::wstring WNEWLINE = L"\n";
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
template <typename... T>
|
||||
using StringConvertible = ValidConversion<std::string, T...>;
|
||||
const std::string TABOVER = " ";
|
||||
|
||||
template <typename... T>
|
||||
using WStringConvertible = ValidConversion<std::wstring, T...>;
|
||||
// std::wstring str_to_wstr(std::string in);
|
||||
|
||||
template <typename... T>
|
||||
constexpr bool StringConvertible_v = StringConvertible<T...>::value;
|
||||
|
||||
template <typename... T>
|
||||
constexpr bool WStringConvertible_v = WStringConvertible<T...>::value;
|
||||
|
||||
template <typename... T>
|
||||
using StringConvertible_to = typename StringConvertible<T...>::to;
|
||||
|
||||
template <typename... T>
|
||||
using WStringConvertible_to = typename WStringConvertible<T...>::to;
|
||||
|
||||
template <typename... T>
|
||||
using StringConvertible_from = typename StringConvertible<T...>::from;
|
||||
|
||||
template <typename... T>
|
||||
using WStringConvertible_from = typename WStringConvertible<T...>::from;
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<StringConvertible_v<T>, std::wstring> str_to_wstr(T in) {
|
||||
std::string str(in);
|
||||
std::wstring ret(str.begin(), str.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<WStringConvertible_v<T>, std::string> wstr_to_str(T in) {
|
||||
std::wstring wstr(in);
|
||||
std::size_t wstrlen = wstr.length();
|
||||
char* c_str= new char[wstrlen];
|
||||
std::wcstombs(c_str, wstr.c_str(), wstrlen);
|
||||
std::string ret(c_str, wstrlen);
|
||||
delete[] c_str;
|
||||
return ret;
|
||||
}
|
||||
// std::string wstr_to_str(std::wstring in);
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args);
|
||||
void stream(const Stream& s, Args&&... args);
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args) {
|
||||
void stream(const Stream& s, Args&&... args) {
|
||||
(s << ... << args);
|
||||
}
|
||||
|
||||
@ -75,68 +39,41 @@ void term(Args&&... args) { stream(std::cout, args..., nb::NEWLINE); }
|
||||
template<typename... Args>
|
||||
void wterm(Args&&... args) { stream(std::wcout, args..., nb::WNEWLINE); }
|
||||
|
||||
template <typename T = std::string, typename A, typename B, typename C>
|
||||
ValidConversion_to<T, A, B, C> find_and_replace(
|
||||
A original,
|
||||
B find,
|
||||
C replace
|
||||
template <typename T = char>
|
||||
std::basic_string<T> find_and_replace(
|
||||
ExplicitType_t<std::basic_string<T>> original,
|
||||
ExplicitType_t<std::basic_string_view<T>> find,
|
||||
ExplicitType_t<std::basic_string_view<T>> replace
|
||||
) {
|
||||
const T& find_t = T(find);
|
||||
const T& replace_t = T(replace);
|
||||
using StringType = std::basic_string<T>;
|
||||
|
||||
T ret(original);
|
||||
StringType ret(original);
|
||||
|
||||
std::size_t find_len = find_t.length();
|
||||
std::size_t replace_len = replace_t.length();
|
||||
std::size_t find_len = find.length();
|
||||
std::size_t replace_len = replace.length();
|
||||
std::size_t currpos = 0;
|
||||
while(true) {
|
||||
currpos = ret.find(find_t, currpos);
|
||||
if (currpos == T::npos) {
|
||||
currpos = ret.find(find, currpos);
|
||||
if (currpos == StringType::npos) {
|
||||
break;
|
||||
}
|
||||
ret = ret.erase(currpos, find_len);
|
||||
ret = ret.insert(currpos, replace_t);
|
||||
ret = ret.insert(currpos, replace);
|
||||
currpos += replace_len;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename A, typename B, typename C>
|
||||
StringConvertible_to<A, B, C> indent_strblock(
|
||||
A block,
|
||||
B prepend,
|
||||
C topIndent
|
||||
) {
|
||||
return std::string(topIndent) + find_and_replace<std::string>(
|
||||
std::string(block),
|
||||
nb::NEWLINE,
|
||||
nb::NEWLINE + std::string(prepend)
|
||||
);
|
||||
}
|
||||
std::string indent_strblock(
|
||||
std::string block,
|
||||
std::string prepend,
|
||||
std::string topIndent
|
||||
);
|
||||
|
||||
template <typename A, typename B>
|
||||
StringConvertible_to<A, B> indent_strblock(
|
||||
A block,
|
||||
B prepend
|
||||
) {
|
||||
return indent_strblock<A, B, B>(block, prepend, prepend);
|
||||
}
|
||||
|
||||
/* template<typename T>
|
||||
T swap_endian(const T& val) {
|
||||
T ret;
|
||||
const int size = sizeof(T);
|
||||
auto retLoc = static_cast<void*>(&ret);
|
||||
auto valLoc = static_cast<const void*>(&val);
|
||||
|
||||
for (int i = 0; i < size; ++i) {
|
||||
memcpy(retLoc+i, valLoc+(size-i-1), 1);
|
||||
}
|
||||
return ret;
|
||||
} */
|
||||
|
||||
|
||||
// using ByteVector = std::vector<uint8_t>;
|
||||
std::string indent_strblock(
|
||||
std::string block,
|
||||
std::string prepend
|
||||
);
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_CORE_TYPES
|
||||
@ -69,6 +69,24 @@ using ValidConversion_to = typename ValidConversion<Types...>::to;
|
||||
template<typename To, typename From>
|
||||
using ValidConversion_from = typename ValidConversion<To, From>::from;
|
||||
|
||||
template <typename T>
|
||||
struct ExplicitType { using type=T; };
|
||||
|
||||
template <typename T>
|
||||
using ExplicitType_t = typename ExplicitType<T>::type;
|
||||
|
||||
template <typename T>
|
||||
T* NULLPTR = static_cast<T*>(nullptr);
|
||||
|
||||
template <typename Func, typename T>
|
||||
struct RunAndOutput {
|
||||
using type = T;
|
||||
T value;
|
||||
RunAndOutput(Func func, T val) : value(val) {
|
||||
func();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Func, std::size_t N=0, typename... Pack>
|
||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>&, Func) {}
|
||||
|
||||
@ -3,12 +3,11 @@
|
||||
|
||||
namespace nb {
|
||||
|
||||
const std::string Error<nb::NoneType>::type = "nb::Error";
|
||||
const std::string Error<>::type = "nb::Error";
|
||||
|
||||
const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = {
|
||||
{Error::Codes::GENERAL, "General std::exception."},
|
||||
{Error::Codes::UNDEFINED, "Undefined / general error."},
|
||||
{Error::Codes::BADERRORCODE, "Unrecognized error code."}
|
||||
{Error::Codes::STANDARD, "std::exception"},
|
||||
{Error::Codes::UNDEFINED, "Error"}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -10,11 +10,21 @@ nb::DefaultDebugLogger logger(std::cout);
|
||||
#endif // _NB_NO_LOGGER
|
||||
|
||||
bool nb::DefaultDebugLogger::process(const LogEvent& msg) {
|
||||
constexpr size_t level_field_width = 3;
|
||||
constexpr size_t msg_prompt_length = level_field_width+6;
|
||||
for (const auto os : this->_ostream) {
|
||||
*os << "[";
|
||||
os->width(level_field_width);
|
||||
(*os << std::to_string(msg.lvl)).width(level_field_width);
|
||||
*os << "] :: ";
|
||||
std::string fileloc="";
|
||||
if (!msg.file.empty()) {
|
||||
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
|
||||
}
|
||||
*os << indent_strblock(
|
||||
msg.msg,
|
||||
std::string(10, ' '),
|
||||
"[ "+std::to_string(msg.lvl)+" ] : "
|
||||
fileloc + msg.msg,
|
||||
std::string(15 - msg_prompt_length, ' '),
|
||||
""
|
||||
) << nb::NEWLINE;
|
||||
}
|
||||
return true;
|
||||
|
||||
@ -1 +1,29 @@
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
/* std::string nb::wstr_to_str(std::wstring in) {
|
||||
std::size_t wstrlen = in.length();
|
||||
char* c_str= new char[wstrlen];
|
||||
std::wcstombs(c_str, in.c_str(), wstrlen);
|
||||
std::string ret(c_str, wstrlen);
|
||||
delete[] c_str;
|
||||
return ret;
|
||||
} */
|
||||
|
||||
std::string nb::indent_strblock(
|
||||
std::string block,
|
||||
std::string prepend,
|
||||
std::string topIndent
|
||||
) {
|
||||
return topIndent + nb::find_and_replace(
|
||||
block,
|
||||
nb::NEWLINE,
|
||||
nb::NEWLINE + prepend
|
||||
);
|
||||
}
|
||||
|
||||
std::string nb::indent_strblock(
|
||||
std::string block,
|
||||
std::string prepend
|
||||
) {
|
||||
return nb::indent_strblock(block, prepend, prepend);
|
||||
}
|
||||
|
||||
@ -35,5 +35,5 @@ const ErrorCodeMap TestError::ErrorMessages{
|
||||
|
||||
TEST(ErrorTest, Test) {
|
||||
auto err = TestError(0, TestError(1, TestError(2, TestError(3, TestError(2)))));
|
||||
ASSERT_STREQ(err.what(), "Hey!\n Trace: How\n Trace: You\n Trace: Doin\n Trace: You\n");
|
||||
ASSERT_STREQ(err.what().c_str(), "Hey!\n Trace: How\n Trace: You\n Trace: Doin\n Trace: You");
|
||||
}
|
||||
@ -3,14 +3,43 @@
|
||||
#include "Errors.hpp"
|
||||
#include "Logger.hpp"
|
||||
|
||||
class TestError : public nb::Error<TestError> {
|
||||
using Base = Error<TestError>;
|
||||
public:
|
||||
using Base::Base;
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
|
||||
enum Codes : unsigned int {
|
||||
A, B, C, D
|
||||
};
|
||||
|
||||
static const std::string type;
|
||||
static const nb::ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
const std::string TestError::type="TestError";
|
||||
const nb::ErrorCodeMap TestError::ErrorMessages{
|
||||
{TestError::Codes::A, "Hey!"},
|
||||
{TestError::Codes::B, "How"},
|
||||
{TestError::Codes::C, "You"},
|
||||
{TestError::Codes::D, "Doin"}
|
||||
};
|
||||
|
||||
int main() {
|
||||
nb::logger.log("Whoop!");
|
||||
try {
|
||||
THROW(nb::Error, "Hiii!");
|
||||
} catch (nb::Error<nb::NoneType> e){
|
||||
nb::logger.log("Winner winner chicken dinner!");
|
||||
THROW(nb::Error(1));
|
||||
} catch (nb::Error<> e){
|
||||
nb::logger.log("nb::Error was thrown!");
|
||||
}
|
||||
|
||||
try {
|
||||
THROW(std::exception());
|
||||
} catch (std::exception e){
|
||||
nb::logger.log("std::exception was thrown!");
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@ -43,38 +43,38 @@ TEST(UtilsTest, TestFindAndReplace) {
|
||||
|
||||
TEST(UtilsTest, TestFindAndReplaceWstr) {
|
||||
ASSERT_STREQ(
|
||||
find_and_replace<std::wstring>(L"Jeff", L"e", L"efe").c_str(),
|
||||
find_and_replace<wchar_t>(L"Jeff", L"e", L"efe").c_str(),
|
||||
L"Jefeff"
|
||||
);
|
||||
|
||||
std::wstring tmp = find_and_replace<std::wstring>(L"Naif", L"a", L"afa");
|
||||
std::wstring tmp = find_and_replace<wchar_t>(L"Naif", L"a", L"afa");
|
||||
ASSERT_STREQ(
|
||||
find_and_replace<std::wstring>(tmp, L"i", L"ifi").c_str(),
|
||||
find_and_replace<wchar_t>(tmp, L"i", L"ifi").c_str(),
|
||||
L"Nafaifif"
|
||||
);
|
||||
|
||||
tmp = find_and_replace<std::wstring>(L"aeiou", L"a", L"afa");
|
||||
tmp = find_and_replace<std::wstring>(tmp, L"e", L"efe");
|
||||
tmp = find_and_replace<std::wstring>(tmp, L"i", L"ifi");
|
||||
tmp = find_and_replace<std::wstring>(tmp, L"o", L"ofo");
|
||||
tmp = find_and_replace<std::wstring>(tmp, L"u", L"ufu");
|
||||
tmp = find_and_replace<wchar_t>(L"aeiou", L"a", L"afa");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"e", L"efe");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"i", L"ifi");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"o", L"ofo");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"u", L"ufu");
|
||||
ASSERT_STREQ(
|
||||
tmp.c_str(),
|
||||
L"afaefeifiofoufu"
|
||||
);
|
||||
|
||||
tmp = find_and_replace<std::wstring>(tmp, L"afa", L"a");
|
||||
tmp = find_and_replace<std::wstring>(tmp, L"efe", L"e");
|
||||
tmp = find_and_replace<std::wstring>(tmp, L"ifi", L"i");
|
||||
tmp = find_and_replace<std::wstring>(tmp, L"ofo", L"o");
|
||||
tmp = find_and_replace<std::wstring>(tmp, L"ufu", L"u");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"afa", L"a");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"efe", L"e");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"ifi", L"i");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"ofo", L"o");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"ufu", L"u");
|
||||
ASSERT_STREQ(
|
||||
tmp.c_str(),
|
||||
L"aeiou"
|
||||
);
|
||||
}
|
||||
|
||||
TEST(UtilsTest, TestWstrToStr) {
|
||||
/* TEST(UtilsTest, TestWstrToStr) {
|
||||
ASSERT_STREQ(
|
||||
nb::wstr_to_str(L"Hi!").c_str(),
|
||||
"Hi!"
|
||||
@ -91,9 +91,9 @@ TEST(UtilsTest, TestWstrToStr) {
|
||||
nb::wstr_to_str(L"Naif\ttalks\r\ra\t\nlot").c_str(),
|
||||
"Naif\ttalks\r\ra\t\nlot"
|
||||
);
|
||||
}
|
||||
} */
|
||||
|
||||
TEST(UtilsTest, TestStrToWstr) {
|
||||
/* TEST(UtilsTest, TestStrToWstr) {
|
||||
ASSERT_STREQ(
|
||||
nb::str_to_wstr("Hi!").c_str(),
|
||||
L"Hi!"
|
||||
@ -110,7 +110,7 @@ TEST(UtilsTest, TestStrToWstr) {
|
||||
nb::str_to_wstr("Naif\ttalks\r\ra\t\nlot").c_str(),
|
||||
L"Naif\ttalks\r\ra\t\nlot"
|
||||
);
|
||||
}
|
||||
} */
|
||||
|
||||
struct A { bool x(); };
|
||||
struct B { bool y(); };
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user