Adding a general logger and integrating it with daggyr

This commit is contained in:
Ian Roddis
2022-02-04 11:58:45 -04:00
parent 57e93b5045
commit ea3f67f226
8 changed files with 245 additions and 20 deletions

View File

@@ -0,0 +1,71 @@
#pragma once
#include <atomic>
#include <condition_variable>
#include <deque>
#include <functional>
#include <mutex>
#include <string>
#include "Defines.hpp"
namespace daggy {
BETTER_ENUM(LogLevel, int, NONE = 0, ERROR, WARN, INFO, DEBUG);
class GeneralLogger
{
public:
explicit GeneralLogger(std::ostream &os, LogLevel level = LogLevel::WARN);
~GeneralLogger();
void setLevel(LogLevel level);
void log(const std::string &msg, LogLevel level);
// Function will only be called if required
void log(const std::function<std::string()> &fun, LogLevel level);
template <typename T>
void error(const T &msg)
{
log(msg, LogLevel::ERROR);
}
template <typename T>
void warn(const T &msg)
{
log(msg, LogLevel::WARN);
}
template <typename T>
void info(const T &msg)
{
log(msg, LogLevel::INFO);
}
template <typename T>
void debug(const T &msg)
{
log(msg, LogLevel::DEBUG);
}
void shutdown();
private:
void emitMessages();
struct LogMessage
{
LogLevel level;
std::string msg;
TimePoint time;
};
std::atomic<bool> running_;
std::ostream &os_;
LogLevel level_;
std::condition_variable newMessage_;
std::mutex messageGuard_;
std::deque<LogMessage> messages_;
std::thread messageEmiter_;
};
} // namespace daggy