diff options
author | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2010-11-13 13:30:50 +0100 |
---|---|---|
committer | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2010-11-14 18:28:19 +0100 |
commit | 27f0bc47672b9d4cfced1140c5b98dcd0c0837b8 (patch) | |
tree | 39bd8402076db1efd9def6a9b824e7e2b590f441 /src/utils | |
parent | 196c1c0bffa8bb594ff0a7442748030f47cc9c58 (diff) | |
download | manaserv-27f0bc47672b9d4cfced1140c5b98dcd0c0837b8.tar.gz manaserv-27f0bc47672b9d4cfced1140c5b98dcd0c0837b8.tar.xz manaserv-27f0bc47672b9d4cfced1140c5b98dcd0c0837b8.zip |
Finally fix the logPerDay log option.
The static std::string date variable had two nasty problems.
Would it have been static or be named 'date',
and it would have not worked right.
I also made the logger add the proper date on the archived log
when changing the current day.
Resolved: TMW-Mantis #530.
Reviewed-by: Jaxad0127.
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/logger.cpp | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/utils/logger.cpp b/src/utils/logger.cpp index 958d2e5..0504195 100644 --- a/src/utils/logger.cpp +++ b/src/utils/logger.cpp @@ -52,6 +52,12 @@ long Logger::mMaxFileSize = 1024; // 1 Mb bool Logger::mSwitchLogEachDay = false; /** Last call date */ static std::string mLastCallDate = ""; +/** + * Old date + * For code simplificatiion, the old Date is kept separate + * from the last call date. + */ +static std::string mOldDate = ""; /** * Gets the current time. @@ -114,11 +120,14 @@ static std::string getCurrentDate() */ bool getDayChanged() { - static std::string date = getCurrentDate(); - if (mLastCallDate != date) + std::string dayDate = getCurrentDate(); + + if (mLastCallDate != dayDate) { + // Keep track of the old date. + mOldDate = mLastCallDate; // Reset the current date for next call. - mLastCallDate = date; + mLastCallDate = dayDate; return true; } return false; @@ -153,7 +162,7 @@ void Logger::setLogFile(const std::string &logFile, bool append) append ? std::ios::app : std::ios::trunc); mFilename = logFile; - mLastCallDate = getCurrentDate(); + mLastCallDate = mOldDate = getCurrentDate(); if (!mLogFile.is_open()) { @@ -212,8 +221,10 @@ void Logger::switchLogs() // Update current filesize long fileSize = mLogFile.tellp(); + bool dayJustChanged = getDayChanged(); + if ((fileSize >= (mMaxFileSize * 1024)) - || (mSwitchLogEachDay && getDayChanged())) + || (mSwitchLogEachDay && dayJustChanged)) { // Close logfile, rename it and open a new one mLogFile.flush(); @@ -222,7 +233,7 @@ void Logger::switchLogs() // Stringify the time, the format is: path/yyyy-mm-dd-n_logFilename. using namespace std; ostringstream os; - os << getCurrentDate(); + os << (dayJustChanged ? mOldDate : getCurrentDate()); int fileNum = 1; ResourceManager::splittedPath filePath = |