logic: Delete log files older than a month (issue #676)
This commit is contained in:
+1
-1
@@ -52,9 +52,9 @@ void setupLogging()
|
||||
|
||||
std::shared_ptr<FileLogger> fileLogger = std::make_shared<FileLogger>();
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user