00001 /*---------------------------------------------------------------------------- 00002 Name: LogManager.cpp 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Handling the Client data 00006 ----------------------------------------------------------------------------*/ 00007 #include <util/LogManager.h> 00008 #include <util/Global.h> 00009 #include <iostream> 00010 #include <exception> 00011 00012 using namespace std; 00013 00014 namespace org { namespace xmlBlaster { 00015 namespace util { 00016 00020 LogManager::LogManager() : logFactory_(new DefaultLogFactory()) 00021 { 00022 } 00023 00024 LogManager::~LogManager() 00025 { 00026 delete logFactory_; 00027 } 00028 00029 void LogManager::initialize(const PropMap& propMap) 00030 { 00031 if (logFactory_ == 0) { 00032 logFactory_ = new DefaultLogFactory(); 00033 } 00034 logFactory_->initialize(propMap); 00035 } 00036 00037 I_LogFactory& LogManager::getLogFactory(const std::string& /*name*/) const 00038 { 00039 return *logFactory_; 00040 } 00041 00042 void LogManager::setLogFactory(const std::string& /*name*/, I_LogFactory* logFactory) 00043 { 00044 delete logFactory_; 00045 logFactory_ = logFactory; 00046 } 00047 00048 //--------------------DefaultLogFactory implementation ------------------------------------------- 00049 00050 00051 //DefaultLogFactory::DefaultLogFactory() : I_LogFactory() {} 00052 00056 //void DefaultLogFactory::initialize(const PropMap& properties) 00057 //{ 00058 // this->propMap_ = properties; 00059 //} 00060 00064 DefaultLogFactory::~DefaultLogFactory() 00065 { 00066 LogMap::reverse_iterator i; 00067 for(i = logMap_.rbegin(); i != logMap_.rend(); ++i) { 00068 I_Log* log = (*i).second; 00069 delete log; 00070 } 00071 logMap_.clear(); 00072 } 00073 00077 I_Log& DefaultLogFactory::getLog(const string& logName) 00078 { 00079 LogMap::iterator pos = logMap_.find(logName); 00080 if (pos != logMap_.end()) return *((*pos).second); 00081 00082 # ifndef ORG_XMLBLASTER_UTIL_LOGMANAGER // To support standalone compilation (see main() below) 00083 Global& glob = Global::getInstance(); 00084 Log *help = new Log(glob.getProperty(), 0, 0, logName); 00085 help->initialize(); 00086 logMap_.insert(LogMap::value_type(logName, help)); 00087 pos = logMap_.find(logName); 00088 if (pos != logMap_.end()) { 00089 I_Log* log = (*pos).second; 00090 return *log; 00091 } 00092 # endif 00093 00094 std::cerr << "LogManager.cpp getLog(" << logName << ") is not implemented -> throwing exception" << std::endl; 00095 throw bad_exception(); 00096 //throw bad_exception("LogManager.cpp not implemented -> throwing exception"); 00097 } 00098 00102 void DefaultLogFactory::releaseLog(const string& name) 00103 { 00104 std::cerr << "LogManager.cpp releaseLog(" << name << ") is not implemented -> throwing exception" << std::endl; 00105 } 00106 00107 }}} // end of namespace 00108 00109 00110 #ifdef ORG_XMLBLASTER_UTIL_LOGMANAGER 00111 using namespace std; 00112 using namespace org::xmlBlaster::util; 00113 00114 class DummyLog : public I_Log 00115 { public: 00116 void info(const std::string &instance, const std::string &text){ 00117 std::cout << "[INFO] " << instance << ": " << text << std::endl; 00118 } 00119 void warn(const std::string &instance, const std::string &text){ 00120 std::cout << "[WARN] " << instance << ": " << text << std::endl; 00121 } 00122 void error(const std::string &instance, const std::string &text){ 00123 std::cout << "[ERROR] " << instance << ": " << text << std::endl; 00124 } 00125 void trace(const std::string &instance, const std::string &text){ 00126 std::cout << "[TRACE] " << instance << ": " << text << std::endl; 00127 } 00128 void call(const std::string &instance, const std::string &text){ 00129 std::cout << "[CALL] " << instance << ": " << text << std::endl; 00130 } 00131 }; 00132 00133 class DummyLogFactory : public I_LogFactory { 00134 private: 00135 DummyLog log_; 00136 public: 00137 DummyLogFactory() {} 00138 void initialize(const PropMap& propMap) {}; 00139 I_Log& getLog(const std::string& name="") { return log_; } 00140 void releaseLog(const std::string& name="") {} 00141 }; 00142 00143 // g++ LogManager.cpp -Wall -g -o LogManager -DORG_XMLBLASTER_UTIL_LOGMANAGER=1 -I../ 00144 int main(int argc, char* argv[]) 00145 { 00146 LogManager::PropMap propMap; 00147 for (int ii=0; ii<argc-1; ii++) { 00148 propMap[argv[ii]] = argv[(ii+1)]; 00149 ii++; 00150 } 00151 00152 try { 00153 cout << endl << "Testing customized LogFactory" << endl; 00154 { 00155 LogManager logManager; 00156 logManager.initialize(propMap); 00157 logManager.setLogFactory("test", new DummyLogFactory()); 00158 I_Log& log = logManager.getLogFactory().getLog(); 00159 log.info(__FILE__, "A message"); 00160 if (log.trace()) log.info(__FILE__, "ERROR: trace not expected"); 00161 } 00162 00163 cout << endl << "Testing default behaviour" << endl; 00164 { 00165 LogManager logManager; 00166 logManager.initialize(propMap); 00167 I_Log& log = logManager.getLogFactory().getLog(); 00168 log.info(__FILE__, "A message"); 00169 } 00170 } 00171 catch(...) { 00172 std::cerr << "Caught unexpected exception" << std::endl; 00173 } 00174 00175 return 0; 00176 } 00177 #endif 00178 00179 00180 00181