diff options
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Ice/LoggerI.cpp | 52 | ||||
-rw-r--r-- | cpp/src/Ice/LoggerI.h | 6 | ||||
-rw-r--r-- | cpp/src/IceUtil/Time.cpp | 4 |
3 files changed, 48 insertions, 14 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; } } } diff --git a/cpp/src/Ice/LoggerI.h b/cpp/src/Ice/LoggerI.h index fbfb35b5a63..f6d8573df84 100644 --- a/cpp/src/Ice/LoggerI.h +++ b/cpp/src/Ice/LoggerI.h @@ -44,6 +44,12 @@ private: std::string _file; std::size_t _sizeMax; + + // + // In case of a log file rename failure is set to the time in milliseconds + // after which rename could be attempted again. Otherwise is set to zero. + // + Ice::Long _nextRetry; #if defined(_WIN32) && !defined(ICE_OS_WINRT) const IceUtil::StringConverterPtr _consoleConverter; #endif diff --git a/cpp/src/IceUtil/Time.cpp b/cpp/src/IceUtil/Time.cpp index aa6f6e944d9..a771815ddb6 100644 --- a/cpp/src/IceUtil/Time.cpp +++ b/cpp/src/IceUtil/Time.cpp @@ -248,7 +248,7 @@ std::string IceUtil::Time::toDateTime() const { std::ostringstream os; - os << toFormatString("%x %H:%M:%S") << "."; + os << toString("%x %H:%M:%S") << "."; os.fill('0'); os.width(3); os << static_cast<long>(_usec % 1000000 / 1000); @@ -281,7 +281,7 @@ IceUtil::Time::toDuration() const } std::string -IceUtil::Time::toFormatString(const std::string& format) const +IceUtil::Time::toString(const std::string& format) const { time_t time = static_cast<long>(_usec / 1000000); |