Compare commits

...

9 Commits

Author SHA1 Message Date
NaifBanana
2ef222b7ba Formatting tweaks 2026-05-02 03:37:06 -05:00
NaifBanana
1c40d98812 Got logging and errors basically all working the way I want 2026-05-02 01:50:53 -05:00
NaifBanana
85241b4aa5 Error logging almost working! 2026-04-19 19:19:47 -05:00
NaifBanana
a5dc639958 Added type traits and string utils 2026-04-19 18:08:58 -05:00
NaifBanana
1b4a891a58 nb::Error objects now working 2026-04-18 18:33:33 -05:00
NaifBanana
6c71bd46af I mean it compiled 2026-04-10 00:41:01 -05:00
NaifBanana
b593b7e083 Man im so tired This is wrong but I can taste it, im close 2026-04-10 00:31:11 -05:00
NaifBanana
a927b96a99 Added more Util tests and str-wstr related changes to util functions 2026-04-09 17:44:19 -05:00
NaifBanana
126862748e Made errors mostly work as hoped 2026-04-06 02:22:28 -05:00
19 changed files with 868 additions and 186 deletions

View File

@ -48,13 +48,12 @@ if(NB_BUILD_TESTS)
include(FetchContent) include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
gtest gtest
URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.zip URL https://github.com/google/googletest/archive/refs/heads/main.zip
) )
if (WIN32) if (WIN32)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(gtest) FetchContent_MakeAvailable(gtest)
endif() endif()
include(GoogleTest)
set(GTEST_COLOR ON) set(GTEST_COLOR ON)
set(GLFW_BUILD_TESTS ON CACHE BOOL "" FORCE) set(GLFW_BUILD_TESTS ON CACHE BOOL "" FORCE)
endif() endif()
@ -62,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

@ -2,16 +2,10 @@
#ifndef _NB_ANSI_TERM #ifndef _NB_ANSI_TERM
#define _NB_ANSI_TERM #define _NB_ANSI_TERM
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <tuple>
#include <type_traits> #include <type_traits>
#include "TypeTraits.hpp"
/* /*
----------- TECH DEBT ------------ ----------- TECH DEBT ------------
Idk wtf to do here. This was originally to allow me to print Idk wtf to do here. This was originally to allow me to print

View File

@ -1,9 +1,9 @@
include_directories(./.)
toAbsolutePath(NB_CORE_SOURCE toAbsolutePath(NB_CORE_SOURCE
./src/Errors.cpp ./src/Errors.cpp
./src/Processes.cpp
./src/Logger.cpp ./src/Logger.cpp
./src/Processes.cpp
./src/StringUtils.cpp
./src/Utils.cpp
) )
toAbsolutePath(NB_CORE_INCLUDE toAbsolutePath(NB_CORE_INCLUDE
@ -12,9 +12,11 @@ toAbsolutePath(NB_CORE_INCLUDE
./Errors.hpp ./Errors.hpp
./Logger.hpp ./Logger.hpp
./Processes.hpp ./Processes.hpp
./StringUtils.hpp
./ThreadsafeQueue.hpp ./ThreadsafeQueue.hpp
./Types.hpp ./Types.hpp
./TypeTraits.hpp ./TypeTraits.hpp
./Utils.hpp
) )
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE) set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
@ -22,6 +24,8 @@ set(NB_CORE_INCLUDE ${NB_CORE_INCLUDE} PARENT_SCOPE)
add_library(NBCore ${NB_CORE_SOURCE}) add_library(NBCore ${NB_CORE_SOURCE})
target_include_directories(NBCore PUBLIC ./.)
if (NB_BUILD_TESTS) if (NB_BUILD_TESTS)
add_subdirectory(./tests) add_subdirectory(./tests)
endif() endif()

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 {
@ -77,7 +78,7 @@ public:
bool isRunning() const noexcept override { bool isRunning() const noexcept override {
return this->_running && (_runningThread!=nullptr); return this->_running && (_runningThread!=nullptr);
} }
bool run() { bool run() override {
if (!type_ptr->isRunning()) { if (!type_ptr->isRunning()) {
this->_running = true; this->_running = true;
_runningThread = std::make_shared<std::thread>([&]{ _runningThread = std::make_shared<std::thread>([&]{

View File

@ -6,134 +6,275 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include "TypeTraits.hpp"
#include <unordered_map> #include <unordered_map>
#include "StringUtils.hpp"
#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
namespace nb { namespace nb {
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap; typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
template<class ErrorType> template<typename T>
class ErrorBase : public std::exception { 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: public:
ErrorBase( const unsigned int code;
const unsigned int code, const std::string msg;
unsigned int line=0, const std::string type;
std::string filename="" const std::shared_ptr<const void> trace;
) noexcept : ErrorBase( const bool traceIsNBError;
code,
ErrorBase<ErrorType>::lookup(code),
line,
filename
) {}
ErrorBase(
const unsigned int code,
const std::exception& trace,
unsigned int line=0,
std::string filename=""
) noexcept : ErrorBase(
code,
ErrorBase<ErrorType>::lookup(code),
trace,
line,
filename
) {}
static std::string lookup(unsigned int);
unsigned int code() const noexcept {
return static_cast<const ErrorType*>(this)->_code;
};
virtual const char* what() const noexcept override final { return _msg.c_str(); };
virtual std::string what() const noexcept = 0;
virtual std::shared_ptr<ErrorBase> make_shared() const noexcept = 0;
protected: protected:
ErrorBase() = default; 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( ErrorBase(
const unsigned int code, unsigned int code_,
std::string msg, std::string msg_,
unsigned int line=0, std::string type_,
std::string filename="" const std::exception* trace_
) noexcept : _code{code}, _msg{""} { ) 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, 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 " "const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
"a class member." "a class member."
); );
static_assert(std::is_enum_v<typename ErrorType::ErrorCodes>, "enum ErrorCodes 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::ErrorCodes>, unsigned int>::value, static_assert(std::is_same<std::underlying_type_t<typename ErrorType::Codes>, unsigned int>::value,
"enum ErrorCodes must be of underlying type unsigned int." "enum Codes must be of underlying type unsigned int."
); );
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value, static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
"const std::string type must be a class member." "const std::string type must be a class member."
); );
_msg += std::string(ErrorType::type);
_msg += "[" + std::to_string(_code) + "]";
if (line && filename.size()) {
_msg += " in \'" + filename + "\':" + std::to_string(line);
}
_msg += "\n " + msg;
}
ErrorBase(
const unsigned int code,
std::string msg,
const std::exception& trace,
unsigned int line=0,
std::string filename=""
) noexcept : ErrorBase(code, msg, line, filename) {
std::string what_msg(trace.what());
std::string::size_type newline_pos=-1;
while((newline_pos=what_msg.find("\n", newline_pos+1))!= std::string::npos) {
what_msg.replace(newline_pos, 1, "\n ");
}
_msg += "\n Trace - " + what_msg;
} }
unsigned int _code;
std::string _msg;
};
class Error : public ErrorBase<Error> {
public: public:
enum ErrorCodes : unsigned int { template <typename T>
GENERAL, UNDEFINED, BADERRORCODE ErrorBase(const ErrorBase<T>& cpy) : Base(cpy) { check_asserts(); }
};
using ErrorBase<Error>::ErrorBase; virtual std::shared_ptr<Base> make_shared() const noexcept override {
return std::static_pointer_cast<Base>(
std::make_shared<ErrorBase>(*this)
);
}
friend ErrorBase<Error>; 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: protected:
static const std::string type; using Base::Base;
static const ErrorCodeMap ErrorMessages;
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 ErrorType>
std::string ErrorBase<ErrorType>::lookup(unsigned int code) {
for (auto kv : ErrorType::ErrorMessages) {
if (kv.first==code) {
return std::string(kv.second);
}
}
throw Error(Error::ErrorCodes::BADERRORCODE); 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 } // namespace nb
#endif // _NB_ERROR #endif // _NB_ERROR

View File

@ -2,16 +2,14 @@
#ifndef _NB_LOGGER #ifndef _NB_LOGGER
#define _NB_LOGGER #define _NB_LOGGER
#include <atomic>
#include <chrono> #include <chrono>
#include <iomanip>
#include <ostream> #include <ostream>
#include <thread> #include <thread>
#include <type_traits>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "DataSink.hpp" #include "DataSink.hpp"
#include "Errors.hpp"
#include "Processes.hpp" #include "Processes.hpp"
#include "ThreadSafeQueue.hpp" #include "ThreadSafeQueue.hpp"
#include "TypeTraits.hpp" #include "TypeTraits.hpp"
@ -23,14 +21,6 @@ typedef std::chrono::time_point<
std::chrono::nanoseconds std::chrono::nanoseconds
> LoggerTimePoint; > LoggerTimePoint;
struct LogEvent{
const LoggerTimePoint time;
const unsigned char lvl;
const std::string msg;
const std::thread::id tid;
const uint64_t pid;
};
typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&); typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&);
typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap; typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
@ -41,25 +31,32 @@ class LoggerBase
using LoggerType = Logger; using LoggerType = Logger;
using Base = MultithreadedDataProcessor<LogType, LoggerType>; using Base = MultithreadedDataProcessor<LogType, LoggerType>;
public: public:
bool run() { bool run() override {
if (!type_ptr->isRunning()) { if (!static_cast<LoggerType*>(this)->isRunning()) {
this->_running = true; this->_running = true;
this->_runningThread = std::make_shared<std::thread>([&]{ this->_runningThread = std::make_shared<std::thread>([&]{
while(type_ptr->isRunning()) { while(static_cast<LoggerType*>(this)->isRunning()) {
type_ptr->flush(); static_cast<LoggerType*>(this)->flush();
} }
type_ptr->flush(); static_cast<LoggerType*>(this)->flush();
}); });
} }
return type_ptr->isRunning(); return static_cast<LoggerType*>(this)->isRunning();
} }
protected: protected:
LoggerBase() = default; LoggerBase() = default;
StreamType _ostream; StreamType _ostream;
};
private: struct LogEvent{
LoggerType* type_ptr = static_cast<LoggerType*>(this); const LoggerTimePoint time;
const unsigned char lvl;
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> template <typename LT>
@ -72,39 +69,73 @@ public:
template<typename... ST> template<typename... ST>
DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec<std::ostream, ST...>(streams...).vec) {} 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=0xFF) { void log(
type_ptr->push(LogEvent{ 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(), std::chrono::system_clock::now(),
lvl, lvl,
msg, msg,
std::this_thread::get_id(), std::this_thread::get_id(),
GetPID(), GetPID(),
file,
line
}); });
} }
template <size_t N> template <size_t N>
void log(char const(&msg) [N], const uint8_t& lvl=0xFF) { void log(
type_ptr->log(std::string(msg), lvl); 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) { template <typename T>
type_ptr->log(err.what(), lvl); 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> 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> 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: protected:
std::vector<std::ostream*> _ostream; std::vector<std::ostream*> _ostream;
private:
LoggerType* type_ptr = static_cast<LoggerType*>(this);
}; };
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> { class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
@ -127,17 +158,112 @@ 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
/* 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 } // namespace nb
#ifdef _NB_AUTOLOG
#ifdef _NB_CODE_ERROR_LOCATIONS
#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
#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 // _NB_AUTOLOG
#ifndef THROW
#ifdef _NB_AUTOLOG
#define THROW(args...) ERROR(args); throw args
#else
#define THROW(args...) throw args
#endif // _NB_CODE_ERROR_LOCATIONS
#endif // THROW
#endif // _NB_LOGGER #endif // _NB_LOGGER

View File

@ -0,0 +1,79 @@
#pragma once
#ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES
#include <iostream>
#include <string>
#include <string_view>
#include "TypeTraits.hpp"
namespace nb {
#ifdef _NB_TARGET_WINDOWS
const std::string NEWLINE = "\n";
const std::wstring WNEWLINE = L"\n";
#endif // _NB_TARGET_WINDOWS
#ifdef _NB_TARGET_LINUX
const std::string NEWLINE = "\n";
const std::wstring WNEWLINE = L"\n";
#endif // _NB_TARGET_LINUX
const std::string TABOVER = " ";
// std::wstring str_to_wstr(std::string in);
// std::string wstr_to_str(std::wstring in);
template <typename Stream, typename... Args>
void stream(const Stream& s, Args&&... args);
template <typename Stream, typename... Args>
void stream(const Stream& s, Args&&... args) {
(s << ... << args);
}
template<typename... Args>
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 = 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
) {
using StringType = std::basic_string<T>;
StringType ret(original);
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, currpos);
if (currpos == StringType::npos) {
break;
}
ret = ret.erase(currpos, find_len);
ret = ret.insert(currpos, replace);
currpos += replace_len;
}
return ret;
}
std::string indent_strblock(
std::string block,
std::string prepend,
std::string topIndent
);
std::string indent_strblock(
std::string block,
std::string prepend
);
} // namespace nb
#endif // _NB_CORE_TYPES

View File

@ -2,15 +2,96 @@
#ifndef _NB_TYPE_TRAITS #ifndef _NB_TYPE_TRAITS
#define _NB_TYPE_TRAITS #define _NB_TYPE_TRAITS
#include <tuple>
#include <type_traits> #include <type_traits>
#include <vector>
namespace nb { namespace nb {
template<std::size_t N=0, typename Func, typename... Pack> namespace detail {
template <
class Default,
class AlwaysVoid,
template <class...> class Op,
class... Args
>
struct detector {
using value = std::false_type;
using type = Default;
};
template <
class Default,
template <class...> class Op,
class... Args
>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...> {
using value = std::true_type;
using type = Op<Args...>;
};
} // namespace detail
struct NoneType{};
template <
template <class...> class Op,
class... Args
>
using is_detected = typename detail::detector<NoneType, void, Op, Args...>::value;
template <typename... Types>
struct ValidConversion;
template <typename To, typename A, typename B, typename... Rest>
struct ValidConversion<To, A, B, Rest...> {
typedef std::conjunction<
std::is_constructible<To, A>, std::is_constructible<To, A>, std::conjunction<std::is_constructible<To, Rest>...>
> value_type;
static constexpr bool value = value_type::value;
typedef std::enable_if_t<value, To> to;
};
template <typename To, typename From>
struct ValidConversion<To, From> {
typedef std::is_constructible<To, From> value_type;
static constexpr bool value = value_type::value;
typedef std::enable_if_t<value, To> to;
typedef std::enable_if_t<value, From> from;
};
template<typename... Types>
using ValidConversion_v = typename ValidConversion<Types...>::value;
template<typename... Types>
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 inline typename std::enable_if<N==sizeof...(Pack), void>::type
ForEach(std::tuple<Pack...>&, Func) {} ForEach(std::tuple<Pack...>&, Func) {}
template<std::size_t N=0, typename Func, typename... Pack> template<typename Func, std::size_t N=0, typename... Pack>
inline typename std::enable_if<N < sizeof...(Pack), void>::type inline typename std::enable_if<N < sizeof...(Pack), void>::type
ForEach(std::tuple<Pack...>& tup, Func f) { ForEach(std::tuple<Pack...>& tup, Func f) {
f(N, std::get<N>(tup)); f(N, std::get<N>(tup));

View File

@ -2,12 +2,10 @@
#ifndef _NB_CORE_TYPES #ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES #define _NB_CORE_TYPES
#include <vector>
namespace nb { namespace nb {
template<typename T> /* template<typename T>
T swapEndian(const T& val) { T swap_endian(const T& val) {
T ret; T ret;
const int size = sizeof(T); const int size = sizeof(T);
auto retLoc = static_cast<void*>(&ret); auto retLoc = static_cast<void*>(&ret);
@ -17,9 +15,10 @@ T swapEndian(const T& val) {
memcpy(retLoc+i, valLoc+(size-i-1), 1); memcpy(retLoc+i, valLoc+(size-i-1), 1);
} }
return ret; return ret;
} } */
using ByteVector = std::vector<uint8_t>;
// using ByteVector = std::vector<uint8_t>;
} // namespace nb } // namespace nb
#endif // _NB_CORE_TYPES #endif // _NB_CORE_TYPES

View File

@ -1,12 +1,13 @@
#include "Errors.hpp" #include "Errors.hpp"
#include "TypeTraits.hpp"
namespace nb { namespace nb {
const std::string Error::type = "Error"; const std::string Error<>::type = "nb::Error";
const ErrorCodeMap Error::ErrorMessages = {
{ErrorCodes::GENERAL, "General std::exception."}, const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = {
{ErrorCodes::UNDEFINED, "Undefined / general error."}, {Error::Codes::STANDARD, "std::exception"},
{ErrorCodes::BADERRORCODE, "Unrecognized error code."} {Error::Codes::UNDEFINED, "Error"}
}; };
} }

View File

@ -1,15 +1,40 @@
#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) {
constexpr size_t level_field_width = 5;
constexpr size_t msg_prompt_length = level_field_width+7;
for (const auto os : this->_ostream) {
*os << "[";
os->width(level_field_width);
*os << std::to_string(msg.lvl) << "] :: ";
std::string fileloc="";
if (!msg.file.empty()) {
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
}
*os << indent_strblock(
fileloc + msg.msg,
std::string(20 - msg_prompt_length, ' '),
""
) << nb::NEWLINE;
}
return true;
}
static bool RUN_LOGGER(nb::DefaultDebugLogger& log) { static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
return log.run(); return log.run();
} }
static const bool LOGGER_RUNNING = RUN_LOGGER(logger); static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
} // namespace nb } // namespace nb

View File

@ -7,7 +7,6 @@
#endif // _NB_TARGET_LINUX #endif // _NB_TARGET_LINUX
#include "Processes.hpp" #include "Processes.hpp"
#include "Types.hpp"
namespace nb { namespace nb {

View File

@ -0,0 +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);
}

View File

@ -0,0 +1 @@

View File

@ -4,12 +4,21 @@ if (NB_BUILD_TESTS)
enable_testing() enable_testing()
include(GoogleTest) include(GoogleTest)
add_executable(TestCore add_executable(TestCore
testErrors.cpp ./testErrors.cpp
testProcesses.cpp #./testProcesses.cpp
./testUtils.cpp
) )
target_link_libraries(TestCore target_link_libraries(TestCore
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

@ -1,38 +1,39 @@
#define CODE_ERROR_LOCATIONS #define CODE_ERROR_LOCATIONS
#include "Errors.hpp"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <iostream>
#include <sstream> #include "Errors.hpp"
#include "Logger.hpp" #include <string>
using namespace nb; using namespace nb;
class TestError : public ErrorBase<TestError> { class TestError : public Error<TestError> {
using Base = Error<TestError>;
public: public:
using ErrorBase<TestError>::ErrorBase; using Base::Base;
using Base::what;
using Base::code;
using Base::msg;
using Base::trace;
enum ErrorCodes : unsigned int { enum Codes : unsigned int {
A, B, C, D A, B, C, D
}; };
static const std::string type; static const std::string type;
static const ErrorCodeMap ErrorMessages; static const nb::ErrorCodeMap ErrorMessages;
}; };
const std::string TestError::type="TestError"; const std::string TestError::type="TestError";
const ErrorCodeMap TestError::ErrorMessages{ const ErrorCodeMap TestError::ErrorMessages{
{TestError::ErrorCodes::A, "Hey!"}, {TestError::Codes::A, "Hey!"},
{TestError::ErrorCodes::B, "How"}, {TestError::Codes::B, "How"},
{TestError::ErrorCodes::C, "You"}, {TestError::Codes::C, "You"},
{TestError::ErrorCodes::D, "Doin"} {TestError::Codes::D, "Doin"}
}; };
TEST(ErrorTest, Test) { TEST(ErrorTest, Test) {
EXPECT_EQ(1, 1); auto err = TestError(0, TestError(1, TestError(2, TestError(3, TestError(2)))));
ASSERT_STREQ(err.what().c_str(), "Hey!\n Trace: How\n Trace: You\n Trace: Doin\n Trace: You");
std::stringstream sstream;
ASSERT_TRUE(nb::logger.isRunning());
nb::logger.log("Hey!");
} }

View File

@ -0,0 +1,46 @@
#include <exception>
#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(TestError(0, TestError(1, TestError(2, TestError(3, TestError(2))))));
} catch (TestError e){
nb::logger.log("nb::Error was thrown!");
}
try {
THROW(std::exception());
} catch (std::exception e){
nb::logger.log("std::exception was thrown!");
}
ERROR("hello there!");
return 0;
}

View File

@ -1,8 +1,6 @@
#define CODE_ERROR_LOCATIONS #define CODE_ERROR_LOCATIONS
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <iostream>
#include "Processes.hpp" #include "Processes.hpp"
#include <Windows.h> #include <Windows.h>

View File

@ -0,0 +1,148 @@
#define CODE_ERROR_LOCATIONS
#include <gtest/gtest.h>
#include <string>
#include "TypeTraits.hpp"
#include "StringUtils.hpp"
namespace nb {
TEST(UtilsTest, TestFindAndReplace) {
ASSERT_STREQ(
find_and_replace("Jeff", "e", "efe").c_str(),
"Jefeff"
);
std::string tmp = find_and_replace("Naif", "a", "afa");
ASSERT_STREQ(
find_and_replace(tmp, "i", "ifi").c_str(),
"Nafaifif"
);
tmp = find_and_replace("aeiou", "a", "afa");
tmp = find_and_replace(tmp, "e", "efe");
tmp = find_and_replace(tmp, "i", "ifi");
tmp = find_and_replace(tmp, "o", "ofo");
tmp = find_and_replace(tmp, "u", "ufu");
ASSERT_STREQ(
tmp.c_str(),
"afaefeifiofoufu"
);
tmp = find_and_replace(tmp, "afa", "a");
tmp = find_and_replace(tmp, "efe", "e");
tmp = find_and_replace(tmp, "ifi", "i");
tmp = find_and_replace(tmp, "ofo", "o");
tmp = find_and_replace(tmp, "ufu", "u");
ASSERT_STREQ(
tmp.c_str(),
"aeiou"
);
}
TEST(UtilsTest, TestFindAndReplaceWstr) {
ASSERT_STREQ(
find_and_replace<wchar_t>(L"Jeff", L"e", L"efe").c_str(),
L"Jefeff"
);
std::wstring tmp = find_and_replace<wchar_t>(L"Naif", L"a", L"afa");
ASSERT_STREQ(
find_and_replace<wchar_t>(tmp, L"i", L"ifi").c_str(),
L"Nafaifif"
);
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<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) {
ASSERT_STREQ(
nb::wstr_to_str(L"Hi!").c_str(),
"Hi!"
);
ASSERT_STREQ(
nb::wstr_to_str(L"Naif").c_str(),
"Naif"
);
ASSERT_STREQ(
nb::wstr_to_str(L"\r\r\r\n").c_str(),
"\r\r\r\n"
);
ASSERT_STREQ(
nb::wstr_to_str(L"Naif\ttalks\r\ra\t\nlot").c_str(),
"Naif\ttalks\r\ra\t\nlot"
);
} */
/* TEST(UtilsTest, TestStrToWstr) {
ASSERT_STREQ(
nb::str_to_wstr("Hi!").c_str(),
L"Hi!"
);
ASSERT_STREQ(
nb::str_to_wstr("Naif").c_str(),
L"Naif"
);
ASSERT_STREQ(
nb::str_to_wstr("\r\r\r\n").c_str(),
L"\r\r\r\n"
);
ASSERT_STREQ(
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(); };
struct C { bool x(); bool y; };
struct D : C {};
template <typename T>
using has_x = decltype(T::x);
template <typename T>
using has_y = decltype(T::y);
TEST(UtilsTest, TestIsDetected) {
auto ret = is_detected<has_x, A>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_y, A>::value;
ASSERT_FALSE(ret);
ret = is_detected<has_y, B>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_x, B>::value;
ASSERT_FALSE(ret);
ret = is_detected<has_x, C>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_y, C>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_x, D>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_y, D>::value;
ASSERT_TRUE(ret);
}
} // namespace nb