Fawkes API Fawkes Development Version
component.cpp
1
2/***************************************************************************
3 * component.cpp - Component logger
4 *
5 * Created: Wed Mar 12 23:46:34 2008
6 * Copyright 2006-2008 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. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24#include <logging/component.h>
25#include <logging/logger.h>
26
27#include <cstdio>
28#include <cstdlib>
29#include <cstring>
30
31namespace fawkes {
32
33/** @class ComponentLogger <logging/component.h>
34 * Component logger.
35 * This is a small wrapper around a logger to make it simpler to use in a
36 * single component. Once initialized it will only accept messages for a
37 * specific component string offers a simplified interface to logging methods.
38 *
39 * @author Tim Niemueller
40 */
41
42/** Constructor.
43 * @param logger logger to use
44 * @param component component string, copied to internal buffer
45 */
46ComponentLogger::ComponentLogger(Logger *logger, const char *component)
47{
48 logger_ = logger;
49 component_ = strdup(component);
50}
51
52/** Destructor. */
54{
55 free(component_);
56}
57
58/** Set a new component name.
59 * @param format format string for the new command string, cf. sprintf
60 * man page for allowed syntax.
61 */
62void
63ComponentLogger::set_component(const char *format, ...)
64{
65 va_list arg;
66 va_start(arg, format);
67 char *new_component;
68 if (vasprintf(&new_component, format, arg) > 0) {
69 char *old_component = component_;
70 component_ = new_component;
71 free(old_component);
72 }
73 va_end(arg);
74}
75
76/** Log debug message.
77 * @param format format of the message, see man page of sprintf for available
78 * tokens.
79 */
80void
81ComponentLogger::log_debug(const char *format, ...)
82{
83 va_list va;
84 va_start(va, format);
85 logger_->vlog_debug(component_, format, va);
86 va_end(va);
87}
88
89/** Log info message.
90 * @param format format of the message, see man page of sprintf for available
91 * tokens.
92 */
93void
94ComponentLogger::log_info(const char *format, ...)
95{
96 va_list va;
97 va_start(va, format);
98 logger_->vlog_info(component_, format, va);
99 va_end(va);
100}
101
102/** Log warning message.
103 * @param format format of the message, see man page of sprintf for available
104 * tokens.
105 */
106void
107ComponentLogger::log_warn(const char *format, ...)
108{
109 va_list va;
110 va_start(va, format);
111 logger_->vlog_warn(component_, format, va);
112 va_end(va);
113}
114
115/** Log error message.
116 * @param format format of the message, see man page of sprintf for available
117 * tokens.
118 */
119void
120ComponentLogger::log_error(const char *format, ...)
121{
122 va_list va;
123 va_start(va, format);
124 logger_->vlog_error(component_, format, va);
125 va_end(va);
126}
127
128/** Log debug message.
129 * @param message message to log
130 */
131void
132ComponentLogger::log_debug(std::string message)
133{
134 logger_->log_debug(component_, "%s", message.c_str());
135}
136
137/** Log info message.
138 * @param message message to log
139 */
140void
141ComponentLogger::log_info(std::string message)
142{
143 logger_->log_info(component_, "%s", message.c_str());
144}
145
146/** Log warning message.
147 * @param message message to log
148 */
149void
150ComponentLogger::log_warn(std::string message)
151{
152 logger_->log_warn(component_, "%s", message.c_str());
153}
154
155/** Log error message.
156 * @param message message to log
157 */
158void
159ComponentLogger::log_error(std::string message)
160{
161 logger_->log_error(component_, "%s", message.c_str());
162}
163
164/** Log exception at debug log level.
165 * @param e exception to log, exception messages will be logged
166 */
167void
169{
170 logger_->log_debug(component_, e);
171}
172
173/** Log exception at info log level.
174 * @param e exception to log, exception messages will be logged
175 */
176void
178{
179 logger_->log_info(component_, e);
180}
181
182/** Log exception at warn log level.
183 * @param e exception to log, exception messages will be logged
184 */
185void
187{
188 logger_->log_warn(component_, e);
189}
190
191/** Log exception at error log level.
192 * @param e exception to log, exception messages will be logged
193 */
194void
196{
197 logger_->log_debug(component_, e);
198}
199
200} // end namespace fawkes
void log_debug(const char *format,...)
Log debug message.
Definition: component.cpp:81
ComponentLogger(Logger *logger, const char *component)
Constructor.
Definition: component.cpp:46
void log_warn(const char *format,...)
Log warning message.
Definition: component.cpp:107
~ComponentLogger()
Destructor.
Definition: component.cpp:53
void set_component(const char *format,...)
Set a new component name.
Definition: component.cpp:63
void log_info(const char *format,...)
Log info message.
Definition: component.cpp:94
void log_error(const char *format,...)
Log error message.
Definition: component.cpp:120
Base class for exceptions in Fawkes.
Definition: exception.h:36
Interface for logging.
Definition: logger.h:42
virtual void vlog_error(const char *component, const char *format, va_list va)=0
Log error message.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
virtual void vlog_debug(const char *component, const char *format, va_list va)=0
Log debug message.
virtual void vlog_info(const char *component, const char *format, va_list va)=0
Log informational message.
virtual void vlog_warn(const char *component, const char *format, va_list va)=0
Log warning message.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Fawkes library namespace.