#include "mologram/Log.hpp" #include #include #include #include #include #include namespace mologram { void Log::addSecret(const std::string& s) { if (s.size() < 4) return; std::lock_guard g(mutex_); secrets_.push_back(s); } std::string Log::scrub(std::string s) const { for (const auto& sec : secrets_) { size_t pos = 0; while ((pos = s.find(sec, pos)) != std::string::npos) { s.replace(pos, sec.size(), ""); pos += 10; } } for (char& c : s) if (c == '\n' || c == '\r') c = ' '; return s; } std::string Log::todayPath() const { std::time_t t = std::time(nullptr); std::tm tm{}; localtime_r(&t, &tm); char buf[48]; std::strftime(buf, sizeof(buf), "mologram-%Y-%m-%d.log", &tm); return dir_ + "/" + buf; } void Log::write(const std::string& level, const std::string& component, const std::string& text) { if (dir_.empty()) return; std::lock_guard g(mutex_); std::error_code ec; std::filesystem::create_directories(dir_, ec); std::time_t t = std::time(nullptr); std::tm tm{}; localtime_r(&t, &tm); char stamp[40]; std::strftime(stamp, sizeof(stamp), "%Y-%m-%dT%H:%M:%S", &tm); std::string line = std::string(stamp) + " " + level + " pid=" + std::to_string((long)::getpid()) + " " + component + ": " + scrub(text) + "\n"; int fd = ::open(todayPath().c_str(), O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0600); if (fd < 0) return; ssize_t n = ::write(fd, line.data(), line.size()); (void)n; ::close(fd); } } // namespace mologram