diff options
| author | Dan Goodliffe <dan.goodliffe@octal.co.uk> | 2026-05-20 17:03:23 +0100 |
|---|---|---|
| committer | Dan Goodliffe <dan.goodliffe@octal.co.uk> | 2026-05-20 17:04:30 +0100 |
| commit | bca5ac479de5721e354b70d627fb9654bd94bdee (patch) | |
| tree | 0d2b011cd3b24fcfba8cbb50df1283ca68434ca1 /src | |
| parent | 161f32ebaea5988fe97ad03ad669eca2222e2a54 (diff) | |
| download | webstat-bca5ac479de5721e354b70d627fb9654bd94bdee.tar.bz2 webstat-bca5ac479de5721e354b70d627fb9654bd94bdee.tar.xz webstat-bca5ac479de5721e354b70d627fb9654bd94bdee.zip | |
Switch to std::fstream and std::print for parkLogLines
Diffstat (limited to 'src')
| -rw-r--r-- | src/ingestor.cpp | 20 | ||||
| -rw-r--r-- | src/util.cpp | 11 | ||||
| -rw-r--r-- | src/util.hpp | 4 |
3 files changed, 26 insertions, 9 deletions
diff --git a/src/ingestor.cpp b/src/ingestor.cpp index 200397b..d15940c 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -5,7 +5,9 @@ #include <connection.h> #include <csignal> #include <dbTypes.h> +#include <fstream> #include <modifycommand.h> +#include <print> #include <ranges> #include <scn/scan.h> #include <selectcommand.h> @@ -451,18 +453,18 @@ namespace WebStat { if (lines.empty()) { return std::unexpected(0); } - const std::filesystem::path path { - settings.fallbackDir / std::format("parked-{:s}.short", makeHash(lines.front()) | TO_HEX_RANGE)}; - if (auto parked = FilePtr(fopen(path.c_str(), "w"))) { - fprintf(parked.get(), "%zu\n", lines.size()); + const std::filesystem::path path + = settings.fallbackDir / std::format("parked-{:s}.short", makeHash(lines.front()) | TO_HEX_RANGE); + if (std::ofstream parked {path}; parked.good()) { + std::println(parked, "{}", lines.size()); for (const auto & line : lines) { - fprintf(parked.get(), "%.*s\n", static_cast<int>(line.length()), line.data()); + std::println(parked, "{}", line); } - if (fflush(parked.get()) == 0) { + if (parked.flush().good()) { lines.clear(); - auto finalPath = std::filesystem::path {path}.replace_extension(".log"); - parked.reset(); - if (rename(path.c_str(), finalPath.c_str()) == 0) { + auto finalPath = auto {path}.replace_extension(".log"); + parked.close(); + if (!renameNoExcept(path, finalPath)) { return finalPath; } } diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000..cdce30e --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,11 @@ +#include "util.hpp" + +namespace WebStat { + std::error_code + renameNoExcept(const std::filesystem::path & path, const std::filesystem::path & finalPath) noexcept + { + std::error_code error; + std::filesystem::rename(path, finalPath, error); + return error; + } +} diff --git a/src/util.hpp b/src/util.hpp index 5cac5a3..d5e2482 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -2,6 +2,7 @@ #include <chrono> #include <command.h> +#include <filesystem> #include <scn/scan.h> #include <shared_mutex> #include <tuple> @@ -144,4 +145,7 @@ namespace WebStat { ValueType value; mutable MutexType mutex; }; + + std::error_code renameNoExcept( + const std::filesystem::path & path, const std::filesystem::path & finalPath) noexcept; } |
