From 58121bdf2818c113af3441a11557663c15e0b603 Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Sat, 2 May 2026 01:50:53 -0500 Subject: [PATCH] Got logging and errors basically all working the way I want --- engine/NBCore/Errors.hpp | 331 ++++++++++++++++------------- engine/NBCore/Logger.hpp | 168 ++++++++++++--- engine/NBCore/StringUtils.hpp | 117 +++------- engine/NBCore/TypeTraits.hpp | 18 ++ engine/NBCore/src/Errors.cpp | 7 +- engine/NBCore/src/Logger.cpp | 16 +- engine/NBCore/src/StringUtils.cpp | 28 +++ engine/NBCore/tests/testErrors.cpp | 2 +- engine/NBCore/tests/testLogger.cpp | 35 ++- engine/NBCore/tests/testUtils.cpp | 34 +-- 10 files changed, 465 insertions(+), 291 deletions(-) diff --git a/engine/NBCore/Errors.hpp b/engine/NBCore/Errors.hpp index 35d94db..2e38434 100644 --- a/engine/NBCore/Errors.hpp +++ b/engine/NBCore/Errors.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include "TypeTraits.hpp" #include @@ -15,103 +14,87 @@ namespace nb { typedef std::unordered_map ErrorCodeMap; +template +constexpr bool IsValidException_v = std::is_base_of_v; template -using has_type = decltype(T::type); +using IsValidException = std::enable_if_t, bool>; -template +template class ErrorBase; +template +class Error; + +template +constexpr bool IsValidNBError_v = std::is_base_of_v, T>; +template +using IsValidNBError = std::enable_if_t, bool>; + + template<> -class ErrorBase : public std::exception { +class ErrorBase { public: const unsigned int code; const std::string msg; - const std::shared_ptr trace; + const std::string type; + const std::shared_ptr 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 make_shared() const noexcept = 0; + protected: - template < - typename TraceType = std::exception, - std::enable_if_t, bool> = true - > + template + ErrorBase(const ErrorBase& err) : ErrorBase(std::move(err)) {} + + template + ErrorBase(ErrorBase&& err) : ErrorBase( + err.code, + err.msg, + err.type, + (err.traceIsNBError) + ? static_cast(err.trace.get()) + : static_cast(err.trace.get()) + ) {} + ErrorBase( - const unsigned int& code_, - const std::string& msg_, - const std::shared_ptr 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(trace_)}, - traceIsNBError( trace_ ? is_detected::value : false) - { - static_assert(std::is_base_of::value, - "`trace_` must be a pointer to a child object of std::exception." - ); - } + type(type_), + trace{ trace_ ? + std::static_pointer_cast( + std::make_shared>(*trace_)) + : nullptr, + }, + traceIsNBError(false) + {} + + template, 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 ErrorBase : public ErrorBase { +private: using Base = ErrorBase; -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(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, 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(*trace_) : nullptr, - line_, - filename_ - ) { + void inline check_asserts() { static_assert(std::is_same, decltype(ErrorType::ErrorMessages)>::value, "const std::unordered_map ErrorMessages must be " "a class member." @@ -125,83 +108,117 @@ protected: ); } - template < - typename TraceType, - std::enable_if_t, bool> = true - > - ErrorBase( - const unsigned int& code_, - const TraceType& trace_, - unsigned int line_=0, - std::string filename_="" - ) noexcept : ErrorBase( - code_, - ErrorType::ErrorMessages.at(code_), - &trace_, - line_, - filename_ - ) {} +public: + template + ErrorBase(const ErrorBase& cpy) : Base(cpy) { check_asserts(); } - template < - typename TraceType, - std::enable_if_t, bool> = true - > + virtual std::shared_ptr make_shared() const noexcept override { + return std::static_pointer_cast( + std::make_shared(*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(trace)->what(); + } else { + trace_msg = std::string(std::static_pointer_cast(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; + +protected: + using Base::Base; + + template ErrorBase( - const std::string& msg_, - const TraceType& trace_, - unsigned int line_=0, - std::string filename_="" - ) noexcept : ErrorBase( - 0, + unsigned int code_, + std::string msg_, + std::string type_, + const T* trace_ + ) : Base( + code_, 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(nullptr), - line_, - filename_ - ) {} - - ErrorBase( - const std::string& msg_, - unsigned int line_=0, - std::string filename_="" - ) noexcept : ErrorBase( - 0, - msg_, - static_cast(nullptr), - line_, - filename_ - ) {} + type_, + trace_ + ) { check_asserts(); } }; template const std::string ErrorBase::type = ErrorType::type; template const ErrorCodeMap ErrorBase::ErrorMessages = ErrorType::ErrorMessages; -template -class Error; - -template<> -class Error; template class Error : public ErrorBase { using Base = ErrorBase; -public: - template - Error(Args... args) : Base(args...) {} +public: + template + Error( + unsigned int code_, + const T& trace_ + ) noexcept : Base( + code_, + ErrorType::ErrorMessages.at(code_), + ErrorType::type, + &trace_ + ) {} + + template + Error( + std::string msg_, + const ErrorBase& 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 + ) {} + + Error(std::string msg_) noexcept : Base( + 0, + msg_, + ErrorType::type, + NULLPTR + ) {} - 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; +protected: + using Base::Base; + }; + template<> class Error : public ErrorBase> { using Base = ErrorBase>; public: using Base::Base; - Error() : Base(0) {} - template - Error(Args... args) : Base(args...) {} + Error(unsigned int code_=1) noexcept : Base( + code_, + ErrorMessages.at(code_), + type, + NULLPTR + ) {} + + 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 + ) {} 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 \ No newline at end of file diff --git a/engine/NBCore/Logger.hpp b/engine/NBCore/Logger.hpp index 2c332bf..e0f1f19 100644 --- a/engine/NBCore/Logger.hpp +++ b/engine/NBCore/Logger.hpp @@ -9,6 +9,7 @@ #include #include "DataSink.hpp" +#include "Errors.hpp" #include "Processes.hpp" #include "ThreadSafeQueue.hpp" #include "TypeTraits.hpp" @@ -31,24 +32,21 @@ class LoggerBase using Base = MultithreadedDataProcessor; public: bool run() override { - if (!type_ptr->isRunning()) { + if (!static_cast(this)->isRunning()) { this->_running = true; this->_runningThread = std::make_shared([&]{ - while(type_ptr->isRunning()) { - type_ptr->flush(); + while(static_cast(this)->isRunning()) { + static_cast(this)->flush(); } - type_ptr->flush(); + static_cast(this)->flush(); }); } - return type_ptr->isRunning(); + return static_cast(this)->isRunning(); } protected: LoggerBase() = default; StreamType _ostream; - -private: - LoggerType* type_ptr = static_cast(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 @@ -69,38 +69,73 @@ public: template DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec(streams...).vec) {} - ~DebugLogger() { type_ptr->stop(); } + ~DebugLogger() { static_cast(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(this)->push(LogEvent{ std::chrono::system_clock::now(), lvl, msg, std::this_thread::get_id(), GetPID(), + file, + line }); } template - 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(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 + void log( + const ErrorBase& err, + uint8_t lvl=0xFF, + std::string file="", + unsigned int line=0 + ) { + static_cast(this)->log(err.what(), lvl, file, line); + } + + template + std::enable_if_t, void> log( + const T& err, + uint8_t lvl=0xFF, + std::string file="", + unsigned int line=0 + ) { + static_cast(this)->log(std::string(err.what()), lvl, file, line); } template - 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(this)->log(val, lvl, file, line); } template - void error(const U& val) { type_ptr->log(val, 0xFF); } + void error( + U val, + std::string file="", + unsigned int line=0 + ) { static_cast(this)->log(val, 0xFF, file, line); } protected: std::vector _ostream; -private: - LoggerType* type_ptr = static_cast(this); }; class DefaultDebugLogger : public DebugLogger { @@ -132,22 +167,103 @@ extern DefaultDebugLogger logger; // Taking Charge of Adult ADHD by Russell Barkley +/* template +struct NB_DEFAULT_LOGGER_THROW; + +template<> +struct NB_DEFAULT_LOGGER_THROW { + template + NB_DEFAULT_LOGGER_THROW(T arg) { + #ifdef _NB_AUTOLOG + logger.error(arg); + #endif // _NB_AUTLOG + throw arg; + } +}; + +template +struct NB_DEFAULT_LOGGER_THROW { + template + NB_DEFAULT_LOGGER_THROW(Args&&... args) { + std::shared_ptr error_ptr; + #ifdef _NB_AUTOLOG + #ifdef _NB_CODE_ERROR_LOCATIONS + error_ptr = std::make_shared(args..., __LINE__, __FILE__); + #else + error_ptr = std::make_shared(args...); + #endif // _NB_CODE_ERROR_LOCATIONS + logger.error(*error_ptr); + #endif // _NB_AUTLOG + throw *error_ptr; + } +}; */ + +/* template +void NB_DEFAULT_LOGGER_THROW(Args&& ... args) { + std::shared_ptr error_ptr; + #ifdef _NB_AUTOLOG + #ifdef _NB_CODE_ERROR_LOCATIONS + error_ptr = std::make_shared(args..., __LINE__, __FILE__); + #else + error_ptr = std::make_shared(args...); + #endif // _NB_CODE_ERROR_LOCATIONS + logger.error(*error_ptr); + #endif // _NB_AUTLOG + throw *error_ptr; +} + +template +void NB_DEFAULT_LOGGER_THROW(const Arg& arg) { + #ifdef _NB_AUTOLOG + logger.error(arg); + #endif // _NB_AUTLOG + throw arg; +} */ + +template +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 \ No newline at end of file diff --git a/engine/NBCore/StringUtils.hpp b/engine/NBCore/StringUtils.hpp index a8cab98..9b709e1 100644 --- a/engine/NBCore/StringUtils.hpp +++ b/engine/NBCore/StringUtils.hpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include "TypeTraits.hpp" @@ -19,53 +19,17 @@ namespace nb { const std::wstring WNEWLINE = L"\n"; #endif // _NB_TARGET_LINUX -template -using StringConvertible = ValidConversion; +const std::string TABOVER = " "; -template -using WStringConvertible = ValidConversion; +// std::wstring str_to_wstr(std::string in); -template -constexpr bool StringConvertible_v = StringConvertible::value; - -template -constexpr bool WStringConvertible_v = WStringConvertible::value; - -template -using StringConvertible_to = typename StringConvertible::to; - -template -using WStringConvertible_to = typename WStringConvertible::to; - -template -using StringConvertible_from = typename StringConvertible::from; - -template -using WStringConvertible_from = typename WStringConvertible::from; - -template -std::enable_if_t, std::wstring> str_to_wstr(T in) { - std::string str(in); - std::wstring ret(str.begin(), str.end()); - return ret; -} - -template -std::enable_if_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 -void stream(Stream& s, Args&&... args); +void stream(const Stream& s, Args&&... args); template -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 void wterm(Args&&... args) { stream(std::wcout, args..., nb::WNEWLINE); } -template -ValidConversion_to find_and_replace( - A original, - B find, - C replace +template +std::basic_string find_and_replace( + ExplicitType_t> original, + ExplicitType_t> find, + ExplicitType_t> replace ) { - const T& find_t = T(find); - const T& replace_t = T(replace); + using StringType = std::basic_string; - 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 -StringConvertible_to indent_strblock( - A block, - B prepend, - C topIndent -) { - return std::string(topIndent) + find_and_replace( - std::string(block), - nb::NEWLINE, - nb::NEWLINE + std::string(prepend) - ); -} +std::string indent_strblock( + std::string block, + std::string prepend, + std::string topIndent +); -template -StringConvertible_to indent_strblock( - A block, - B prepend -) { - return indent_strblock(block, prepend, prepend); -} - -/* template -T swap_endian(const T& val) { - T ret; - const int size = sizeof(T); - auto retLoc = static_cast(&ret); - auto valLoc = static_cast(&val); - - for (int i = 0; i < size; ++i) { - memcpy(retLoc+i, valLoc+(size-i-1), 1); - } - return ret; -} */ - - -// using ByteVector = std::vector; +std::string indent_strblock( + std::string block, + std::string prepend +); } // namespace nb #endif // _NB_CORE_TYPES \ No newline at end of file diff --git a/engine/NBCore/TypeTraits.hpp b/engine/NBCore/TypeTraits.hpp index f92df9a..fb990fe 100644 --- a/engine/NBCore/TypeTraits.hpp +++ b/engine/NBCore/TypeTraits.hpp @@ -69,6 +69,24 @@ using ValidConversion_to = typename ValidConversion::to; template using ValidConversion_from = typename ValidConversion::from; +template +struct ExplicitType { using type=T; }; + +template +using ExplicitType_t = typename ExplicitType::type; + +template +T* NULLPTR = static_cast(nullptr); + +template +struct RunAndOutput { + using type = T; + T value; + RunAndOutput(Func func, T val) : value(val) { + func(); + } +}; + template inline typename std::enable_if::type ForEach(std::tuple&, Func) {} diff --git a/engine/NBCore/src/Errors.cpp b/engine/NBCore/src/Errors.cpp index 43b6227..e5b22fa 100644 --- a/engine/NBCore/src/Errors.cpp +++ b/engine/NBCore/src/Errors.cpp @@ -3,12 +3,11 @@ namespace nb { -const std::string Error::type = "nb::Error"; +const std::string Error<>::type = "nb::Error"; const ErrorCodeMap Error::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"} }; } diff --git a/engine/NBCore/src/Logger.cpp b/engine/NBCore/src/Logger.cpp index b9353a4..795136d 100644 --- a/engine/NBCore/src/Logger.cpp +++ b/engine/NBCore/src/Logger.cpp @@ -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; diff --git a/engine/NBCore/src/StringUtils.cpp b/engine/NBCore/src/StringUtils.cpp index 44f97f5..1946c3c 100644 --- a/engine/NBCore/src/StringUtils.cpp +++ b/engine/NBCore/src/StringUtils.cpp @@ -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); +} diff --git a/engine/NBCore/tests/testErrors.cpp b/engine/NBCore/tests/testErrors.cpp index 1e919aa..0c7be49 100644 --- a/engine/NBCore/tests/testErrors.cpp +++ b/engine/NBCore/tests/testErrors.cpp @@ -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"); } \ No newline at end of file diff --git a/engine/NBCore/tests/testLogger.cpp b/engine/NBCore/tests/testLogger.cpp index da6fb99..451a01f 100644 --- a/engine/NBCore/tests/testLogger.cpp +++ b/engine/NBCore/tests/testLogger.cpp @@ -3,14 +3,43 @@ #include "Errors.hpp" #include "Logger.hpp" +class TestError : public nb::Error { + using Base = Error; +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 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; diff --git a/engine/NBCore/tests/testUtils.cpp b/engine/NBCore/tests/testUtils.cpp index 24ca054..a726369 100644 --- a/engine/NBCore/tests/testUtils.cpp +++ b/engine/NBCore/tests/testUtils.cpp @@ -43,38 +43,38 @@ TEST(UtilsTest, TestFindAndReplace) { TEST(UtilsTest, TestFindAndReplaceWstr) { ASSERT_STREQ( - find_and_replace(L"Jeff", L"e", L"efe").c_str(), + find_and_replace(L"Jeff", L"e", L"efe").c_str(), L"Jefeff" ); - std::wstring tmp = find_and_replace(L"Naif", L"a", L"afa"); + std::wstring tmp = find_and_replace(L"Naif", L"a", L"afa"); ASSERT_STREQ( - find_and_replace(tmp, L"i", L"ifi").c_str(), + find_and_replace(tmp, L"i", L"ifi").c_str(), L"Nafaifif" ); - tmp = find_and_replace(L"aeiou", L"a", L"afa"); - tmp = find_and_replace(tmp, L"e", L"efe"); - tmp = find_and_replace(tmp, L"i", L"ifi"); - tmp = find_and_replace(tmp, L"o", L"ofo"); - tmp = find_and_replace(tmp, L"u", L"ufu"); + tmp = find_and_replace(L"aeiou", L"a", L"afa"); + tmp = find_and_replace(tmp, L"e", L"efe"); + tmp = find_and_replace(tmp, L"i", L"ifi"); + tmp = find_and_replace(tmp, L"o", L"ofo"); + tmp = find_and_replace(tmp, L"u", L"ufu"); ASSERT_STREQ( tmp.c_str(), L"afaefeifiofoufu" ); - tmp = find_and_replace(tmp, L"afa", L"a"); - tmp = find_and_replace(tmp, L"efe", L"e"); - tmp = find_and_replace(tmp, L"ifi", L"i"); - tmp = find_and_replace(tmp, L"ofo", L"o"); - tmp = find_and_replace(tmp, L"ufu", L"u"); + tmp = find_and_replace(tmp, L"afa", L"a"); + tmp = find_and_replace(tmp, L"efe", L"e"); + tmp = find_and_replace(tmp, L"ifi", L"i"); + tmp = find_and_replace(tmp, L"ofo", L"o"); + tmp = find_and_replace(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(); };