diff options
Diffstat (limited to 'src/zenutil/include')
| -rw-r--r-- | src/zenutil/include/zenutil/logging/rotatingfilesink.h | 69 |
1 files changed, 10 insertions, 59 deletions
diff --git a/src/zenutil/include/zenutil/logging/rotatingfilesink.h b/src/zenutil/include/zenutil/logging/rotatingfilesink.h index f28e908eb..e4a99fc30 100644 --- a/src/zenutil/include/zenutil/logging/rotatingfilesink.h +++ b/src/zenutil/include/zenutil/logging/rotatingfilesink.h @@ -20,14 +20,11 @@ class RotatingFileSink : public spdlog::sinks::sink { public: RotatingFileSink(const std::filesystem::path& BaseFilename, std::size_t MaxSize, std::size_t MaxFiles, bool RotateOnOpen = false) - : m_BasePath(BaseFilename.parent_path()) - , m_Stem(BaseFilename.stem().string()) - , m_Extension(BaseFilename.extension().string()) + : m_BaseFilename(BaseFilename) , m_MaxSize(MaxSize) , m_MaxFiles(MaxFiles) { - std::filesystem::path RootFileName = GetFileName(0); - std::error_code Ec; + std::error_code Ec; if (RotateOnOpen) { RwLock::ExclusiveLockScope RotateLock(m_Lock); @@ -35,7 +32,7 @@ public: } else { - m_CurrentFile.Open(RootFileName, BasicFile::Mode::kWrite, Ec); + m_CurrentFile.Open(m_BaseFilename, BasicFile::Mode::kWrite, Ec); if (!Ec) { m_CurrentSize = m_CurrentFile.FileSize(Ec); @@ -52,7 +49,7 @@ public: if (Ec) { - throw std::system_error(Ec, fmt::format("Failed to open log file '{}'", RootFileName.string())); + throw std::system_error(Ec, fmt::format("Failed to open log file '{}'", m_BaseFilename.string())); } } @@ -151,57 +148,22 @@ public: } private: - static bool IsEmpty(const std::filesystem::path& Path, std::error_code& Ec) - { - bool Exists = std::filesystem::exists(Path, Ec); - if (Ec) - { - return false; - } - if (!Exists) - { - return true; - } - uintmax_t Size = std::filesystem::file_size(Path, Ec); - if (Ec) - { - return false; - } - return Size == 0; - } - void Rotate(RwLock::ExclusiveLockScope&, std::error_code& OutEc) { m_CurrentFile.Close(); - bool BaseIsEmpty = IsEmpty(GetFileName(0), OutEc); + + OutEc = RotateFiles(m_BaseFilename, m_MaxFiles); if (OutEc) { return; } - if (!BaseIsEmpty) - { - // We try our best to rotate the logs, if we fail we fail and will try to open the base log file anyway - for (auto i = m_MaxFiles; i > 0; i--) - { - std::filesystem::path src = GetFileName(i - 1); - if (!std::filesystem::exists(src)) - { - continue; - } - std::error_code DummyEc; - std::filesystem::path target = GetFileName(i); - if (std::filesystem::exists(target, DummyEc)) - { - std::filesystem::remove(target, DummyEc); - } - std::filesystem::rename(src, target, DummyEc); - } - } - m_CurrentFile.Open(GetFileName(0), BasicFile::Mode::kWrite, OutEc); + + m_CurrentFile.Open(m_BaseFilename, BasicFile::Mode::kWrite, OutEc); if (OutEc) { return; } + // If we fail to rotate, try extending the current log file m_CurrentSize = m_CurrentFile.FileSize(OutEc); } @@ -252,19 +214,8 @@ private: return true; } - std::filesystem::path GetFileName(size_t Index) const - { - if (Index == 0) - { - return m_BasePath / (m_Stem + m_Extension); - } - return m_BasePath / fmt::format("{}.{}{}", m_Stem, Index, m_Extension); - } - RwLock m_Lock; - const std::filesystem::path m_BasePath; - const std::string m_Stem; - const std::string m_Extension; + const std::filesystem::path m_BaseFilename; std::unique_ptr<spdlog::formatter> m_Formatter; std::atomic_size_t m_CurrentSize; const std::size_t m_MaxSize; |