Cleaning up logger.

This commit is contained in:
NaifBanana 2026-03-15 23:18:45 -05:00
parent cc0bca634f
commit 48464504c8
7 changed files with 126 additions and 108 deletions

View File

@ -3,6 +3,10 @@ project(NBEngine VERSION 0.1.0 LANGUAGES C CXX)
cmake_policy(SET CMP0135 NEW)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(CMAKE_BUILD_TYPE STREQUAL "Release")
message(STATUS "Targeting Release build")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")

View File

@ -12,6 +12,14 @@
#include "TypeTraits.hpp"
/*
----------- TECH DEBT ------------
Idk wtf to do here. This was originally to allow me to print
to terminal easily, but it's becoming too much of an investment.
For the time being, drop it.
*/
namespace nb {
template <typename ST, typename U>
@ -19,15 +27,19 @@ struct SmartText {
using Type = ST;
using ValType = U;
static constexpr bool HasChild = Type::HasChild;
SmartText() = default;
};
template <typename Type, typename U>
struct StyleFormatter : SmartText<StyleFormatter<Type, U>, U> {
using Base = SmartText<StyleFormatter<Type, U>>;
using Base::Type;
using Base::ValType;
using Base = SmartText<StyleFormatter<Type, U>, U>;
using typename Base::ValType;
static constexpr bool HasChild = true;
StyleFormatter(const std::string& _fmt, const ValType& _val, const std::string& _closing) :
fmt{_fmt}, val{_val}, closing{_closing} {}
std::string fmt;
ValType val;
std::string closing;
@ -35,11 +47,12 @@ struct StyleFormatter : SmartText<StyleFormatter<Type, U>, U> {
template <typename Child>
struct StyleFormatter<Child, void> : SmartText<StyleFormatter<Child, void>, void>{
using Base = SmartText<StyleFormatter>;
using Base::Type;
using Base = SmartText<StyleFormatter<Child, void>, void>;
using Base::ValType;
static constexpr bool HasChild = false;
StyleFormatter(const std::string& _fmt) : fmt{_fmt} {}
std::string fmt;
};
@ -49,7 +62,7 @@ struct ANSITextColor;
template <typename U>
struct ANSITextColor : StyleFormatter<ANSITextColor<U>, U> {
using Base = StyleFormatter<ANSITextColor, U>;
using Base::ValType
using typename Base::ValType;
using Base::HasChild;
template <typename = typename std::enable_if<HasChild, U>::type>
@ -69,7 +82,23 @@ struct ANSITextColor<std::string> : StyleFormatter<ANSITextColor<std::string>, s
ANSITextColor(const std::string& _val) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
};
struct CellBase {
} // namespace nb
template<typename Type, typename U>
std::ostream& operator<<(std::ostream& stream, const nb::StyleFormatter<Type, U>& msg) {
if (&stream == &std::cout) {
stream << msg.fmt;
if (msg.HasChild) {
stream << msg.val << msg.closing;
}
} else {
if (msg.HasChild) { stream << msg.val; }
}
stream << std::flush;
return stream;
}
/* struct CellBase {
uint8_t width;
uint8_t height;
};
@ -77,13 +106,13 @@ struct CellBase {
template <typename T>
struct Cell : SmartText<Cell<T>, T> {
using Base = SmartText<Cell<T>, T>;
using Base::ValType
using typename Base::ValType;
using Base::HasChild;
Cell(const Type& _val, const uint8_t& w, const uint8_t& h=1)
Cell(const ValType& _val, const uint8_t& w, const uint8_t& h=1)
: val(_val), width(w), height(h) {}
Type val;
ValType val;
uint8_t width;
uint8_t height;
@ -92,7 +121,7 @@ struct Cell : SmartText<Cell<T>, T> {
template <>
struct Cell<std::string> : SmartText<Cell<std::string>, std::string> {
using Base = SmartText<Cell<std::string>, std::string>;
using Base::ValType
using typename Base::ValType;
using Base::HasChild;
template <std::size_t N>
@ -113,7 +142,10 @@ struct Cell<std::string> : SmartText<Cell<std::string>, std::string> {
template <typename T>
std::ostream& operator<<(std::ostream& stream, const Cell<T>& cll) {
if (&stream == &std::cout) {
stream << "\x1b[s" << cll.val << "\x1b[u\x1b[" << 1+cll.width << "C\x1b[" << 1+cll.height;
}
return stream;
}
template <typename T>
@ -133,7 +165,7 @@ struct TableRow {
using DelimiterArray = std::array<std::string, sizeof...(Pack)+1>;
TableRow(CellWidths&& widths_, DelimiterArray&& delims_, const Pack&... vals_)
: _widths(widths_), _delimiters(delims_) {
: widths(widths_), delimiters(delims_), topBorder("") {
nb::ForEach(_cells, GetTallestCell, _height);
}
@ -145,14 +177,22 @@ struct TableRow {
return (_height=newh);
}
const CellWidths widths;
const DelimiterArray delimiters;
const std::string topBorder;
const std::string bottomBorder;
protected:
const CellWidths _widths;
const DelimiterArray _delimiters;
RowCells _cells;
int _height;
};
template <typename... Pack>
std::ostream& operator<<(std::ostream& stream, const TableRow<Pack...>& row) {
}
template<typename... Pack>
class Table {
public:
@ -164,20 +204,6 @@ private:
} // namespace nb
template<typename Type, typename U>
std::ostream& operator<<(std::ostream& stream, const nb::StyleFormatter<Type, U>& msg) {
if (&stream == &std::cout) {
stream << msg.fmt;
if (msg.HasChild) {
stream << msg.val << msg.closing;
}
} else {
if (msg.HasChild) { stream << msg.val; }
}
stream << std::flush;
return stream;
}
#ifdef _NB_TARGET_WINDOWS
template<typename... Types>
std::ostream& operator<<(std::ostream& stream, const nb::TableRow<Types...>& row) {
@ -189,6 +215,6 @@ std::ostream& operator<<(std::ostream& stream, const nb::TableRow<Types...>& row
return stream;
}
#endif // _NB_TARGET_WINDOWS
#endif // _NB_TARGET_WINDOWS */
#endif // _NB_ANSI_TERM

View File

@ -3,6 +3,7 @@ include_directories(./.)
add_library(NBCore
./src/Errors.cpp
./src/Processes.cpp
./src/Logger.cpp
)
if (NB_BUILD_TESTS)

View File

@ -8,13 +8,13 @@
#include <ostream>
#include <thread>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include "ANSITerm.hpp"
#include "DataSink.hpp"
#include "Processes.hpp"
#include "ThreadSafeQueue.hpp"
#include "TypeTraits.hpp"
namespace nb {
@ -62,48 +62,6 @@ private:
LoggerType* type_ptr = static_cast<LoggerType*>(this);
};
template <typename T, typename... Pack>
struct PackIsType;
template <typename T, typename PackType>
struct PackIsType<T, PackType> {
const bool value = std::is_base_of<T, PackType>::value;
};
template <typename T, typename FirstPack, typename... Pack>
struct PackIsType<T, FirstPack, Pack...> {
const bool value = std::is_base_of<T, FirstPack>::value && PackIsType<T, Pack...>::value;
};
template <typename T, typename Pack1, typename... Pack>
struct StreamRefToPtrVec {
StreamRefToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
: vec(_vec) {
static_assert(std::is_base_of<T, Pack1>::value);
vec.push_back(&p1);
vec = StreamRefToPtrVec<T, Pack...>(vec, packs...).vec;
}
StreamRefToPtrVec(Pack1& p1, Pack&... packs)
: StreamRefToPtrVec({}, p1, packs...) {}
std::vector<T*> vec;
};
template <typename T, typename Pack>
struct StreamRefToPtrVec<T, Pack> {
StreamRefToPtrVec(std::vector<T*> _vec, Pack& p1)
: vec(_vec) {
static_assert(std::is_base_of<T, Pack>::value);
vec.push_back(&p1);
}
StreamRefToPtrVec(Pack& p1)
: StreamRefToPtrVec({}, p1) {}
std::vector<T*> vec;
};
template <typename LT>
class DebugLogger : public LoggerBase<LogEvent, LT, std::vector<std::ostream*>>{
using StreamType = std::vector<std::ostream*>;
@ -112,7 +70,7 @@ class DebugLogger : public LoggerBase<LogEvent, LT, std::vector<std::ostream*>>{
public:
template<typename... ST>
DebugLogger(ST&... streams) : _ostream(StreamRefToPtrVec<std::ostream, ST...>(streams...).vec) {}
DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec<std::ostream, ST...>(streams...).vec) {}
~DebugLogger() { type_ptr->stop(); }
@ -152,12 +110,9 @@ private:
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
using LoggerType = DefaultDebugLogger;
using Base = DebugLogger<DefaultDebugLogger>;
template <typename U>
struct Field {
Field(int w, U v) : width{w}, val(v) {}
int width;
U val;
};
template <typename... Ts>
struct LogRow;
public:
using Base::Base;
@ -166,36 +121,23 @@ public:
friend class BufferedDataProcessor<LogEvent, ThreadsafeQueue<LogEvent>, DefaultDebugLogger>;
template <typename STR, typename U>
friend STR& operator<<(STR&, const DefaultDebugLogger::Field<U>&);
template <typename STR, typename... Ts>
friend STR& operator<<(STR&, const DefaultDebugLogger::LogRow<Ts...>&);
protected:
using Base::_ostream;
bool process(const LogEvent& msg) {
for (const auto os : this->_ostream) {
(*os) << "[ " << ANSITextColor<std::string>("hey") << " | "
<< Field(2, msg.tid) << " | " << Field(10, msg.msg) << " ]\n";
*os << msg.lvl << "\t|\t" << msg.msg << "\n";
}
return true;
}
};
template <typename STR, typename U>
STR& operator<<(STR& stream, const DefaultDebugLogger::Field<U>& field) {
stream << std::setw(field.width) << field.val;
return stream;
}
extern DefaultDebugLogger logger;
/* class BasicLogger : public LoggerBase<BasicLogger> {
public:
using Base = LoggerBase<BasicLogger>;
BasicLogger(std::ostream& stream) : Base(stream) {}
virtual void process(const LogEvent& event) {
*_ostreams[0] << event.msg << "\n";
}
}; */
// Taking Charge of Adult ADHD by Russell Barkley
} // namespace nb
#endif // _NB_LOGGER

View File

@ -28,6 +28,48 @@ ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
ForEach<N+1, Func, Args, Pack...>(tup, f, arg);
}
template <typename T, typename Pack1, typename... Pack>
struct RefPackToPtrVec {
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
: vec(_vec) {
static_assert(std::is_base_of<T, Pack1>::value);
vec.push_back(&p1);
vec = RefPackToPtrVec<T, Pack...>(vec, packs...).vec;
}
RefPackToPtrVec(Pack1& p1, Pack&... packs)
: RefPackToPtrVec({}, p1, packs...) {}
std::vector<T*> vec;
};
template <typename T, typename Pack>
struct RefPackToPtrVec<T, Pack> {
RefPackToPtrVec(std::vector<T*> _vec, Pack& p1)
: vec(_vec) {
static_assert(std::is_base_of<T, Pack>::value);
vec.push_back(&p1);
}
RefPackToPtrVec(Pack& p1)
: RefPackToPtrVec({}, p1) {}
std::vector<T*> vec;
};
template <typename T, typename... Pack>
struct PackIsSameType;
template <typename T, typename PackType>
struct PackIsSameType<T, PackType> {
const bool value = std::is_base_of<T, PackType>::value;
};
template <typename T, typename FirstPack, typename... Pack>
struct PackIsSameType<T, FirstPack, Pack...> {
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
};
} // namespace nb
#endif // _NB_TYPE_TRAITS

View File

@ -0,0 +1,8 @@
#include "Logger.hpp"
namespace nb {
nb::DefaultDebugLogger logger;
//logger.run();
} // namespace nb

View File

@ -32,13 +32,8 @@ TEST(ErrorTest, Test) {
EXPECT_EQ(1, 1);
std::stringstream sstream;
nb::DefaultDebugLogger logr(std::cout, sstream);
ASSERT_TRUE(logr.run());
ASSERT_TRUE(logr.isRunning());
while (!logr.isRunning()){
1+1;
}
logr.log("Hey!");
logr.stop();
ASSERT_TRUE(nb::logger.isRunning());
nb::logger.log("Hey!");
std::cout << "STRINGSTREAM:\n" << sstream.str();
}