NBEngine/engine/NBCore/Logger.hpp
2026-04-04 15:01:39 -05:00

143 lines
3.6 KiB
C++

#pragma once
#ifndef _NB_LOGGER
#define _NB_LOGGER
#include <atomic>
#include <chrono>
#include <iomanip>
#include <ostream>
#include <thread>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include "DataSink.hpp"
#include "Processes.hpp"
#include "ThreadSafeQueue.hpp"
#include "TypeTraits.hpp"
namespace nb {
typedef std::chrono::time_point<
std::chrono::system_clock,
std::chrono::nanoseconds
> 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::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
template<typename LogType, typename Logger, typename ST=std::ostream*>
class LoggerBase
: public MultithreadedDataProcessor<LogType, Logger>{
using StreamType = ST;
using LoggerType = Logger;
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
public:
bool run() {
if (!type_ptr->isRunning()) {
this->_running = true;
this->_runningThread = std::make_shared<std::thread>([&]{
while(type_ptr->isRunning()) {
type_ptr->flush();
}
type_ptr->flush();
});
}
return type_ptr->isRunning();
}
protected:
LoggerBase() = default;
StreamType _ostream;
private:
LoggerType* type_ptr = static_cast<LoggerType*>(this);
};
template <typename LT>
class DebugLogger : public LoggerBase<LogEvent, LT, std::vector<std::ostream*>>{
using StreamType = std::vector<std::ostream*>;
using LoggerType = LT;
using Base = LoggerBase<LogEvent, LoggerType, StreamType>;
public:
template<typename... ST>
DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec<std::ostream, ST...>(streams...).vec) {}
~DebugLogger() { type_ptr->stop(); }
void log(const std::string& msg, const uint8_t& lvl=0xFF) {
type_ptr->push(LogEvent{
std::chrono::system_clock::now(),
lvl,
msg,
std::this_thread::get_id(),
GetPID(),
});
}
template <size_t N>
void log(char const(&msg) [N], const uint8_t& lvl=0xFF) {
type_ptr->log(std::string(msg), lvl);
}
void log(const std::exception& err, const uint8_t& lvl=0xFF) {
type_ptr->log(err.what(), lvl);
}
template<typename U>
void warn(const U& val, const uint8_t& lvl=0x01) { type_ptr->log(val, lvl); }
template<typename U>
void error(const U& val) { type_ptr->log(val, 0xFF); }
protected:
std::vector<std::ostream*> _ostream;
private:
LoggerType* type_ptr = static_cast<LoggerType*>(this);
};
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
using LoggerType = DefaultDebugLogger;
using Base = DebugLogger<DefaultDebugLogger>;
template <typename... Ts>
struct LogRow;
public:
using Base::Base;
~DefaultDebugLogger() { stop(); }
friend class BufferedDataProcessor<LogEvent, ThreadsafeQueue<LogEvent>, DefaultDebugLogger>;
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 << msg.lvl << "\t|\t" << msg.msg << "\n";
}
return true;
}
};
extern DefaultDebugLogger logger;
// Taking Charge of Adult ADHD by Russell Barkley
} // namespace nb
#endif // _NB_LOGGER