64 lines
1.5 KiB
C++
64 lines
1.5 KiB
C++
#pragma once
|
|
#ifndef _NB_LOGGER
|
|
#define _NB_LOGGER
|
|
|
|
#include <chrono>
|
|
#include <ostream>
|
|
#include <thread>
|
|
#include <unordered_map>
|
|
|
|
#include "SmartStreams.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;
|
|
};
|
|
|
|
template<typename T>
|
|
class Logger {
|
|
typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&);
|
|
typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
|
|
public:
|
|
Logger();
|
|
|
|
bool isRunning() const;
|
|
unsigned int count() const;
|
|
|
|
bool run();
|
|
bool stop();
|
|
LogEvent popProcess();
|
|
LogEvent popDelete();
|
|
void flush();
|
|
void deleteAll();
|
|
void msg(const std::string&, uint8_t lvl=0);
|
|
void warn(const std::string&, uint8_t lvl=1);
|
|
void warn(const std::exception&, uint8_t lvl=1);
|
|
void error(const std::string&, uint8_t lvl=0xFF);
|
|
void error(const std::exception&, uint8_t lvl=0xFF);
|
|
|
|
|
|
protected:
|
|
virtual void process(const LogEvent&);
|
|
|
|
const LogProcessFunctionMap _function_map;
|
|
|
|
ThreadsafeQueue<LogEvent> _queue;
|
|
T* _ostream;
|
|
// std::condition_variable _cond;
|
|
// mutable std::mutex _mutex;
|
|
mutable std::mutex _runningMutex;
|
|
bool _running;
|
|
std::shared_ptr<std::thread> _runningThread = nullptr;
|
|
};
|
|
|
|
} // namespace nb
|
|
#endif // _NB_LOGGER
|