Adding a general logger and integrating it with daggyr
This commit is contained in:
71
libdaggy/include/daggy/GeneralLogger.hpp
Normal file
71
libdaggy/include/daggy/GeneralLogger.hpp
Normal 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
|
||||
Reference in New Issue
Block a user