diff options
author | Dwayne Boone <dwayne@zeroc.com> | 2009-04-03 13:00:44 -0230 |
---|---|---|
committer | Dwayne Boone <dwayne@zeroc.com> | 2009-04-03 13:00:44 -0230 |
commit | 797ff949357d15973f3d1a25567f9e21a6380390 (patch) | |
tree | d5a3d33784d90e10227fcf63eb4261e649bb9a45 /cpp/src/Ice/LoggerI.cpp | |
parent | Bug 3800 - remove unecessary metadata (diff) | |
download | ice-797ff949357d15973f3d1a25567f9e21a6380390.tar.bz2 ice-797ff949357d15973f3d1a25567f9e21a6380390.tar.xz ice-797ff949357d15973f3d1a25567f9e21a6380390.zip |
Bug 3911 - Added ability to write logger output to file
Diffstat (limited to 'cpp/src/Ice/LoggerI.cpp')
-rw-r--r-- | cpp/src/Ice/LoggerI.cpp | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/cpp/src/Ice/LoggerI.cpp b/cpp/src/Ice/LoggerI.cpp index a388cdaab0e..5a3301886ae 100644 --- a/cpp/src/Ice/LoggerI.cpp +++ b/cpp/src/Ice/LoggerI.cpp @@ -7,9 +7,10 @@ // // ********************************************************************** +#include <IceUtil/StaticMutex.h> #include <IceUtil/Time.h> #include <Ice/LoggerI.h> -#include <IceUtil/StaticMutex.h> +#include <Ice/LocalException.h> using namespace std; using namespace Ice; @@ -17,20 +18,36 @@ using namespace IceInternal; static IceUtil::StaticMutex outputMutex = ICE_STATIC_MUTEX_INITIALIZER; -Ice::LoggerI::LoggerI(const string& prefix) +Ice::LoggerI::LoggerI(const string& prefix, const string& file) { if(!prefix.empty()) { _prefix = prefix + ": "; } + + if(!file.empty()) + { + _out.open(file.c_str(), fstream::out | fstream::app); + if(!_out.is_open()) + { + throw InitializationException(__FILE__, __LINE__, "FileLogger: cannot open " + file); + } + } +} + + +Ice::LoggerI::~LoggerI() +{ + if(_out.is_open()) + { + _out.close(); + } } void Ice::LoggerI::print(const string& message) { - IceUtil::StaticMutex::Lock sync(outputMutex); - - cerr << message << endl; + write(message); } void @@ -50,23 +67,32 @@ Ice::LoggerI::trace(const string& category, const string& message) ++idx; } - IceUtil::StaticMutex::Lock sync(outputMutex); - - cerr << s << endl; + write(s); } void Ice::LoggerI::warning(const string& message) { - IceUtil::StaticMutex::Lock sync(outputMutex); - - cerr << IceUtil::Time::now().toDateTime() << " " << _prefix << "warning: " << message << endl; + write(IceUtil::Time::now().toDateTime() + " " + _prefix + "warning: " + message); } void Ice::LoggerI::error(const string& message) { + write(IceUtil::Time::now().toDateTime() + " " + _prefix + "error: " + message); +} + +void +Ice::LoggerI::write(const string& message) +{ IceUtil::StaticMutex::Lock sync(outputMutex); - cerr << IceUtil::Time::now().toDateTime() << " " << _prefix << "error: " << message << endl; + if(_out.is_open()) + { + _out << message << endl; + } + else + { + cerr << message << endl; + } } |