#include #include #ifndef _WIN32 #include #include #include #include #endif #include #include #include #include #include #include #include #include namespace Void::Logging::Detail { namespace { std::tm to_utc_tm(std::time_t tt) { std::tm tm{}; #if defined(_WIN32) gmtime_s(&tm, &tt); #else gmtime_r(&tt, &tm); #endif return tm; } } // namespace std::string format_timestamp(std::chrono::system_clock::time_point tp) { using namespace std::chrono; const auto since_epoch = tp.time_since_epoch(); const auto secs = duration_cast(since_epoch); const auto us = duration_cast(since_epoch - secs); const std::tm tm = to_utc_tm(system_clock::to_time_t(tp)); return std::format("{:04d}{:02d}{:02d}-{:02d}{:02d}{:02d}-{:06d}", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, static_cast(us.count())); } RollingFile::RollingFile(std::filesystem::path path, RollingFileOptions opt, bool self_rotate) : m_path(std::move(path)), m_opt(opt), m_self_rotate(self_rotate) { } bool RollingFile::open() { std::error_code ec; if (m_opt.create_directories && m_path.has_parent_path()) { std::filesystem::create_directories(m_path.parent_path(), ec); if (ec) { return false; } } if (std::filesystem::exists(m_path, ec)) { m_current_bytes = static_cast( std::filesystem::file_size(m_path, ec)); } else { m_current_bytes = 0; } m_file.clear(); m_file.open(m_path, std::ios::app); m_creationTime = std::chrono::system_clock::now(); return m_file.is_open(); } bool RollingFile::reopen() { if (m_file.is_open()) { m_file.flush(); m_file.close(); } m_file.clear(); return open(); } void RollingFile::write(std::string_view line) { if (!m_file.is_open()) { throw std::runtime_error(std::format( "RollingFile: stream closed for '{}'", m_path.string())); } m_file.write(line.data(), static_cast(line.size())); m_file.put('\n'); if (!m_file.good()) { throw std::runtime_error(std::format( "RollingFile: write failure on '{}'", m_path.string())); } m_current_bytes += line.size() + 1; if (m_self_rotate && m_current_bytes >= m_opt.max_bytes) { rotate(); } } void RollingFile::flush() { if (!m_file.is_open()) { return; } m_file.flush(); if (!m_file.good()) { throw std::runtime_error(std::format( "RollingFile: flush failure on '{}'", m_path.string())); } } Void::Result::Result RollingFile::sync() { try { flush(); } catch (const std::exception& e) { return Void::Result::fail( make_log_error(LogErrorCode::io_error, e.what())); } #ifndef _WIN32 const int fd = ::open(m_path.c_str(), O_RDONLY); if (fd < 0) { return Void::Result::fail(make_log_error( LogErrorCode::io_error, std::format("RollingFile::sync open('{}') failed: {}", m_path.string(), std::strerror(errno)))); } int rc = 0; do { rc = ::fsync(fd); } while (rc < 0 && errno == EINTR); const int saved_err = errno; ::close(fd); if (rc < 0) { return Void::Result::fail(make_log_error( LogErrorCode::io_error, std::format("RollingFile::sync fsync('{}') failed: {}", m_path.string(), std::strerror(saved_err)))); } #endif return {}; } void RollingFile::rotate() { m_rotationTime = std::chrono::system_clock::now(); m_file.close(); std::error_code ec; if (m_opt.max_files > 0) { auto parent = m_path.parent_path(); auto history_dir = parent / "history"; std::filesystem::create_directories(history_dir, ec); if (ec) { throw std::runtime_error(std::format( "RollingFile: cannot create history dir '{}'", history_dir.string())); } auto stem = m_path.stem(); auto ext = m_path.extension(); auto new_filename = stem.string() + "-" + format_timestamp(m_rotationTime) + ext.string(); auto archived = history_dir / new_filename; std::filesystem::rename(m_path, archived, ec); prune_archives(); } else { std::filesystem::remove(m_path, ec); } m_file.clear(); m_file.open(m_path, std::ios::trunc); if (!m_file.is_open()) { throw std::runtime_error(std::format( "RollingFile: reopen after rotate failed on '{}'", m_path.string())); } m_current_bytes = 0; m_creationTime = m_rotationTime; } void RollingFile::prune_archives() { auto parent = m_path.parent_path(); auto history_dir = parent / "history"; if (!std::filesystem::exists(history_dir)) { return; } auto stem = m_path.stem().string(); std::vector archives; std::error_code ec; try { for (const auto& entry : std::filesystem::directory_iterator(history_dir, ec)) { if (ec) { return; } auto filename = entry.path().filename().string(); if (filename.starts_with(stem + "-")) { archives.push_back(entry.path()); } } } catch (const std::filesystem::filesystem_error&) { return; } std::sort(archives.begin(), archives.end()); while (archives.size() > m_opt.max_files) { std::filesystem::remove(archives.front(), ec); archives.erase(archives.begin()); } } void RollingFile::snapshot_into(const std::filesystem::path& dir) { m_rotationTime = std::chrono::system_clock::now(); m_file.close(); std::error_code ec; std::filesystem::create_directories(dir, ec); std::filesystem::rename(m_path, dir / m_path.filename(), ec); m_file.clear(); m_file.open(m_path, std::ios::trunc); m_current_bytes = 0; m_creationTime = m_rotationTime; } } // namespace Void::Logging::Detail