From 46527ded56cc44e60a4945d2a6d1cf3816f53ca7 Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Tue, 19 May 2015 14:04:02 +0100 Subject: [PATCH] Limited Windows service log file size to 1MB #4677 Oversized file is renamed to .1 to keep old log files in case needed, but the old file will eventually be overwritten on 2nd recycle --- src/lib/base/log_outputters.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/lib/base/log_outputters.cpp b/src/lib/base/log_outputters.cpp index 8db32f3a..010d79f2 100644 --- a/src/lib/base/log_outputters.cpp +++ b/src/lib/base/log_outputters.cpp @@ -22,6 +22,10 @@ #include +enum EFileLogOutputter { + kFileSizeLimit = 1024 // kb +}; + // // StopLogOutputter // @@ -252,13 +256,27 @@ FileLogOutputter::setLogFilename(const char* logFile) bool FileLogOutputter::write(ELevel level, const char *message) { + bool moveFile = false; + std::ofstream m_handle; m_handle.open(m_fileName.c_str(), std::fstream::app); if (m_handle.is_open() && m_handle.fail() != true) { m_handle << message << std::endl; + + // when file size exceeds limits, move to 'old log' filename. + int p = m_handle.tellp(); + if (p > (kFileSizeLimit * 1024)) { + moveFile = true; + } } m_handle.close(); + if (moveFile) { + String oldLogFilename = synergy::string::sprintf("%s.1", m_fileName.c_str()); + remove(oldLogFilename.c_str()); + rename(m_fileName.c_str(), oldLogFilename.c_str()); + } + return true; }