59 lines
1.1 KiB
C++
59 lines
1.1 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)
|
|
{
|
|
std::cout << "step 1" << std::endl;
|
|
if(_enabled)
|
|
{
|
|
std::cout << "step 2" << std::endl;
|
|
_queue.add_log([this, s, args...] {
|
|
std::cout << "step 3" << std::endl;
|
|
_logLine.str("");
|
|
log_internal(s, args...);
|
|
});
|
|
}
|
|
}
|
|
|
|
private:
|
|
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_
|