WIP: Custom Erroring system #1

Draft
naifb wants to merge 11 commits from error_struct into main
8 changed files with 73 additions and 31 deletions
Showing only changes of commit 0f62aecfa3 - Show all commits

View File

@ -61,6 +61,7 @@ endif()
if (NB_LOGGING) if (NB_LOGGING)
message(STATUS "Building with automatic logging") message(STATUS "Building with automatic logging")
add_compile_definitions(_NB_AUTOLOG) add_compile_definitions(_NB_AUTOLOG)
add_compile_definitions(_NB_CODE_ERROR_LOCATIONS)
endif() endif()
if (NB_TARGET_WINDOWS) if (NB_TARGET_WINDOWS)

View File

@ -1,6 +1,6 @@
toAbsolutePath(NB_CORE_SOURCE toAbsolutePath(NB_CORE_SOURCE
./src/Errors.cpp ./src/Errors.cpp
# ./src/Logger.cpp ./src/Logger.cpp
./src/Processes.cpp ./src/Processes.cpp
./src/StringUtils.cpp ./src/StringUtils.cpp
./src/Utils.cpp ./src/Utils.cpp

View File

@ -2,6 +2,7 @@
#ifndef _NB_DATASINK #ifndef _NB_DATASINK
#define _NB_DATASINK #define _NB_DATASINK
#include <atomic>
#include <thread> #include <thread>
#include "ThreadSafeQueue.hpp" #include "ThreadSafeQueue.hpp"
@ -34,9 +35,9 @@ class BufferedDataProcessor : public DataSink<DataType> {
public: public:
using Base::Base; using Base::Base;
bool stop() noexcept { return type_ptr->stop(); } virtual bool stop() noexcept override { return type_ptr->stop(); }
bool run() { return type_ptr->run(); } virtual bool run() override { return type_ptr->run(); }
bool in(const DataType& val) { return type_ptr->in(val); } virtual bool in(const DataType& val) override { return type_ptr->in(val); }
protected: protected:
unsigned int count() const { unsigned int count() const {

View File

@ -178,7 +178,7 @@ protected:
) noexcept : ErrorBase( ) noexcept : ErrorBase(
0, 0,
msg_, msg_,
nullptr, static_cast<std::exception*>(nullptr),
line_, line_,
filename_ filename_
) {} ) {}
@ -199,7 +199,7 @@ class Error : public ErrorBase<ErrorType> {
using Base = ErrorBase<ErrorType>; using Base = ErrorBase<ErrorType>;
public: public:
template <typename... Args> template <typename... Args>
Error(Args... T) : Base(T...) {} Error(Args... args) : Base(args...) {}
using Base::str; using Base::str;
using Base::what; using Base::what;
@ -219,6 +219,9 @@ class Error<NoneType> : public ErrorBase<Error<NoneType>> {
public: public:
using Base::Base; using Base::Base;
Error() : Base(0) {} Error() : Base(0) {}
template <typename... Args>
Error(Args... args) : Base(args...) {}
enum Codes : unsigned int { enum Codes : unsigned int {
GENERAL, UNDEFINED, BADERRORCODE GENERAL, UNDEFINED, BADERRORCODE

View File

@ -8,22 +8,6 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#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 "DataSink.hpp"
#include "Processes.hpp" #include "Processes.hpp"
#include "ThreadSafeQueue.hpp" #include "ThreadSafeQueue.hpp"
@ -87,7 +71,7 @@ public:
~DebugLogger() { type_ptr->stop(); } ~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{ type_ptr->push(LogEvent{
std::chrono::system_clock::now(), std::chrono::system_clock::now(),
lvl, lvl,
@ -98,12 +82,12 @@ public:
} }
template <size_t N> template <size_t N>
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); type_ptr->log(std::string(msg), lvl);
} }
void log(const std::exception& err, const uint8_t& lvl=0xFF) { 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<typename U> template<typename U>
@ -139,17 +123,31 @@ public:
protected: protected:
using Base::_ostream; using Base::_ostream;
bool process(const LogEvent& msg) { bool process(const LogEvent& msg);
for (const auto os : this->_ostream) {
*os << msg.lvl << "\t|\t" << msg.msg << "\n";
}
return true;
}
}; };
#ifndef _NB_NO_LOGGER
extern DefaultDebugLogger logger; extern DefaultDebugLogger logger;
#endif // _NB_NO_LOGGER
// Taking Charge of Adult ADHD by Russell Barkley // Taking Charge of Adult ADHD by Russell Barkley
} // namespace nb } // 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 #endif // _NB_LOGGER

View File

@ -1,10 +1,24 @@
#include <iostream> #include <iostream>
#include "Logger.hpp" #include "Logger.hpp"
#include "StringUtils.hpp"
namespace nb { namespace nb {
#ifndef _NB_NO_LOGGER
nb::DefaultDebugLogger logger(std::cout); 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) { static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
return log.run(); return log.run();

View File

@ -12,5 +12,13 @@ if (NB_BUILD_TESTS)
NBCore NBCore
GTest::gtest_main GTest::gtest_main
) )
add_executable(LoggerTest
./testLogger.cpp
)
target_link_libraries(LoggerTest
NBCore
)
gtest_discover_tests(TestCore) gtest_discover_tests(TestCore)
endif() endif()

View File

@ -0,0 +1,17 @@
#include <exception>
#include "Errors.hpp"
#include "Logger.hpp"
int main() {
nb::logger.log("Whoop!");
try {
THROW(nb::Error, "Hiii!");
} catch (nb::Error<nb::NoneType> e){
nb::logger.log("Winner winner chicken dinner!");
}
return 0;
}