#pragma once #ifndef _NB_LOGGER #define _NB_LOGGER #include #include #include #include #include #include #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 LogProcessFunctionMap; /* template class LoggerBase; template class LoggerBase : public MultithreadedDataProcessor{ protected: typename Logger::StreamType _ostreams; }; */ class DebugLogger : public MultithreadedDataProcessor{ public: DebugLogger(std::ostream& stream) : _ostream(&stream) {} ~DebugLogger() { stop(); } virtual bool stop() noexcept override { auto ret = MultithreadedDataProcessor::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 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 void warn(const U& val, const uint8_t& lvl=0x01) { log(val, lvl); } template 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 { public: using Base = LoggerBase; BasicLogger(std::ostream& stream) : Base(stream) {} virtual void process(const LogEvent& event) { *_ostreams[0] << event.msg << "\n"; } }; */ } // namespace nb #endif // _NB_LOGGER