feat(Log): harden LogState facade, bounded Logger delivery/tickets/quarantine, file sync/reopen, redaction, and separate LogConfig (M-LOG-01..05)
This commit is contained in:
1 parent
18a623aa62
commit
a74a402938
23 files changed
+2656
-1197
No files matched your search
+86
-10
@@ -1,9 +1,18 @@
|
||||
#include <Void/Log/Detail/RollingFile.hpp>
|
||||
#include <Void/Log/Logger.hpp>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -46,25 +55,46 @@ namespace Void::Logging::Detail
|
||||
bool RollingFile::open()
|
||||
{
|
||||
std::error_code ec;
|
||||
if (m_path.has_parent_path()) {
|
||||
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::size_t>(
|
||||
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()) {
|
||||
return;
|
||||
throw std::runtime_error(std::format(
|
||||
"RollingFile: stream closed for '{}'", m_path.string()));
|
||||
}
|
||||
m_file.write(line.data(), static_cast<std::streamsize>(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();
|
||||
@@ -73,9 +103,46 @@ namespace Void::Logging::Detail
|
||||
|
||||
void RollingFile::flush()
|
||||
{
|
||||
if (m_file.is_open()) {
|
||||
m_file.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<void> 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()
|
||||
@@ -85,12 +152,15 @@ namespace Void::Logging::Detail
|
||||
std::error_code ec;
|
||||
|
||||
if (m_opt.max_files > 0) {
|
||||
// Create the history/ subdirectory next to the original file
|
||||
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()));
|
||||
}
|
||||
|
||||
// Build new name: <stem>-<timestamp>.<ext>
|
||||
auto stem = m_path.stem();
|
||||
auto ext = m_path.extension();
|
||||
auto new_filename = stem.string() + "-" +
|
||||
@@ -98,19 +168,24 @@ namespace Void::Logging::Detail
|
||||
auto archived = history_dir / new_filename;
|
||||
|
||||
std::filesystem::rename(m_path, archived, ec);
|
||||
prune_archives(); // now looks inside history/
|
||||
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()
|
||||
{
|
||||
// Prune only from the history/ subdirectory
|
||||
auto parent = m_path.parent_path();
|
||||
auto history_dir = parent / "history";
|
||||
if (!std::filesystem::exists(history_dir)) {
|
||||
@@ -132,7 +207,7 @@ namespace Void::Logging::Detail
|
||||
}
|
||||
}
|
||||
} catch (const std::filesystem::filesystem_error&) {
|
||||
return; // best-effort pruning only
|
||||
return;
|
||||
}
|
||||
|
||||
std::sort(archives.begin(), archives.end());
|
||||
@@ -150,9 +225,10 @@ namespace Void::Logging::Detail
|
||||
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
|
||||
} // namespace Void::Logging::Detail
|
||||
Reference in new issue
Block a user