1 /*----------------------------------------------------------------------------
2 Name: LogManager.cpp
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Handling the Client data
6 ----------------------------------------------------------------------------*/
7 #include <util/LogManager.h>
8 #include <util/Global.h>
9 #include <iostream>
10 #include <exception>
11
12 using namespace std;
13
14 namespace org { namespace xmlBlaster {
15 namespace util {
16
17 /**
18 * Initializes logging and Properties
19 */
20 LogManager::LogManager() : logFactory_(new DefaultLogFactory())
21 {
22 }
23
24 LogManager::~LogManager()
25 {
26 delete logFactory_;
27 }
28
29 void LogManager::initialize(const PropMap& propMap)
30 {
31 if (logFactory_ == 0) {
32 logFactory_ = new DefaultLogFactory();
33 }
34 logFactory_->initialize(propMap);
35 }
36
37 I_LogFactory& LogManager::getLogFactory(const std::string& /*name*/) const
38 {
39 return *logFactory_;
40 }
41
42 void LogManager::setLogFactory(const std::string& /*name*/, I_LogFactory* logFactory)
43 {
44 delete logFactory_;
45 logFactory_ = logFactory;
46 }
47
48 //--------------------DefaultLogFactory implementation -------------------------------------------
49
50
51 //DefaultLogFactory::DefaultLogFactory() : I_LogFactory() {}
52
53 /**
54 * Enforced by I_LogFactory, we are the default implementation.
55 */
56 //void DefaultLogFactory::initialize(const PropMap& properties)
57 //{
58 // this->propMap_ = properties;
59 //}
60
61 /**
62 * Enforced by I_LogFactory, we are the default implementation.
63 */
64 DefaultLogFactory::~DefaultLogFactory()
65 {
66 LogMap::reverse_iterator i;
67 for(i = logMap_.rbegin(); i != logMap_.rend(); ++i) {
68 I_Log* log = (*i).second;
69 delete log;
70 }
71 logMap_.clear();
72 }
73
74 /**
75 * Enforced by I_LogFactory, we are the default implementation.
76 */
77 I_Log& DefaultLogFactory::getLog(const string& logName)
78 {
79 LogMap::iterator pos = logMap_.find(logName);
80 if (pos != logMap_.end()) return *((*pos).second);
81
82 # ifndef ORG_XMLBLASTER_UTIL_LOGMANAGER // To support standalone compilation (see main() below)
83 Global& glob = Global::getInstance();
84 Log *help = new Log(glob.getProperty(), 0, 0, logName);
85 help->initialize();
86 logMap_.insert(LogMap::value_type(logName, help));
87 pos = logMap_.find(logName);
88 if (pos != logMap_.end()) {
89 I_Log* log = (*pos).second;
90 return *log;
91 }
92 # endif
93
94 std::cerr << "LogManager.cpp getLog(" << logName << ") is not implemented -> throwing exception" << std::endl;
95 throw bad_exception();
96 //throw bad_exception("LogManager.cpp not implemented -> throwing exception");
97 }
98
99 /**
100 * Enforced by I_LogFactory, we are the default implementation.
101 */
102 void DefaultLogFactory::releaseLog(const string& name)
103 {
104 std::cerr << "LogManager.cpp releaseLog(" << name << ") is not implemented -> throwing exception" << std::endl;
105 }
106
107 }}} // end of namespace
108
109
110 #ifdef ORG_XMLBLASTER_UTIL_LOGMANAGER
111 using namespace std;
112 using namespace org::xmlBlaster::util;
113
114 class DummyLog : public I_Log
115 { public:
116 void info(const std::string &instance, const std::string &text){
117 std::cout << "[INFO] " << instance << ": " << text << std::endl;
118 }
119 void warn(const std::string &instance, const std::string &text){
120 std::cout << "[WARN] " << instance << ": " << text << std::endl;
121 }
122 void error(const std::string &instance, const std::string &text){
123 std::cout << "[ERROR] " << instance << ": " << text << std::endl;
124 }
125 void trace(const std::string &instance, const std::string &text){
126 std::cout << "[TRACE] " << instance << ": " << text << std::endl;
127 }
128 void call(const std::string &instance, const std::string &text){
129 std::cout << "[CALL] " << instance << ": " << text << std::endl;
130 }
131 };
132
133 class DummyLogFactory : public I_LogFactory {
134 private:
135 DummyLog log_;
136 public:
137 DummyLogFactory() {}
138 void initialize(const PropMap& propMap) {};
139 I_Log& getLog(const std::string& name="") { return log_; }
140 void releaseLog(const std::string& name="") {}
141 };
142
143 // g++ LogManager.cpp -Wall -g -o LogManager -DORG_XMLBLASTER_UTIL_LOGMANAGER=1 -I../
144 int main(int argc, char* argv[])
145 {
146 LogManager::PropMap propMap;
147 for (int ii=0; ii<argc-1; ii++) {
148 propMap[argv[ii]] = argv[(ii+1)];
149 ii++;
150 }
151
152 try {
153 cout << endl << "Testing customized LogFactory" << endl;
154 {
155 LogManager logManager;
156 logManager.initialize(propMap);
157 logManager.setLogFactory("test", new DummyLogFactory());
158 I_Log& log = logManager.getLogFactory().getLog();
159 log.info(__FILE__, "A message");
160 if (log.trace()) log.info(__FILE__, "ERROR: trace not expected");
161 }
162
163 cout << endl << "Testing default behaviour" << endl;
164 {
165 LogManager logManager;
166 logManager.initialize(propMap);
167 I_Log& log = logManager.getLogFactory().getLog();
168 log.info(__FILE__, "A message");
169 }
170 }
171 catch(...) {
172 std::cerr << "Caught unexpected exception" << std::endl;
173 }
174
175 return 0;
176 }
177 #endif
syntax highlighted by Code2HTML, v. 0.9.1