diff options
author | Jose <jose@zeroc.com> | 2016-04-07 19:07:10 +0200 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2016-04-07 19:07:10 +0200 |
commit | 538771b59abc2dedbf163abdc4df282224ae4d18 (patch) | |
tree | 51350d8db53fccfae5c3c9f5d6ff5ffe3b0d99ba /cpp/src/Ice/LoggerI.cpp | |
parent | ICE-7035 - Add option to roll log files (diff) | |
download | ice-538771b59abc2dedbf163abdc4df282224ae4d18.tar.bz2 ice-538771b59abc2dedbf163abdc4df282224ae4d18.tar.xz ice-538771b59abc2dedbf163abdc4df282224ae4d18.zip |
Do not throw if log rotation fails.
If log rotate fails because the file cannot be renamed
we will keep using the same log file or in case that it
cannot be reopen we will log to stderr.
Diffstat (limited to 'cpp/src/Ice/LoggerI.cpp')
-rw-r--r-- | cpp/src/Ice/LoggerI.cpp | 52 |
1 files changed, 40 insertions, 12 deletions
diff --git a/cpp/src/Ice/LoggerI.cpp b/cpp/src/Ice/LoggerI.cpp index 41809daf7b1..c35e84fee18 100644 --- a/cpp/src/Ice/LoggerI.cpp +++ b/cpp/src/Ice/LoggerI.cpp @@ -46,6 +46,11 @@ public: Init init; +// +// Timeout in milliseconds after which rename will be attempted +// in case of failures renaming files. That is set to 5 minutes. +// +const Ice::Long retryTimeout = 5 * 60 * 1000; } Ice::LoggerI::LoggerI(const string& prefix, const string& file, @@ -57,7 +62,8 @@ Ice::LoggerI::LoggerI(const string& prefix, const string& file, #if defined(_WIN32) && !defined(ICE_OS_WINRT) _consoleConverter(new IceUtil::WindowsStringConverter(GetConsoleOutputCP())), #endif - _sizeMax(sizeMax) + _sizeMax(sizeMax), + _nextRetry(0) { if(!prefix.empty()) { @@ -72,6 +78,11 @@ Ice::LoggerI::LoggerI(const string& prefix, const string& file, { throw InitializationException(__FILE__, __LINE__, "FileLogger: cannot open " + _file); } + + if(_sizeMax > 0) + { + _out.seekp(0, _out.end); + } } } @@ -147,14 +158,13 @@ Ice::LoggerI::write(const string& message, bool indent) { if(_sizeMax > 0) { - _out.seekp(0, _out.end); - // // If file size + message size exceed max size we archive the log file, // but we do not archive empty files or truncate messages. // size_t sz = static_cast<size_t>(_out.tellp()); - if(sz > 0 && sz + message.size() >= _sizeMax) + if(sz > 0 && sz + message.size() >= _sizeMax && + (_nextRetry == 0 || _nextRetry <= IceUtil::Time::now().toMilliSeconds())) { string basename = _file; @@ -170,7 +180,7 @@ Ice::LoggerI::write(const string& message, bool indent) int id = 0; string archive; - string date = IceUtil::Time::now().toFormatString("%Y%m%d-%H%M%S"); + string date = IceUtil::Time::now().toString("%Y%m%d-%H%M%S"); while(true) { ostringstream s; @@ -192,17 +202,35 @@ Ice::LoggerI::write(const string& message, bool indent) break; } - int error = IceUtilInternal::rename(_file, archive); - if(error) + int err = IceUtilInternal::rename(_file, archive); + + _out.open(_file, fstream::out | fstream::app); + + if(err) { - throw InitializationException(__FILE__, __LINE__, "FileLogger: cannot rename " + _file + "\n" + - IceUtilInternal::lastErrorToString()); + _nextRetry = retryTimeout + IceUtil::Time::now().toMilliSeconds(); + // + // We temporally set the maximum size to 0 to ensure that there isn't any rename attempts + // in the nested error call. + // + int sizeMax = _sizeMax; + _sizeMax = 0; + sync.release(); + error("FileLogger: cannot rename " + _file + "\n" + IceUtilInternal::lastErrorToString()); + sync.acquire(); + _sizeMax = sizeMax; } - - _out.open(_file, fstream::out | fstream::app); + else if(_nextRetry != 0) + { + _nextRetry = 0; + } + if(!_out.is_open()) { - throw InitializationException(__FILE__, __LINE__, "FileLogger: cannot open " + _file); + sync.release(); + error("FileLogger: cannot open " + _file + " log messages will be send to stderr"); + write(message, indent); + return; } } } |