Fawkes API  Fawkes Development Version
log.cpp
1 
2 /***************************************************************************
3  * log.cpp - XML-RPC methods related to logging
4  *
5  * Created: Mon Aug 31 20:50:37 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "log.h"
24 #include <logging/cache.h>
25 
26 #include <xmlrpc-c/girerr.hpp>
27 
28 using namespace fawkes;
29 
30 /** @class XmlRpcLogMethods "log.h"
31  * Wrapper class for logging related XML-RPC methods.
32  * @author Tim Niemueller
33  */
34 
35 /** Constructor.
36  * @param registry XML registry, methods will be automatically registered
37  * @param cache_logger cache logger to access recent log messages
38  * @param logger logger to output messages
39  */
40 XmlRpcLogMethods::XmlRpcLogMethods(xmlrpc_c::registry *registry,
41  fawkes::CacheLogger *cache_logger,
42  fawkes::Logger *logger)
43 {
44  __xmlrpc_registry = registry;
45  __cache_logger = cache_logger;
46  __logger = logger;
47  __log_entries = new log_entries(cache_logger);
48  __log_get_size = new log_get_size(cache_logger);
49  __log_set_size = new log_set_size(cache_logger);
50  __log_log_debug = new log_log(logger, fawkes::Logger::LL_DEBUG);
51  __log_log_info = new log_log(logger, fawkes::Logger::LL_INFO);
52  __log_log_warn = new log_log(logger, fawkes::Logger::LL_WARN);
53  __log_log_error = new log_log(logger, fawkes::Logger::LL_ERROR);
54  __xmlrpc_registry->addMethod("log.entries", __log_entries);
55  __xmlrpc_registry->addMethod("log.get_size", __log_get_size);
56  __xmlrpc_registry->addMethod("log.set_size", __log_set_size);
57  __xmlrpc_registry->addMethod("log.log_debug", __log_log_debug);
58  __xmlrpc_registry->addMethod("log.log_info", __log_log_info);
59  __xmlrpc_registry->addMethod("log.log_warn", __log_log_warn);
60  __xmlrpc_registry->addMethod("log.log_error", __log_log_error);
61 }
62 
63 /** Destructor. */
65 {
66  delete __log_entries;
67  delete __log_get_size;
68  delete __log_set_size;
69  delete __log_log_debug;
70  delete __log_log_info;
71  delete __log_log_warn;
72  delete __log_log_debug;
73 }
74 
75 
76 /** @class XmlRpcLogMethods::log_entries "log.h"
77  * Get most recent log entries via XML-RPC method.
78  * @author Tim Niemueller
79  */
80 
81 /** Constructor.
82  * @param cache_logger cache logger to access recent log messages
83  */
85 {
86  _signature = "A:";
87  _help = "Returns array of recent log messages. Each entry is a struct "
88  "consisting of the entries component, time string and message.";
89 
90  __cache_logger = cache_logger;
91 }
92 
93 /** Virtual empty destructor. */
95 {
96 }
97 
98 /** Execute method.
99  * @param params parameters
100  * @param result result value
101  */
102 void
103 XmlRpcLogMethods::log_entries::execute(xmlrpc_c::paramList const& params,
104  xmlrpc_c::value * const result)
105 {
106  // No reference, copy!
107  __cache_logger->lock();
108  std::list<CacheLogger::CacheEntry> messages = __cache_logger->get_messages();
109  __cache_logger->unlock();
110  std::list<CacheLogger::CacheEntry>::iterator i;
111 
112  std::vector<xmlrpc_c::value> array;
113 
114  for (i = messages.begin(); i != messages.end(); ++i) {
115  std::map<std::string, xmlrpc_c::value> elem;
116  elem.insert(std::make_pair("component", xmlrpc_c::value_string(i->component)));
117  elem.insert(std::make_pair("time", xmlrpc_c::value_datetime(i->time)));
118  elem.insert(std::make_pair("message", xmlrpc_c::value_string(i->message)));
119  array.push_back(xmlrpc_c::value_struct(elem));
120  }
121 
122  *result = xmlrpc_c::value_array(array);
123 }
124 
125 
126 /** @class XmlRpcLogMethods::log_get_size "log.h"
127  * XML-RPC method to get the current cache log size.
128  * @author Tim Niemueller
129  */
130 
131 
132 /** Constructor.
133  * @param cache_logger cache logger to access recent log messages
134  */
136 {
137  _signature = "i:";
138  _help = "Get current maximum size of the cache log.";
139 
140  __cache_logger = cache_logger;
141 }
142 
143 /** Virtual empty destructor. */
145 {
146 }
147 
148 /** Execute method.
149  * @param params parameters
150  * @param result result value
151  */
152 void
153 XmlRpcLogMethods::log_get_size::execute(xmlrpc_c::paramList const& params,
154  xmlrpc_c::value * const result)
155 {
156  *result = xmlrpc_c::value_int(__cache_logger->size());
157 }
158 
159 
160 
161 /** @class XmlRpcLogMethods::log_set_size "log.h"
162  * XML-RPC method to set maximum size of cache logger.
163  * @author Tim Niemueller
164  */
165 
166 /** Constructor.
167  * @param cache_logger cache logger
168  */
170 {
171  _signature = "n:i";
172  _help = "Set maximum size of cache logger.";
173 
174  __cache_logger = cache_logger;
175 }
176 
177 /** Virtual empty destructor. */
179 {
180 }
181 
182 /** Execute method.
183  * @param params parameters
184  * @param result result value
185  */
186 void
187 XmlRpcLogMethods::log_set_size::execute(xmlrpc_c::paramList const& params,
188  xmlrpc_c::value * const result)
189 {
190  int new_size = params.getInt(0);
191  if (new_size <= 0) {
192  throw xmlrpc_c::fault("Illegal size value, must be integer > 0",
193  xmlrpc_c::fault::CODE_UNSPECIFIED);
194  }
195  __cache_logger->set_size(new_size);
196  *result = xmlrpc_c::value_nil();
197 }
198 
199 
200 
201 /** @class XmlRpcLogMethods::log_log "log.h"
202  * XML-RPC method to log a message.
203  * @author Tim Niemueller
204  */
205 
206 /** Constructor.
207  * @param logger logger to output messages
208  * @param log_level level to log messages at
209  */
211  fawkes::Logger::LogLevel log_level)
212 {
213  _signature = "n:ss";
214  _help = "Log message of specified level, arguments are component and message.";
215 
216  __logger = logger;
217  __log_level = log_level;
218 }
219 
220 /** Virtual empty destructor. */
222 {
223 }
224 
225 /** Execute method.
226  * @param params parameters
227  * @param result result value
228  */
229 void
230 XmlRpcLogMethods::log_log::execute(xmlrpc_c::paramList const& params,
231  xmlrpc_c::value * const result)
232 {
233  std::string component = params.getString(0);
234  std::string message = params.getString(1);
235  __logger->log(__log_level, component.c_str(), "%s", message.c_str());
236  *result = xmlrpc_c::value_nil();
237 }
LogLevel
Log level.
Definition: logger.h:45
informational output about normal procedures
Definition: logger.h:47
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: log.cpp:187
Fawkes library namespace.
Logging Cache.
Definition: cache.h:40
warning, should be investigated but software still functions, an example is that something was reques...
Definition: logger.h:48
Get most recent log entries via XML-RPC method.
Definition: log.h:41
virtual ~log_set_size()
Virtual empty destructor.
Definition: log.cpp:178
log_entries(fawkes::CacheLogger *logger)
Constructor.
Definition: log.cpp:84
log_log(fawkes::Logger *logger, fawkes::Logger::LogLevel log_level)
Constructor.
Definition: log.cpp:210
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:51
XML-RPC method to get the current cache log size.
Definition: log.h:51
XML-RPC method to set maximum size of cache logger.
Definition: log.h:61
log_get_size(fawkes::CacheLogger *logger)
Constructor.
Definition: log.cpp:135
XML-RPC method to log a message.
Definition: log.h:71
virtual ~log_entries()
Virtual empty destructor.
Definition: log.cpp:94
virtual ~log_log()
Virtual empty destructor.
Definition: log.cpp:221
~XmlRpcLogMethods()
Destructor.
Definition: log.cpp:64
debug output, relevant only when tracking down problems
Definition: logger.h:46
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: log.cpp:230
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: log.cpp:153
virtual ~log_get_size()
Virtual empty destructor.
Definition: log.cpp:144
log_set_size(fawkes::CacheLogger *cache_logger)
Constructor.
Definition: log.cpp:169
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: log.cpp:103
XmlRpcLogMethods(xmlrpc_c::registry *registry, fawkes::CacheLogger *cache_logger, fawkes::Logger *logger)
Constructor.
Definition: log.cpp:40
Interface for logging.
Definition: logger.h:34