From 85241b4aa53e77a7400225457d1c43ec2b8f1cc0 Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Sun, 19 Apr 2026 19:19:47 -0500 Subject: [PATCH] Error logging almost working! --- CMakeLists.txt | 1 + engine/NBCore/CMakeLists.txt | 2 +- engine/NBCore/DataSink.hpp | 7 +++-- engine/NBCore/Errors.hpp | 7 +++-- engine/NBCore/Logger.hpp | 48 ++++++++++++++---------------- engine/NBCore/src/Logger.cpp | 14 +++++++++ engine/NBCore/tests/CMakeLists.txt | 8 +++++ engine/NBCore/tests/testLogger.cpp | 17 +++++++++++ 8 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 engine/NBCore/tests/testLogger.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 65af2c9..9473a5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ endif() if (NB_LOGGING) message(STATUS "Building with automatic logging") add_compile_definitions(_NB_AUTOLOG) + add_compile_definitions(_NB_CODE_ERROR_LOCATIONS) endif() if (NB_TARGET_WINDOWS) diff --git a/engine/NBCore/CMakeLists.txt b/engine/NBCore/CMakeLists.txt index 9f67cd0..ee3a13b 100644 --- a/engine/NBCore/CMakeLists.txt +++ b/engine/NBCore/CMakeLists.txt @@ -1,6 +1,6 @@ toAbsolutePath(NB_CORE_SOURCE ./src/Errors.cpp -# ./src/Logger.cpp + ./src/Logger.cpp ./src/Processes.cpp ./src/StringUtils.cpp ./src/Utils.cpp diff --git a/engine/NBCore/DataSink.hpp b/engine/NBCore/DataSink.hpp index 3aa0c50..d3c5d25 100644 --- a/engine/NBCore/DataSink.hpp +++ b/engine/NBCore/DataSink.hpp @@ -2,6 +2,7 @@ #ifndef _NB_DATASINK #define _NB_DATASINK +#include #include #include "ThreadSafeQueue.hpp" @@ -34,9 +35,9 @@ class BufferedDataProcessor : public DataSink { public: using Base::Base; - bool stop() noexcept { return type_ptr->stop(); } - bool run() { return type_ptr->run(); } - bool in(const DataType& val) { return type_ptr->in(val); } + virtual bool stop() noexcept override { return type_ptr->stop(); } + virtual bool run() override { return type_ptr->run(); } + virtual bool in(const DataType& val) override { return type_ptr->in(val); } protected: unsigned int count() const { diff --git a/engine/NBCore/Errors.hpp b/engine/NBCore/Errors.hpp index d36f749..35d94db 100644 --- a/engine/NBCore/Errors.hpp +++ b/engine/NBCore/Errors.hpp @@ -178,7 +178,7 @@ protected: ) noexcept : ErrorBase( 0, msg_, - nullptr, + static_cast(nullptr), line_, filename_ ) {} @@ -199,7 +199,7 @@ class Error : public ErrorBase { using Base = ErrorBase; public: template - Error(Args... T) : Base(T...) {} + Error(Args... args) : Base(args...) {} using Base::str; using Base::what; @@ -219,6 +219,9 @@ class Error : public ErrorBase> { public: using Base::Base; Error() : Base(0) {} + + template + Error(Args... args) : Base(args...) {} enum Codes : unsigned int { GENERAL, UNDEFINED, BADERRORCODE diff --git a/engine/NBCore/Logger.hpp b/engine/NBCore/Logger.hpp index 40919f6..2c332bf 100644 --- a/engine/NBCore/Logger.hpp +++ b/engine/NBCore/Logger.hpp @@ -8,22 +8,6 @@ #include #include -#ifndef THROW_WITH_INFO - #ifdef CODE_ERROR_LOCATIONS - #define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__) - #else - #define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__) - #endif // CODE_ERROR_LOCATIONS -#endif // THROW_WITH_INFO - -#ifndef THROW - #ifdef LOGGING - #define THROW_WTIH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__) - #else - #define THROW(...) THROW_WITH_INFO(__VA_ARGS__) - #endif // LOGGING -#endif // THROW - #include "DataSink.hpp" #include "Processes.hpp" #include "ThreadSafeQueue.hpp" @@ -87,7 +71,7 @@ public: ~DebugLogger() { type_ptr->stop(); } - void log(const std::string& msg, const uint8_t& lvl=0xFF) { + void log(const std::string& msg, const uint8_t& lvl=0x00) { type_ptr->push(LogEvent{ std::chrono::system_clock::now(), lvl, @@ -98,12 +82,12 @@ public: } template - void log(char const(&msg) [N], const uint8_t& lvl=0xFF) { + void log(char const(&msg) [N], const uint8_t& lvl=0x00) { type_ptr->log(std::string(msg), lvl); } void log(const std::exception& err, const uint8_t& lvl=0xFF) { - type_ptr->log(prepend_strblock(err.what(), " "), lvl); + type_ptr->log(err.what(), lvl); } template @@ -139,17 +123,31 @@ public: protected: using Base::_ostream; - bool process(const LogEvent& msg) { - for (const auto os : this->_ostream) { - *os << msg.lvl << "\t|\t" << msg.msg << "\n"; - } - return true; - } + bool process(const LogEvent& msg); }; +#ifndef _NB_NO_LOGGER extern DefaultDebugLogger logger; +#endif // _NB_NO_LOGGER // Taking Charge of Adult ADHD by Russell Barkley } // namespace nb + +#ifndef CONSTRUCT_ERROR + #ifdef _NB_CODE_ERROR_LOCATIONS + #define CONSTRUCT_ERROR(type, ...) type(# __VA_ARGS__, __LINE__, __FILE__) + #else + #define CONSTRUCT_ERROR(type, ...) type(__VA_ARGS__) + #endif // _NB_CODE_ERROR_LOCATIONS +#endif // CONSTRUCT_ERROR + +#ifndef THROW + #ifdef _NB_AUTOLOG + #define THROW(type, ...) {auto x = CONSTRUCT_ERROR(type, ## __VA_ARGS__); nb::logger.error(x); throw x;} + #else + #define THROW(type, ...) throw CONSTRUCT_ERROR(type, __VA_ARGS__) + #endif // _NB_AUTOLOG +#endif // THROW + #endif // _NB_LOGGER \ No newline at end of file diff --git a/engine/NBCore/src/Logger.cpp b/engine/NBCore/src/Logger.cpp index 042fdaf..b9353a4 100644 --- a/engine/NBCore/src/Logger.cpp +++ b/engine/NBCore/src/Logger.cpp @@ -1,10 +1,24 @@ #include #include "Logger.hpp" +#include "StringUtils.hpp" namespace nb { +#ifndef _NB_NO_LOGGER nb::DefaultDebugLogger logger(std::cout); +#endif // _NB_NO_LOGGER + +bool nb::DefaultDebugLogger::process(const LogEvent& msg) { + for (const auto os : this->_ostream) { + *os << indent_strblock( + msg.msg, + std::string(10, ' '), + "[ "+std::to_string(msg.lvl)+" ] : " + ) << nb::NEWLINE; + } + return true; +} static bool RUN_LOGGER(nb::DefaultDebugLogger& log) { return log.run(); diff --git a/engine/NBCore/tests/CMakeLists.txt b/engine/NBCore/tests/CMakeLists.txt index 02c31c7..34476ff 100644 --- a/engine/NBCore/tests/CMakeLists.txt +++ b/engine/NBCore/tests/CMakeLists.txt @@ -12,5 +12,13 @@ if (NB_BUILD_TESTS) NBCore GTest::gtest_main ) + + add_executable(LoggerTest + ./testLogger.cpp + ) + target_link_libraries(LoggerTest + NBCore + ) + gtest_discover_tests(TestCore) endif() \ No newline at end of file diff --git a/engine/NBCore/tests/testLogger.cpp b/engine/NBCore/tests/testLogger.cpp new file mode 100644 index 0000000..da6fb99 --- /dev/null +++ b/engine/NBCore/tests/testLogger.cpp @@ -0,0 +1,17 @@ +#include + +#include "Errors.hpp" +#include "Logger.hpp" + + + +int main() { + nb::logger.log("Whoop!"); + try { + THROW(nb::Error, "Hiii!"); + } catch (nb::Error e){ + nb::logger.log("Winner winner chicken dinner!"); + } + + return 0; +} \ No newline at end of file