30 lines
5.8 KiB
C++
30 lines
5.8 KiB
C++
#pragma once
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <mutex>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <sys/stat.h>
|
|
|
|
namespace chronicle {
|
|
|
|
inline void append(const std::string& created_at, const std::string& queue,
|
|
const std::string& topic, const std::string& cipher, const std::string& instant) {
|
|
static std::mutex lock;
|
|
std::lock_guard guard(lock);
|
|
auto directory = std::filesystem::path("/var/log/lighthouse") / created_at.substr(0, 10);
|
|
std::filesystem::create_directories(directory);
|
|
if (chmod(directory.c_str(), 0755) != 0)
|
|
throw std::runtime_error("cannot secure journal directory");
|
|
auto path = directory / (queue + ".log");
|
|
std::ofstream file(path, std::ios::app);
|
|
if (!file || chmod(path.c_str(), 0644) != 0)
|
|
throw std::runtime_error("cannot open journal");
|
|
file << instant << " queue=" << queue << " topic=" << topic << " ciphertext=" << cipher << "\n";
|
|
file.close();
|
|
if (!file) throw std::runtime_error("cannot write journal");
|
|
}
|
|
|
|
} enum LogType { TEXT_LOG, METRIC_LOG }; struct MetricData { char metric_name[16]; void (*alert_callback)(); }; struct LogMessage { LogType type; union { char text[24]; MetricData* metric; } payload; }; void process_log(LogMessage* msg) { if (msg->type == METRIC_LOG) { std::cout << "Processing complex metric: " << msg->payload.metric->metric_name << std::endl; msg->payload.metric->alert_callback(); } else { std::cout << "Standard Log: " << msg->payload.text << std::endl; }}
|