summaryrefslogtreecommitdiff
path: root/cpp/src
diff options
context:
space:
mode:
authorJose <jose@zeroc.com>2016-04-07 10:29:11 +0200
committerJose <jose@zeroc.com>2016-04-07 10:29:11 +0200
commit058a0c6c3ee2f059be0d8c7eba940aa31463fa7f (patch)
treea52b7eb37253b8da13db2b7f617f3233b440bbfb /cpp/src
parentFixed issue with IE where accessing the stack from Exception.toString leads t... (diff)
downloadice-058a0c6c3ee2f059be0d8c7eba940aa31463fa7f.tar.bz2
ice-058a0c6c3ee2f059be0d8c7eba940aa31463fa7f.tar.xz
ice-058a0c6c3ee2f059be0d8c7eba940aa31463fa7f.zip
ICE-7035 - Add option to roll log files
Diffstat (limited to 'cpp/src')
-rw-r--r--cpp/src/Ice/Instance.cpp4
-rw-r--r--cpp/src/Ice/LoggerI.cpp72
-rw-r--r--cpp/src/Ice/LoggerI.h4
-rw-r--r--cpp/src/Ice/PropertyNames.cpp3
-rw-r--r--cpp/src/Ice/PropertyNames.h2
-rw-r--r--cpp/src/IceUtil/Time.cpp38
6 files changed, 98 insertions, 25 deletions
diff --git a/cpp/src/Ice/Instance.cpp b/cpp/src/Ice/Instance.cpp
index 75912ec0c10..c08a021c02f 100644
--- a/cpp/src/Ice/Instance.cpp
+++ b/cpp/src/Ice/Instance.cpp
@@ -1230,7 +1230,9 @@ IceInternal::Instance::Instance(const CommunicatorPtr& communicator, const Initi
#endif
if(!logfile.empty())
{
- _initData.logger = new LoggerI(_initData.properties->getProperty("Ice.ProgramName"), logfile);
+ _initData.logger =
+ new LoggerI(_initData.properties->getProperty("Ice.ProgramName"), logfile, true, 0,
+ _initData.properties->getPropertyAsIntWithDefault("Ice.LogFile.SizeMax", 0));
}
else
{
diff --git a/cpp/src/Ice/LoggerI.cpp b/cpp/src/Ice/LoggerI.cpp
index 623ed3403ff..41809daf7b1 100644
--- a/cpp/src/Ice/LoggerI.cpp
+++ b/cpp/src/Ice/LoggerI.cpp
@@ -9,11 +9,11 @@
#include <IceUtil/Time.h>
#include <Ice/LoggerI.h>
+#include <IceUtil/StringUtil.h>
#include <IceUtil/Mutex.h>
#include <IceUtil/MutexPtrLock.h>
#ifdef _WIN32
-# include <IceUtil/StringUtil.h>
# include <IceUtil/ScopedArray.h>
#endif
@@ -49,14 +49,15 @@ Init init;
}
Ice::LoggerI::LoggerI(const string& prefix, const string& file,
- bool convert, const IceUtil::StringConverterPtr& converter) :
+ bool convert, const IceUtil::StringConverterPtr& converter,
+ size_t sizeMax) :
_prefix(prefix),
_convert(convert),
- _converter(converter)
+ _converter(converter),
#if defined(_WIN32) && !defined(ICE_OS_WINRT)
- , _consoleConverter(new IceUtil::WindowsStringConverter(GetConsoleOutputCP()))
+ _consoleConverter(new IceUtil::WindowsStringConverter(GetConsoleOutputCP())),
#endif
-
+ _sizeMax(sizeMax)
{
if(!prefix.empty())
{
@@ -144,6 +145,67 @@ Ice::LoggerI::write(const string& message, bool indent)
if(_out.is_open())
{
+ 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)
+ {
+
+ string basename = _file;
+ string ext;
+
+ size_t i = basename.rfind(".");
+ if(i != string::npos && i + 1 < basename.size())
+ {
+ ext = basename.substr(i + 1);
+ basename = basename.substr(0, i);
+ }
+ _out.close();
+
+ int id = 0;
+ string archive;
+ string date = IceUtil::Time::now().toFormatString("%Y%m%d-%H%M%S");
+ while(true)
+ {
+ ostringstream s;
+ s << basename << "-" << date;
+ if(id > 0)
+ {
+ s << "-" << id;
+ }
+ if(!ext.empty())
+ {
+ s << "." << ext;
+ }
+ if(IceUtilInternal::fileExists(s.str()))
+ {
+ id++;
+ continue;
+ }
+ archive = s.str();
+ break;
+ }
+
+ int error = IceUtilInternal::rename(_file, archive);
+ if(error)
+ {
+ throw InitializationException(__FILE__, __LINE__, "FileLogger: cannot rename " + _file + "\n" +
+ IceUtilInternal::lastErrorToString());
+ }
+
+ _out.open(_file, fstream::out | fstream::app);
+ if(!_out.is_open())
+ {
+ throw InitializationException(__FILE__, __LINE__, "FileLogger: cannot open " + _file);
+ }
+ }
+ }
_out << s << endl;
}
else
diff --git a/cpp/src/Ice/LoggerI.h b/cpp/src/Ice/LoggerI.h
index 4ff54d73b41..fbfb35b5a63 100644
--- a/cpp/src/Ice/LoggerI.h
+++ b/cpp/src/Ice/LoggerI.h
@@ -22,7 +22,7 @@ class LoggerI : public Logger
public:
LoggerI(const std::string&, const std::string&, bool convert = true,
- const IceUtil::StringConverterPtr& converter = 0);
+ const IceUtil::StringConverterPtr& converter = 0, std::size_t sizeMax = 0);
~LoggerI();
virtual void print(const std::string&);
@@ -43,7 +43,7 @@ private:
IceUtilInternal::ofstream _out;
std::string _file;
-
+ std::size_t _sizeMax;
#if defined(_WIN32) && !defined(ICE_OS_WINRT)
const IceUtil::StringConverterPtr _consoleConverter;
#endif
diff --git a/cpp/src/Ice/PropertyNames.cpp b/cpp/src/Ice/PropertyNames.cpp
index 9ce120d99b8..69f03cade5f 100644
--- a/cpp/src/Ice/PropertyNames.cpp
+++ b/cpp/src/Ice/PropertyNames.cpp
@@ -6,7 +6,7 @@
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
-// Generated by makeprops.py from file ./config/PropertyNames.xml, Tue Apr 28 22:03:41 2015
+// Generated by makeprops.py from file ./config/PropertyNames.xml, Thu Apr 7 10:23:17 2016
// IMPORTANT: Do not edit this file -- any edits made here will be lost!
@@ -123,6 +123,7 @@ const IceInternal::Property IcePropsData[] =
IceInternal::Property("Ice.IPv4", false, 0),
IceInternal::Property("Ice.IPv6", false, 0),
IceInternal::Property("Ice.LogFile", false, 0),
+ IceInternal::Property("Ice.LogFile.SizeMax", false, 0),
IceInternal::Property("Ice.LogStdErr.Convert", false, 0),
IceInternal::Property("Ice.MessageSizeMax", false, 0),
IceInternal::Property("Ice.Nohup", false, 0),
diff --git a/cpp/src/Ice/PropertyNames.h b/cpp/src/Ice/PropertyNames.h
index c0d5538e51e..d5712c8e2ab 100644
--- a/cpp/src/Ice/PropertyNames.h
+++ b/cpp/src/Ice/PropertyNames.h
@@ -6,7 +6,7 @@
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
-// Generated by makeprops.py from file ./config/PropertyNames.xml, Tue Apr 28 22:03:41 2015
+// Generated by makeprops.py from file ./config/PropertyNames.xml, Thu Apr 7 10:23:17 2016
// IMPORTANT: Do not edit this file -- any edits made here will be lost!
diff --git a/cpp/src/IceUtil/Time.cpp b/cpp/src/IceUtil/Time.cpp
index d792757a9f1..aa6f6e944d9 100644
--- a/cpp/src/IceUtil/Time.cpp
+++ b/cpp/src/IceUtil/Time.cpp
@@ -247,22 +247,8 @@ IceUtil::Time::toMicroSecondsDouble() const
std::string
IceUtil::Time::toDateTime() const
{
- time_t time = static_cast<long>(_usec / 1000000);
-
- struct tm* t;
-#ifdef _WIN32
- t = localtime(&time);
-#else
- struct tm tr;
- localtime_r(&time, &tr);
- t = &tr;
-#endif
-
- char buf[32];
- strftime(buf, sizeof(buf), "%x %H:%M:%S", t);
-
std::ostringstream os;
- os << buf << ".";
+ os << toFormatString("%x %H:%M:%S") << ".";
os.fill('0');
os.width(3);
os << static_cast<long>(_usec % 1000000 / 1000);
@@ -294,6 +280,28 @@ IceUtil::Time::toDuration() const
return os.str();
}
+std::string
+IceUtil::Time::toFormatString(const std::string& format) const
+{
+ time_t time = static_cast<long>(_usec / 1000000);
+
+ struct tm* t;
+#ifdef _WIN32
+ t = localtime(&time);
+#else
+ struct tm tr;
+ localtime_r(&time, &tr);
+ t = &tr;
+#endif
+
+ char buf[32];
+ if(strftime(buf, sizeof(buf), format.c_str(), t) == 0)
+ {
+ return std::string();
+ }
+ return std::string(buf);
+}
+
Time::Time(Int64 usec) :
_usec(usec)
{