38 lines
1.2 KiB
C
Raw Permalink Normal View History

2019-06-26 22:11:22 +02:00
#pragma once
#include <spdlog/logger.h>
2019-11-23 21:16:55 +01:00
#include <spdlog/sinks/base_sink.h>
2019-06-26 22:11:22 +02:00
namespace logger {
2020-01-24 02:49:59 +01:00
class ColorCodeFormatter : public spdlog::formatter {
public:
void format(const spdlog::details::log_msg &msg, spdlog::memory_buf_t &dest) override {
dest.append(msg.payload.begin(), msg.payload.end());
}
2019-11-23 21:16:55 +01:00
2020-01-24 02:49:59 +01:00
[[nodiscard]] std::unique_ptr<formatter> clone() const override {
return std::unique_ptr<ColorCodeFormatter>();
}
};
2019-11-23 21:16:55 +01:00
//TODO: Mutex really needed here?
2020-01-24 02:49:59 +01:00
class TerminalSink : public spdlog::sinks::base_sink<std::mutex> {
2019-06-26 22:11:22 +02:00
public:
2020-01-24 02:49:59 +01:00
void sink_it_(const spdlog::details::log_msg &msg) override;
void flush_() override;
2019-06-26 22:11:22 +02:00
};
2020-01-24 02:49:59 +01:00
class LogFormatter : public spdlog::formatter {
public:
explicit LogFormatter(bool colored) : _colored{colored} {}
2019-06-26 22:11:22 +02:00
2020-01-24 02:49:59 +01:00
void format(const spdlog::details::log_msg &msg, spdlog::memory_buf_t &dest) override;
[[nodiscard]] std::unique_ptr<formatter> clone() const override;
2019-06-26 22:11:22 +02:00
2020-01-24 02:49:59 +01:00
inline bool colored() const { return this->_colored; }
inline void colored(bool flag) { this->_colored = flag; }
private:
bool _colored{true};
};
2019-06-26 22:11:22 +02:00
}