mirror of
https://github.com/0O0o0oOoO00/Alas.git
synced 2026-05-14 15:59:25 +08:00
38 lines
1.1 KiB
C++
38 lines
1.1 KiB
C++
#ifndef LOGGER_HPP
|
|
#define LOGGER_HPP
|
|
|
|
#include <iostream>
|
|
#include <mutex>
|
|
#include <spdlog/spdlog.h>
|
|
#include <spdlog/sinks/base_sink.h>
|
|
#include <httplib.h>
|
|
#include <json/json.h>
|
|
|
|
template <typename Mutex>
|
|
class net_sink : public spdlog::sinks::base_sink<Mutex> {
|
|
public:
|
|
using spdlog::sinks::base_sink<Mutex>::base_sink;
|
|
protected:
|
|
void sink_it_(const spdlog::details::log_msg &msg) override {
|
|
spdlog::memory_buf_t formatted;
|
|
this->formatter_->format(msg, formatted);
|
|
|
|
Json::Value root;
|
|
root["level"] = int(msg.level);
|
|
root["message"] = std::string(msg.payload.data(), msg.payload.size());
|
|
|
|
Json::StreamWriterBuilder writer;
|
|
writer["indentation"] = "";
|
|
std::string json_payload = Json::writeString(writer, root);
|
|
|
|
httplib::Client cli("http://127.0.0.1:7539");
|
|
auto res = cli.Post("/log", json_payload, "application/json");
|
|
}
|
|
void flush_() override {}
|
|
};
|
|
|
|
using net_sink_mt = net_sink<std::mutex>;
|
|
using net_sink_st = net_sink<spdlog::details::null_mutex>;
|
|
|
|
#endif //LOGGER_HPP
|