Compare commits

...

2 Commits

Author SHA1 Message Date
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
15 changed files with 273 additions and 138 deletions

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,7 +1,8 @@
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/Utils.cpp ./src/Utils.cpp
) )
@ -11,6 +12,7 @@ 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

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

@ -9,23 +9,7 @@
#include <type_traits> #include <type_traits>
#include "TypeTraits.hpp" #include "TypeTraits.hpp"
#include <unordered_map> #include <unordered_map>
#include "Utils.hpp" #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 {
@ -83,9 +67,9 @@ public:
ErrorBase& operator=(const ErrorBase&) = delete; ErrorBase& operator=(const ErrorBase&) = delete;
virtual inline std::string str() const noexcept override { virtual inline std::string str() const noexcept override {
std::string ret = msg; std::string ret = msg + nb::NEWLINE;
if (trace) { if (trace) {
const std::string replace = nb::NEWLINE+" "; const std::string tabover = " ";
std::string trace_msg; std::string trace_msg;
if (traceIsNBError) { if (traceIsNBError) {
trace_msg = std::static_pointer_cast<const ErrorBase>(trace)->str(); trace_msg = std::static_pointer_cast<const ErrorBase>(trace)->str();
@ -95,13 +79,9 @@ public:
} else { } else {
trace_msg = std::string(trace->what()); trace_msg = std::string(trace->what());
} }
ret += replace + "Trace: " + find_and_replace<std::string>( ret += indent_strblock(trace_msg, tabover, tabover+"Trace: ")+nb::NEWLINE;
trace_msg,
nb::NEWLINE,
replace
);
} }
return ret + nb::NEWLINE; return ret;
} }
static const std::string type; static const std::string type;
@ -198,7 +178,7 @@ protected:
) noexcept : ErrorBase( ) noexcept : ErrorBase(
0, 0,
msg_, msg_,
nullptr, static_cast<std::exception*>(nullptr),
line_, line_,
filename_ filename_
) {} ) {}
@ -219,13 +199,14 @@ 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;
using Base::code; using Base::code;
using Base::msg; using Base::msg;
using Base::trace; using Base::trace;
using Base::traceIsNBError;
using Base::type; using Base::type;
using Base::ErrorMessages; using Base::ErrorMessages;
@ -238,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
@ -245,10 +229,11 @@ public:
using Base::str; using Base::str;
using Base::what; using Base::what;
using Base::code; using Base::code;
using Base::msg; using Base::msg;
using Base::trace; using Base::trace;
using Base::traceIsNBError;
static const std::string type; static const std::string type;
static const ErrorCodeMap ErrorMessages; static const ErrorCodeMap ErrorMessages;
}; };

View File

@ -2,12 +2,9 @@
#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>
@ -23,14 +20,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,7 +30,7 @@ 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 (!type_ptr->isRunning()) {
this->_running = true; this->_running = true;
this->_runningThread = std::make_shared<std::thread>([&]{ this->_runningThread = std::make_shared<std::thread>([&]{
@ -62,6 +51,14 @@ private:
LoggerType* type_ptr = static_cast<LoggerType*>(this); LoggerType* type_ptr = static_cast<LoggerType*>(this);
}; };
struct LogEvent{
const LoggerTimePoint time;
const unsigned char lvl;
const std::string msg;
const std::thread::id tid;
const uint64_t pid;
};
template <typename LT> template <typename LT>
class DebugLogger : public LoggerBase<LogEvent, LT, std::vector<std::ostream*>>{ class DebugLogger : public LoggerBase<LogEvent, LT, std::vector<std::ostream*>>{
using StreamType = std::vector<std::ostream*>; using StreamType = std::vector<std::ostream*>;
@ -74,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,
@ -85,7 +82,7 @@ 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);
} }
@ -104,7 +101,6 @@ protected:
private: private:
LoggerType* type_ptr = static_cast<LoggerType*>(this); LoggerType* type_ptr = static_cast<LoggerType*>(this);
}; };
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> { class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
@ -127,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

@ -0,0 +1,142 @@
#pragma once
#ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES
#include <iostream>
#include <string>
#include <type_traits>
#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
template <typename... T>
using StringConvertible = ValidConversion<std::string, T...>;
template <typename... T>
using WStringConvertible = ValidConversion<std::wstring, T...>;
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;
}
template <typename Stream, typename... Args>
void stream(Stream& s, Args&&... args);
template <typename Stream, typename... Args>
void stream(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 = std::string, typename A, typename B, typename C>
ValidConversion_to<T, A, B, C> find_and_replace(
A original,
B find,
C replace
) {
const T& find_t = T(find);
const T& replace_t = T(replace);
T ret(original);
std::size_t find_len = find_t.length();
std::size_t replace_len = replace_t.length();
std::size_t currpos = 0;
while(true) {
currpos = ret.find(find_t, currpos);
if (currpos == T::npos) {
break;
}
ret = ret.erase(currpos, find_len);
ret = ret.insert(currpos, replace_t);
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)
);
}
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>;
} // namespace nb
#endif // _NB_CORE_TYPES

View File

@ -2,7 +2,9 @@
#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 {
@ -37,11 +39,41 @@ template <
> >
using is_detected = typename detail::detector<NoneType, void, Op, Args...>::value; using is_detected = typename detail::detector<NoneType, void, Op, Args...>::value;
/*template<std::size_t N=0, typename Func, typename... Pack> 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 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));
@ -99,7 +131,7 @@ struct PackIsSameType<T, PackType> {
template <typename T, typename FirstPack, typename... Pack> template <typename T, typename FirstPack, typename... Pack>
struct PackIsSameType<T, FirstPack, Pack...> { struct PackIsSameType<T, FirstPack, Pack...> {
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value; const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
};*/ };
} // namespace nb } // namespace nb

View File

@ -2,80 +2,8 @@
#ifndef _NB_CORE_TYPES #ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES #define _NB_CORE_TYPES
#include <iostream>
#include <string>
#include <type_traits>
namespace nb { 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
template<typename T>
std::enable_if_t<std::is_convertible_v<T, std::string>, 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<std::is_convertible_v<T, std::wstring>, 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;
}
template <typename Stream, typename... Args>
void stream(Stream& s, Args&&... args);
template <typename Stream, typename... Args>
void stream(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 = std::string, typename A, typename B, typename C>
T find_and_replace(
A original,
B find,
C replace
) {
const T& original_t = T(original);
const T& find_t = T(find);
const T& replace_t = T(replace);
T ret = original_t;
std::size_t find_len = find_t.length();
std::size_t replace_len = replace_t.length();
std::size_t currpos = 0;
while(true) {
currpos = ret.find(find_t, currpos);
if (currpos == T::npos) {
break;
}
ret = ret.erase(currpos, find_len);
ret = ret.insert(currpos, replace_t);
currpos += replace_len;
}
return ret;
}
/* template<typename T> /* template<typename T>
T swap_endian(const T& val) { T swap_endian(const T& val) {
T ret; T ret;

View File

@ -1,15 +1,31 @@
#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();
} }
static const bool LOGGER_RUNNING = RUN_LOGGER(logger); static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
} // namespace nb } // namespace nb

View File

@ -0,0 +1 @@
#include "StringUtils.hpp"

View File

@ -1,8 +1 @@
#include <string>
#include "Utils.hpp"
template<>
std::wstring nb::str_to_wstr(const std::string& in) {
std::wstring ret(in.begin(), in.end());
return ret;
}

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

@ -4,7 +4,6 @@
#include "Errors.hpp" #include "Errors.hpp"
#include <string> #include <string>
#include "Utils.hpp"
using namespace nb; using namespace nb;

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;
}

View File

@ -4,7 +4,7 @@
#include <string> #include <string>
#include "TypeTraits.hpp" #include "TypeTraits.hpp"
#include "Utils.hpp" #include "StringUtils.hpp"
namespace nb { namespace nb {