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
25#include <logging/cache.h>
26
27#include <xmlrpc-c/girerr.hpp>
28
29using namespace fawkes;
30
31/** @class XmlRpcLogMethods "log.h"
32 * Wrapper class for logging related XML-RPC methods.
33 * @author Tim Niemueller
34 */
35
36/** Constructor.
37 * @param registry XML registry, methods will be automatically registered
38 * @param cache_logger cache logger to access recent log messages
39 * @param logger logger to output messages
40 */
41XmlRpcLogMethods::XmlRpcLogMethods(std::shared_ptr<xmlrpc_c::registry> registry,
42 fawkes::CacheLogger * cache_logger,
43 fawkes::Logger * logger)
44: xmlrpc_registry_(registry), logger_(logger), cache_logger_(cache_logger)
45{
46 log_entries_.reset(new log_entries(cache_logger_));
47 log_get_size_.reset(new log_get_size(cache_logger_));
48 log_set_size_.reset(new log_set_size(cache_logger_));
49 log_log_debug_.reset(new log_log(logger_, fawkes::Logger::LL_DEBUG));
50 log_log_info_.reset(new log_log(logger_, fawkes::Logger::LL_INFO));
51 log_log_warn_.reset(new log_log(logger_, fawkes::Logger::LL_WARN));
52 log_log_error_.reset(new log_log(logger_, fawkes::Logger::LL_ERROR));
53 xmlrpc_registry_->addMethod("log.entries", &*log_entries_);
54 xmlrpc_registry_->addMethod("log.get_size", &*log_get_size_);
55 xmlrpc_registry_->addMethod("log.set_size", &*log_set_size_);
56 xmlrpc_registry_->addMethod("log.log_debug", &*log_log_debug_);
57 xmlrpc_registry_->addMethod("log.log_info", &*log_log_info_);
58 xmlrpc_registry_->addMethod("log.log_warn", &*log_log_warn_);
59 xmlrpc_registry_->addMethod("log.log_error", &*log_log_error_);
60}
61
62/** Destructor. */
64{
65}
66
67/** @class XmlRpcLogMethods::log_entries "log.h"
68 * Get most recent log entries via XML-RPC method.
69 * @author Tim Niemueller
70 */
71
72/** Constructor.
73 * @param cache_logger cache logger to access recent log messages
74 */
76{
77 _signature = "A:";
78 _help = "Returns array of recent log messages. Each entry is a struct "
79 "consisting of the entries component, time string and message.";
80
81 cache_logger_ = cache_logger;
82}
83
84/** Virtual empty destructor. */
86{
87}
88
89/** Execute method.
90 * @param params parameters
91 * @param result result value
92 */
93void
94XmlRpcLogMethods::log_entries::execute(xmlrpc_c::paramList const &params,
95 xmlrpc_c::value *const result)
96{
97 // No reference, copy!
98 cache_logger_->lock();
99 std::list<CacheLogger::CacheEntry> messages = cache_logger_->get_messages();
100 cache_logger_->unlock();
101 std::list<CacheLogger::CacheEntry>::iterator i;
102
103 std::vector<xmlrpc_c::value> array;
104
105 for (i = messages.begin(); i != messages.end(); ++i) {
106 std::map<std::string, xmlrpc_c::value> elem;
107 elem.insert(std::make_pair("component", xmlrpc_c::value_string(i->component)));
108 elem.insert(std::make_pair("time", xmlrpc_c::value_datetime(i->time)));
109 elem.insert(std::make_pair("message", xmlrpc_c::value_string(i->message)));
110 array.push_back(xmlrpc_c::value_struct(elem));
111 }
112
113 *result = xmlrpc_c::value_array(array);
114}
115
116/** @class XmlRpcLogMethods::log_get_size "log.h"
117 * XML-RPC method to get the current cache log size.
118 * @author Tim Niemueller
119 */
120
121/** Constructor.
122 * @param cache_logger cache logger to access recent log messages
123 */
125{
126 _signature = "i:";
127 _help = "Get current maximum size of the cache log.";
128
129 cache_logger_ = cache_logger;
130}
131
132/** Virtual empty destructor. */
134{
135}
136
137/** Execute method.
138 * @param params parameters
139 * @param result result value
140 */
141void
142XmlRpcLogMethods::log_get_size::execute(xmlrpc_c::paramList const &params,
143 xmlrpc_c::value *const result)
144{
145 *result = xmlrpc_c::value_int(cache_logger_->size());
146}
147
148/** @class XmlRpcLogMethods::log_set_size "log.h"
149 * XML-RPC method to set maximum size of cache logger.
150 * @author Tim Niemueller
151 */
152
153/** Constructor.
154 * @param cache_logger cache logger
155 */
157{
158 _signature = "n:i";
159 _help = "Set maximum size of cache logger.";
160
161 cache_logger_ = cache_logger;
162}
163
164/** Virtual empty destructor. */
166{
167}
168
169/** Execute method.
170 * @param params parameters
171 * @param result result value
172 */
173void
174XmlRpcLogMethods::log_set_size::execute(xmlrpc_c::paramList const &params,
175 xmlrpc_c::value *const result)
176{
177 int new_size = params.getInt(0);
178 if (new_size <= 0) {
179 throw xmlrpc_c::fault("Illegal size value, must be integer > 0",
180 xmlrpc_c::fault::CODE_UNSPECIFIED);
181 }
182 cache_logger_->set_size(new_size);
183 *result = xmlrpc_c::value_nil();
184}
185
186/** @class XmlRpcLogMethods::log_log "log.h"
187 * XML-RPC method to log a message.
188 * @author Tim Niemueller
189 */
190
191/** Constructor.
192 * @param logger logger to output messages
193 * @param log_level level to log messages at
194 */
196{
197 _signature = "n:ss";
198 _help = "Log message of specified level, arguments are component and message.";
199
200 logger_ = logger;
201 log_level_ = log_level;
202}
203
204/** Virtual empty destructor. */
206{
207}
208
209/** Execute method.
210 * @param params parameters
211 * @param result result value
212 */
213void
214XmlRpcLogMethods::log_log::execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
215{
216 std::string component = params.getString(0);
217 std::string message = params.getString(1);
218 logger_->log(log_level_, component.c_str(), "%s", message.c_str());
219 *result = xmlrpc_c::value_nil();
220}
Get most recent log entries via XML-RPC method.
Definition: log.h:43
log_entries(fawkes::CacheLogger *logger)
Constructor.
Definition: log.cpp:75
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: log.cpp:94
virtual ~log_entries()
Virtual empty destructor.
Definition: log.cpp:85
XML-RPC method to get the current cache log size.
Definition: log.h:55
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: log.cpp:142
log_get_size(fawkes::CacheLogger *logger)
Constructor.
Definition: log.cpp:124
virtual ~log_get_size()
Virtual empty destructor.
Definition: log.cpp:133
XML-RPC method to log a message.
Definition: log.h:79
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: log.cpp:214
log_log(fawkes::Logger *logger, fawkes::Logger::LogLevel log_level)
Constructor.
Definition: log.cpp:195
virtual ~log_log()
Virtual empty destructor.
Definition: log.cpp:205
XML-RPC method to set maximum size of cache logger.
Definition: log.h:67
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: log.cpp:174
log_set_size(fawkes::CacheLogger *cache_logger)
Constructor.
Definition: log.cpp:156
virtual ~log_set_size()
Virtual empty destructor.
Definition: log.cpp:165
XmlRpcLogMethods(std::shared_ptr< xmlrpc_c::registry > registry, fawkes::CacheLogger *cache_logger, fawkes::Logger *logger)
Constructor.
Definition: log.cpp:41
~XmlRpcLogMethods()
Destructor.
Definition: log.cpp:63
Logging Cache.
Definition: cache.h:38
std::list< CacheEntry > & get_messages()
Get messages.
Definition: cache.cpp:65
unsigned int size() const
Get maximum number of log entries in cache.
Definition: cache.cpp:83
void lock()
Lock cache logger, no new messages can be added.
Definition: cache.cpp:106
void set_size(unsigned int new_size)
Set maximum number of log entries in cache.
Definition: cache.cpp:92
void unlock()
Unlock cache logger.
Definition: cache.cpp:113
Interface for logging.
Definition: logger.h:42
virtual void log(LogLevel level, const char *component, const char *format,...)
Log message of given log level.
Definition: logger.cpp:326
LogLevel
Log level.
Definition: logger.h:51
@ LL_INFO
informational output about normal procedures
Definition: logger.h:53
@ LL_WARN
warning, should be investigated but software still functions, an example is that something was reques...
Definition: logger.h:54
@ LL_ERROR
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:57
@ LL_DEBUG
debug output, relevant only when tracking down problems
Definition: logger.h:52
Fawkes library namespace.