diff --git a/src/app/main.cpp b/src/app/main.cpp index f155a019..e801ef27 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -52,9 +52,9 @@ void setupLogging() std::shared_ptr fileLogger = std::make_shared(); fileLogger->setLogDirectory(UserPaths::getLogPath().getAbsolute()); - fileLogger->setFileName(FileLogger::generateDatedFileName(L"log")); fileLogger->setLogLevel(Logger::LOG_ALL); + fileLogger->deleteLogFiles(FileLogger::generateDatedFileName(L"log", L"", -30)); logManager->addLogger(fileLogger); } diff --git a/src/lib/utility/file/FileSystem.cpp b/src/lib/utility/file/FileSystem.cpp index f644cd93..348e964c 100644 --- a/src/lib/utility/file/FileSystem.cpp +++ b/src/lib/utility/file/FileSystem.cpp @@ -204,7 +204,8 @@ TimeStamp FileSystem::getLastWriteTime(const FilePath& filePath) bool FileSystem::remove(const FilePath& path) { - const bool ret = boost::filesystem::remove(path.getPath()); + boost::system::error_code ec; + const bool ret = boost::filesystem::remove(path.getPath(), ec); path.recheckExists(); return ret; } diff --git a/src/lib/utility/logging/FileLogger.cpp b/src/lib/utility/logging/FileLogger.cpp index 9548740d..0c836ebe 100644 --- a/src/lib/utility/logging/FileLogger.cpp +++ b/src/lib/utility/logging/FileLogger.cpp @@ -8,18 +8,24 @@ #include "FileSystem.h" #include "utilityString.h" -std::wstring FileLogger::generateDatedFileName(const std::wstring& prefix, const std::wstring& suffix) +std::wstring FileLogger::generateDatedFileName(const std::wstring& prefix, const std::wstring& suffix, int offsetDays) { time_t time; std::time(&time); tm t = *std::localtime(&time); + if (offsetDays != 0) + { + time = mktime(&t) + offsetDays * 24 * 60 * 60; + t = *std::localtime(&time); + } + std::wstringstream filename; if (!prefix.empty()) { - filename << prefix; - filename << L"_"; + filename << prefix << L"_"; } + filename << t.tm_year + 1900 << L"-"; filename << (t.tm_mon < 9 ? L"0" : L"") << t.tm_mon + 1 << L"-"; filename << (t.tm_mday < 10 ? L"0" : L"") << t.tm_mday << L"_"; @@ -29,8 +35,7 @@ std::wstring FileLogger::generateDatedFileName(const std::wstring& prefix, const if (!suffix.empty()) { - filename << L"_"; - filename << suffix; + filename << L"_" << suffix; } return filename.str(); @@ -101,6 +106,17 @@ void FileLogger::setMaxLogFileCount(unsigned int fileCount) m_maxLogFileCount = fileCount; } +void FileLogger::deleteLogFiles(const std::wstring& cutoffDate) +{ + for (const FilePath& file : FileSystem::getFilePathsFromDirectory(m_logDirectory, { L".txt" })) + { + if (file.fileName() < cutoffDate) + { + FileSystem::remove(file); + } + } +} + void FileLogger::updateLogFileName() { if (m_logFileName.empty()) diff --git a/src/lib/utility/logging/FileLogger.h b/src/lib/utility/logging/FileLogger.h index a2f2e09b..add7d4d1 100644 --- a/src/lib/utility/logging/FileLogger.h +++ b/src/lib/utility/logging/FileLogger.h @@ -10,7 +10,7 @@ class FileLogger: public Logger { public: - static std::wstring generateDatedFileName(const std::wstring& prefix = L"", const std::wstring& suffix = L""); + static std::wstring generateDatedFileName(const std::wstring& prefix = L"", const std::wstring& suffix = L"", int offsetDays = 0); FileLogger(); @@ -24,6 +24,8 @@ public: // setting the max log file count to 0 will disable ringlogging void setMaxLogFileCount(unsigned int amount); + void deleteLogFiles(const std::wstring& cutoffDate); + private: void logInfo(const LogMessage& message) override; void logWarning(const LogMessage& message) override;