NBEngine/engine/NBCore/Logger.hpp

103 lines
2.5 KiB
C++

#pragma once
#ifndef _NB_LOGGER
#define _NB_LOGGER
#include <atomic>
#include <chrono>
#include <ostream>
#include <thread>
#include <unordered_map>
#include <vector>
#include "DataSink.hpp"
#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 LogType, typename Logger, typename ST=std::ostream>
class LoggerBase;
template<typename Logger>
class LoggerBase<typename Logger::LogType, Logger, typename Logger::StreamType>
: public MultithreadedDataProcessor<typename Logger::LogType, Logger>{
protected:
typename Logger::StreamType _ostreams;
}; */
class DebugLogger : public MultithreadedDataProcessor<LogEvent, DebugLogger>{
public:
DebugLogger(std::ostream& stream) : _ostream(&stream) {}
~DebugLogger() { stop(); }
virtual bool stop() noexcept override {
auto ret = MultithreadedDataProcessor<LogEvent, DebugLogger>::stop();
flush();
return ret;
}
void log(const std::string& msg, const uint8_t& lvl=0xFF) {
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) {
log(std::string(msg), lvl);
}
void log(const std::exception& err, const uint8_t& lvl=0xFF) {
log(err.what(), lvl);
}
template<typename U>
void warn(const U& val, const uint8_t& lvl=0x01) { log(val, lvl); }
template<typename U>
void error(const U& val, const uint8_t& lvl=0xFF) { log(val, lvl); }
protected:
std::ostream* const _ostream;
virtual bool process(const LogEvent& msg) override {
*_ostream << msg.pid << " - " << msg.tid << " : " << msg.msg << "\n";
return true;
}
};
/* 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";
}
}; */
} // namespace nb
#endif // _NB_LOGGER