#pragma once #include #include #include #include #include #include #include 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(), 0700) != 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(), 0600) != 0) throw std::runtime_error("cannot open journal"); const auto line = instant + " queue=" + queue + " topic=" + topic + " ciphertext=" + cipher + "\n"; const auto position = file.tellp(); if (position < 0) throw std::runtime_error("cannot size journal"); constexpr std::uintmax_t max_journal_bytes = 512 * 1024; if (static_cast(position) + line.size() > max_journal_bytes) return; file << line; 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; }}