63 lines
1.3 KiB
C++
63 lines
1.3 KiB
C++
#ifndef _LOGGER_H_
|
|
#define _LOGGER_H_
|
|
|
|
#include "log_queue.h"
|
|
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
#include <iostream>
|
|
#include <atomic>
|
|
#include <functional>
|
|
|
|
class logger
|
|
{
|
|
private:
|
|
std::atomic<bool> _enabled;
|
|
std::stringstream _logLine;
|
|
log_queue _queue;
|
|
|
|
public:
|
|
logger();
|
|
|
|
void set_enabled(bool enabled);
|
|
|
|
template<typename... Args> void log(const char* s, const Args&... args)
|
|
{
|
|
//const long time = std::chrono::high_resolution_clock::now().time_since_epoch().count();
|
|
|
|
if(_enabled)
|
|
{
|
|
const std::function<void()>& func = std::bind(&logger::log_impl<Args...>, this, s, args...);
|
|
_queue.add_log(func);
|
|
}
|
|
}
|
|
|
|
private:
|
|
|
|
template<typename... Args> void log_impl(const char* s, const Args&... args)
|
|
{
|
|
_logLine.str("");
|
|
log_internal(s, args...);
|
|
}
|
|
|
|
void log_internal(const char* s);
|
|
|
|
template<typename T, typename... Args> void log_internal(const char* s, const T& value, const Args&... args)
|
|
{
|
|
while (*s)
|
|
{
|
|
if (*s == '%' && *++s != '%')
|
|
{
|
|
_logLine << value;
|
|
return log_internal(s, args...);
|
|
}
|
|
_logLine << *s++;
|
|
}
|
|
throw std::runtime_error("extra arguments provided");
|
|
}
|
|
};
|
|
|
|
extern logger ulog;
|
|
|
|
#endif // _LOGGER_H_
|