NBEngine/engine/NBCore/Logger.hpp
2026-01-19 04:05:26 -06:00

148 lines
3.4 KiB
C++

#pragma once
#ifndef _NB_LOGGER
#define _NB_LOGGER
#include <atomic>
#include <chrono>
#include <ostream>
#include <thread>
#include <unordered_map>
#include "Processes.hpp"
#include "ThreadSafeQueue.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 LoggerName>
class Logger {
public:
using Type = LoggerName;
Logger(std::ostream& stream) : _ostream(&stream) {}
bool isRunning() const {
return _running && bool(_runningThread);
}
unsigned int count() const {
return _queue.size();
}
bool run() {
auto this_type = static_cast<Type*>(this);
if (!isRunning()) {
_runningThread = std::make_shared<std::thread>([&]{
while(this_type->isRunning()) {
this_type->flush();
}
});
}
return isRunning();
}
bool stop() {
if (isRunning()) {
_running = false;
_runningThread->join();
_runningThread = nullptr;
flush();
}
return isRunning();
}
LogEvent pop() {
std::shared_ptr<LogEvent> event;
_queue.pop(event);
static_cast<Type*>(this)->process(*event);
return *event;
}
void flush() {
while(_queue.size()) {
pop();
}
}
void deleteAll() {
_queue.empty();
}
template <typename T>
void log(const T&, const uint8_t);
void log(const std::string& msg, const uint8_t& lvl) {
_queue.push(LogEvent{
std::chrono::system_clock::now(),
lvl,
msg,
std::this_thread::get_id(),
get_pid(),
});
}
void log(const std::exception& err, const uint8_t& lvl) {
_queue.push(LogEvent{
std::chrono::system_clock::now(),
lvl,
err.what(),
std::this_thread::get_id(),
get_pid(),
});
}
void msg(const std::string& msg, const uint8_t& lvl=0x00) {
static_cast<Type>(this)->log(msg, lvl);
}
void warn(const std::string& msg, const uint8_t& lvl=0x01) {
static_cast<Type>(this)->log(msg, lvl);
}
void warn(const std::exception& err, const uint8_t lvl=0x01) {
static_cast<Type>(this)->log(err, lvl);
}
void error(const std::string& msg, const uint8_t lvl=0xFF) {
static_cast<Type>(this)->log(msg, lvl);
}
void error(const std::exception& err, const uint8_t lvl=0xFF) {
static_cast<Type*>(this)->log(err, lvl);
}
protected:
void process(const LogEvent&);
ThreadsafeQueue<LogEvent> _queue;
std::ostream* const _ostream;
std::atomic<bool> _running;
std::shared_ptr<std::thread> _runningThread = nullptr;
};
class BasicLogger : public Logger<BasicLogger> {
public:
using Base = Logger<BasicLogger>;
BasicLogger(std::ostream& stream) : Base(stream) {}
virtual void process(const LogEvent& event) {
*_ostream << event.msg << "\n";
}
};
} // namespace nb
#endif // _NB_LOGGER