Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * liblogger.h - Fawkes lib logger 00004 * 00005 * Created: Mon May 07 15:22:18 2007 00006 * Copyright 2006-2007 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <utils/logging/liblogger.h> 00025 #include <utils/logging/multi.h> 00026 #include <utils/logging/console.h> 00027 00028 #include <core/exceptions/software.h> 00029 #include <core/threading/mutex.h> 00030 00031 namespace fawkes { 00032 00033 /** @class LibLogger <utils/logging/liblogger.h> 00034 * Library logger. 00035 * This logger is meant to be used in libraries that depend on utils anyway 00036 * and in utils itself. This logger is completely static so it only has to be 00037 * initialized once per process. If the logger is used before it has been 00038 * initialized it is automatically initialized with an empty MultiLogger. 00039 * If you want to see output you have to make sure that you add loggers 00040 * like the ConsoleLogger. 00041 * 00042 * Make sure that you call finalize() at the end of the surrounding process 00043 * to free all the loggers associcated with the internal multi logger and 00044 * the multi logger itself. 00045 * 00046 * @see MultiLogger 00047 * @author Tim Niemueller 00048 */ 00049 00050 00051 /** The internal multi logger. */ 00052 MultiLogger * LibLogger::logger = NULL; 00053 /** Internal mutex */ 00054 Mutex * LibLogger::mutex = NULL; 00055 00056 00057 /** Initialize logger. 00058 * @param multi_logger Logger to use in this multi logger. If NULL a new 00059 * logger is created. Note that LibLogger takes over ownership of the 00060 * multi logger and will destroy it if finalize() is called. 00061 */ 00062 void 00063 LibLogger::init(MultiLogger *multi_logger) 00064 { 00065 if ( logger != NULL ) { 00066 throw AccessViolationException("LibLogger already initialized"); 00067 } 00068 mutex = new Mutex(); 00069 if ( multi_logger == NULL ) { 00070 logger = new MultiLogger(new ConsoleLogger()); 00071 } else { 00072 logger = multi_logger; 00073 } 00074 00075 } 00076 00077 00078 /** Delete internal logger. 00079 * Note that the multi logger took over ownership of the loggers. 00080 * @see MultiLogger 00081 */ 00082 void 00083 LibLogger::finalize() 00084 { 00085 delete logger; 00086 delete mutex; 00087 } 00088 00089 00090 /** Add logger. 00091 * @param l sub-logger to add 00092 * @see MultiLogger::add_logger() 00093 */ 00094 void 00095 LibLogger::add_logger(Logger *l) 00096 { 00097 if ( logger == NULL ) init(); 00098 mutex->lock(); 00099 logger->add_logger(l); 00100 mutex->unlock(); 00101 } 00102 00103 00104 /** Remove logger. 00105 * @param l sub-logger to remove 00106 * @see MultiLogger::remove_logger() 00107 */ 00108 void 00109 LibLogger::remove_logger(Logger *l) 00110 { 00111 if ( logger == NULL ) init(); 00112 mutex->lock(); 00113 logger->remove_logger(l); 00114 mutex->unlock(); 00115 } 00116 00117 00118 /** Log debug message. 00119 * @param component component, used to distuinguish logged messages 00120 * @param format format of the message, see man page of sprintf for available 00121 * tokens. 00122 */ 00123 void 00124 LibLogger::log_debug(const char *component, const char *format, ...) 00125 { 00126 if ( logger == NULL ) init(); 00127 mutex->lock(); 00128 va_list va; 00129 va_start(va, format); 00130 logger->vlog_debug(component, format, va); 00131 va_end(va); 00132 mutex->unlock(); 00133 } 00134 00135 00136 /** Log informational message. 00137 * @param component component, used to distuinguish logged messages 00138 * @param format format of the message, see man page of sprintf for available 00139 * tokens. 00140 */ 00141 void 00142 LibLogger::log_info(const char *component, const char *format, ...) 00143 { 00144 if ( logger == NULL ) init(); 00145 mutex->lock(); 00146 va_list va; 00147 va_start(va, format); 00148 logger->vlog_info(component, format, va); 00149 va_end(va); 00150 mutex->unlock(); 00151 } 00152 00153 00154 /** Log warning message. 00155 * @param component component, used to distuinguish logged messages 00156 * @param format format of the message, see man page of sprintf for available 00157 * tokens. 00158 */ 00159 void 00160 LibLogger::log_warn(const char *component, const char *format, ...) 00161 { 00162 if ( logger == NULL ) init(); 00163 mutex->lock(); 00164 va_list va; 00165 va_start(va, format); 00166 logger->vlog_warn(component, format, va); 00167 va_end(va); 00168 mutex->unlock(); 00169 } 00170 00171 00172 /** Log error message. 00173 * @param component component, used to distuinguish logged messages 00174 * @param format format of the message, see man page of sprintf for available 00175 * tokens. 00176 */ 00177 void 00178 LibLogger::log_error(const char *component, const char *format, ...) 00179 { 00180 if ( logger == NULL ) init(); 00181 mutex->lock(); 00182 va_list va; 00183 va_start(va, format); 00184 logger->vlog_error(component, format, va); 00185 va_end(va); 00186 mutex->unlock(); 00187 } 00188 00189 00190 /** Log debug message. 00191 * @param component component, used to distuinguish logged messages 00192 * @param format format of the message, see man page of sprintf for available 00193 * tokens. 00194 * @param va variadic argument list 00195 */ 00196 void 00197 LibLogger::vlog_debug(const char *component, const char *format, va_list va) 00198 { 00199 if ( logger == NULL ) init(); 00200 mutex->lock(); 00201 logger->vlog_debug(component, format, va); 00202 mutex->unlock(); 00203 } 00204 00205 00206 /** Log informational message. 00207 * @param component component, used to distuinguish logged messages 00208 * @param format format of the message, see man page of sprintf for available 00209 * tokens. 00210 * @param va variadic argument list 00211 */ 00212 void 00213 LibLogger::vlog_info(const char *component, const char *format, va_list va) 00214 { 00215 if ( logger == NULL ) init(); 00216 mutex->lock(); 00217 logger->vlog_info(component, format, va); 00218 mutex->unlock(); 00219 } 00220 00221 00222 /** Log warning message. 00223 * @param component component, used to distuinguish logged messages 00224 * @param format format of the message, see man page of sprintf for available 00225 * tokens. 00226 * @param va variadic argument list 00227 */ 00228 void 00229 LibLogger::vlog_warn(const char *component, const char *format, va_list va) 00230 { 00231 if ( logger == NULL ) init(); 00232 mutex->lock(); 00233 logger->vlog_warn(component, format, va); 00234 mutex->unlock(); 00235 } 00236 00237 00238 /** Log error message. 00239 * @param component component, used to distuinguish logged messages 00240 * @param format format of the message, see man page of sprintf for available 00241 * tokens. 00242 * @param va variadic argument list 00243 */ 00244 void 00245 LibLogger::vlog_error(const char *component, const char *format, va_list va) 00246 { 00247 if ( logger == NULL ) init(); 00248 mutex->lock(); 00249 logger->vlog_error(component, format, va); 00250 mutex->unlock(); 00251 } 00252 00253 00254 00255 /** Log debug message. 00256 * @param component component, used to distuinguish logged messages 00257 * @param e exception to log, exception messages will be logged 00258 */ 00259 void 00260 LibLogger::log_debug(const char *component, Exception &e) 00261 { 00262 if ( logger == NULL ) init(); 00263 mutex->lock(); 00264 logger->log_debug(component, e); 00265 mutex->unlock(); 00266 } 00267 00268 /** Log informational message. 00269 * @param component component, used to distuinguish logged messages 00270 * @param e exception to log, exception messages will be logged 00271 */ 00272 void 00273 LibLogger::log_info(const char *component, Exception &e) 00274 { 00275 if ( logger == NULL ) init(); 00276 mutex->lock(); 00277 logger->log_info(component, e); 00278 mutex->unlock(); 00279 } 00280 00281 00282 /** Log warning message. 00283 * @param component component, used to distuinguish logged messages 00284 * @param e exception to log, exception messages will be logged 00285 */ 00286 void 00287 LibLogger::log_warn(const char *component, Exception &e) 00288 { 00289 if ( logger == NULL ) init(); 00290 mutex->lock(); 00291 logger->log_warn(component, e); 00292 mutex->unlock(); 00293 } 00294 00295 00296 /** Log error message. 00297 * @param component component, used to distuinguish logged messages 00298 * @param e exception to log, exception messages will be logged 00299 */ 00300 void 00301 LibLogger::log_error(const char *component, Exception &e) 00302 { 00303 if ( logger == NULL ) init(); 00304 mutex->lock(); 00305 logger->log_error(component, e); 00306 mutex->unlock(); 00307 } 00308 00309 00310 } // end namespace fawkes