Fawkes API Fawkes Development Version

logger.cpp

00001 
00002 /***************************************************************************
00003  *  logger.cpp - Fawkes logging interface
00004  *
00005  *  Created: Tue Jan 16 20:40:15 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/logger.h>
00025 
00026 namespace fawkes {
00027 
00028 /** @class Logger logging/logger.h
00029  * Interface for logging.
00030  * This interface facilitates a way to collect all output, be it debugging
00031  * output, informational output, warning or error messages.
00032  *
00033  * There should be no need no more for usage of printf in the code but
00034  * rather a logger should be used instead.
00035  *
00036  * The LoggingAspect should be added to a Thread that has to log something
00037  * (which is likely to be the case).
00038  * For library use QuickLog is the recommended way of logging. Do NOT use
00039  * these in plugins as it hides a dependency of this plugin.
00040  *
00041  * A special note to logging hackers: A logger may never ever bounce. This
00042  * means that not printing a message is ok in case of an internal error in
00043  * the logger, but it may never indicate that error with an exception!
00044  * If a logger cannot deliver the messages as it should be (like a network
00045  * logger that cannot send because the connection is dead) it should at
00046  * least dump it to stderr!
00047  *
00048  * Loggers have to be fast - damn fast. If a lengthy operations is needed
00049  * (like a network connection that may block) messages shall be enqueued
00050  * and processed later (for example in a separate thread). This is because
00051  * everywhere in the software (even in libraries like the utils) a logger
00052  * may be used to log an error that occured (but which is not that critical
00053  * that the application should die). In that case a logger which takes to
00054  * long is absolutely the wrong thing because this would influence the
00055  * performance of the whole software at unpredicted times - while if the
00056  * operations are carried out at a specified time or in a separate thread
00057  * they do not harm the performance.
00058  *
00059  * Caution: The line between log_* methods and vlog_* methods is very thin.
00060  * You can actually call log_info() with a va_list as the only variadic
00061  * parameter in some cases. The call is syntactically correct, but the
00062  * result is not what you intended. Thus make sure that you always use the
00063  * vlog_* method if you pass along a va_list!
00064  *
00065  * @fn void Logger::log_debug(const char *component, const char *format, ...) = 0
00066  * Log debug message.
00067  * @param component component, used to distuinguish logged messages
00068  * @param format format of the message, see man page of sprintf for available
00069  * tokens.
00070  *
00071  * @fn void Logger::log_info(const char *component, const char *format, ...) = 0
00072  * Log informational message.
00073  * @param component component, used to distuinguish logged messages
00074  * @param format format of the message, see man page of sprintf for available
00075  * tokens.
00076  *
00077  * @fn void Logger::log_warn(const char *component, const char *format, ...) = 0
00078  * Log warning message.
00079  * @param component component, used to distuinguish logged messages
00080  * @param format format of the message, see man page of sprintf for available
00081  * tokens.
00082  *
00083  * @fn void Logger::log_error(const char *component, const char *format, ...) = 0
00084  * Log error message.
00085  * @param component component, used to distuinguish logged messages
00086  * @param format format of the message, see man page of sprintf for available
00087  * tokens.
00088  *
00089  * @fn void Logger::vlog_debug(const char *component, const char *format, va_list va) = 0
00090  * Log debug message.
00091  * @param component component, used to distuinguish logged messages
00092  * @param format format of the message, see man page of sprintf for available
00093  * tokens.
00094  * @param va variable argument list
00095  *
00096  * @fn void Logger::vlog_info(const char *component, const char *format, va_list va) = 0
00097  * Log informational message.
00098  * @param component component, used to distuinguish logged messages
00099  * @param format format of the message, see man page of sprintf for available
00100  * tokens.
00101  * @param va variable argument list
00102  *
00103  * @fn void Logger::vlog_warn(const char *component, const char *format, va_list va) = 0
00104  * Log warning message.
00105  * @param component component, used to distuinguish logged messages
00106  * @param format format of the message, see man page of sprintf for available
00107  * tokens.
00108  * @param va variable argument list
00109  *
00110  * @fn void Logger::vlog_error(const char *component, const char *format, va_list va) = 0
00111  * Log error message.
00112  * @param component component, used to distuinguish logged messages
00113  * @param format format of the message, see man page of sprintf for available
00114  * tokens.
00115  * @param va variable argument list
00116  *
00117  * @fn void Logger::log_debug(const char *component, Exception &e) = 0
00118  * Log debug exception.
00119  * @param component component, used to distuinguish logged messages
00120  * @param e exception to log, exception messages will be logged
00121  *
00122  * @fn void Logger::log_info(const char *component, Exception &e) = 0
00123  * Log informational exception.
00124  * @param component component, used to distuinguish logged messages
00125  * @param e exception to log, exception messages will be logged
00126  *
00127  * @fn void Logger::log_warn(const char *component, Exception &e) = 0
00128  * Log warning exception.
00129  * @param component component, used to distuinguish logged messages
00130  * @param e exception to log, exception messages will be logged
00131  *
00132  * @fn void Logger::log_error(const char *component, Exception &e) = 0
00133  * Log error exception.
00134  * @param component component, used to distuinguish logged messages
00135  * @param e exception to log, exception messages will be logged
00136  *
00137  * @fn void Logger::tlog_debug(struct timeval *t, const char *component, const char *format, ...) = 0
00138  * Log debug message for specific time.
00139  * @param t time for this message to log
00140  * @param component component, used to distuinguish logged messages
00141  * @param format format of the message, see man page of sprintf for available
00142  * tokens.
00143  *
00144  * @fn void Logger::tlog_info(struct timeval *t, const char *component, const char *format, ...) = 0
00145  * Log informational message for specific time.
00146  * @param t time for this message to log
00147  * @param component component, used to distuinguish logged messages
00148  * @param format format of the message, see man page of sprintf for available
00149  * tokens.
00150  *
00151  * @fn void Logger::tlog_warn(struct timeval *t, const char *component, const char *format, ...) = 0
00152  * Log warning message for specific time.
00153  * @param t time for this message to log
00154  * @param component component, used to distuinguish logged messages
00155  * @param format format of the message, see man page of sprintf for available
00156  * tokens.
00157  *
00158  * @fn void Logger::tlog_error(struct timeval *t, const char *component, const char *format, ...) = 0
00159  * Log error message for specific time.
00160  * @param t time for this message to log
00161  * @param component component, used to distuinguish logged messages
00162  * @param format format of the message, see man page of sprintf for available
00163  * tokens.
00164  *
00165  * @fn void Logger::tlog_debug(struct timeval *t, const char *component, Exception &e) = 0
00166  * Log debug exception for specific time.
00167  * @param t time for this message to log
00168  * @param component component, used to distuinguish logged messages
00169  * @param e exception to log, exception messages will be logged
00170  *
00171  * @fn void Logger::tlog_info(struct timeval *t, const char *component, Exception &e) = 0
00172  * Log informational exception for specific time.
00173  * @param t time for this message to log
00174  * @param component component, used to distuinguish logged messages
00175  * @param e exception to log, exception messages will be logged
00176  *
00177  * @fn void Logger::tlog_warn(struct timeval *t, const char *component, Exception &e) = 0
00178  * Log warning exception for specific time.
00179  * @param t time for this message to log
00180  * @param component component, used to distuinguish logged messages
00181  * @param e exception to log, exception messages will be logged
00182  *
00183  * @fn void Logger::tlog_error(struct timeval *t, const char *component, Exception &e) = 0
00184  * Log error exception for specific time.
00185  * @param t time for this message to log
00186  * @param component component, used to distuinguish logged messages
00187  * @param e exception to log, exception messages will be logged
00188  *
00189  * @fn void Logger::vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va) = 0
00190  * Log debug message for specific time.
00191  * @param t time for this message to log
00192  * @param component component, used to distuinguish logged messages
00193  * @param format format of the message, see man page of sprintf for available
00194  * tokens.
00195  * @param va variable argument list
00196  *
00197  * @fn void Logger::vtlog_info(struct timeval *t, const char *component, const char *format, va_list va) = 0
00198  * Log informational message for specific time.
00199  * @param t time for this message to log
00200  * @param component component, used to distuinguish logged messages
00201  * @param format format of the message, see man page of sprintf for available
00202  * tokens.
00203  * @param va variable argument list
00204  *
00205  * @fn void Logger::vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va) = 0
00206  * Log warning message for specific time.
00207  * @param t time for this message to log
00208  * @param component component, used to distuinguish logged messages
00209  * @param format format of the message, see man page of sprintf for available
00210  * tokens.
00211  * @param va variable argument list
00212  *
00213  * @fn void Logger::vtlog_error(struct timeval *t, const char *component, const char *format, va_list va) = 0
00214  * Log error message for specific time.
00215  * @param t time for this message to log
00216  * @param component component, used to distuinguish logged messages
00217  * @param format format of the message, see man page of sprintf for available
00218  * tokens.
00219  * @param va variable argument list
00220  *
00221  */
00222 
00223 /** Constructor.
00224  * @param log_level log level
00225  */
00226 Logger::Logger(LogLevel log_level)
00227 {
00228   this->log_level = log_level;
00229 }
00230 
00231 
00232 /** Virtual empty destructor. */
00233 Logger::~Logger()
00234 {
00235 }
00236 
00237 
00238 /** Sets the log level.
00239  * The log level determines the minimum log level. If a message is logged that
00240  * is below this level the message is ignored.
00241  * @param level new log level
00242  */
00243 void
00244 Logger::set_loglevel(LogLevel level)
00245 {
00246   log_level = level;
00247 }
00248 
00249 
00250 /** Get log level.
00251  * @return current log level.
00252  */
00253 Logger::LogLevel
00254 Logger::loglevel()
00255 {
00256   return log_level;
00257 }
00258 
00259 
00260 /** Log message for given log level.
00261  * @param level log level
00262  * @param component component, used to distuinguish logged messages
00263  * @param format format of the message, see man page of sprintf for available
00264  * tokens.
00265  * @param va variadic argument list
00266  */
00267 void
00268 Logger::vlog(LogLevel level,
00269              const char *component, const char *format, va_list va)
00270 {
00271   if ( log_level <= level ) {
00272     switch (level) {
00273     case LL_DEBUG:  vlog_debug(component, format, va);  break;
00274     case LL_INFO:   vlog_info(component, format, va);   break;
00275     case LL_WARN:   vlog_warn(component, format, va);   break;
00276     case LL_ERROR:  vlog_error(component, format, va);  break;
00277     default: break;
00278     }
00279   }
00280 }
00281 
00282 
00283 
00284 /** Log message for given log level and time.
00285  * @param level log level
00286  * @param t time
00287  * @param component component, used to distuinguish logged messages
00288  * @param format format of the message, see man page of sprintf for available
00289  * tokens.
00290  * @param va variadic argument list
00291  */
00292 void
00293 Logger::vtlog(LogLevel level, struct timeval *t,
00294               const char *component, const char *format, va_list va)
00295 {
00296   if ( log_level <= level ) {
00297     switch (level) {
00298     case LL_DEBUG:  vtlog_debug(t, component, format, va);  break;
00299     case LL_INFO:   vtlog_info(t, component, format, va);   break;
00300     case LL_WARN:   vtlog_warn(t, component, format, va);   break;
00301     case LL_ERROR:  vtlog_error(t, component, format, va);  break;
00302     default: break;
00303     }
00304   }
00305 }
00306 
00307 
00308 /** Log message of given log level.
00309  * @param level log level
00310  * @param component component, used to distuinguish logged messages
00311  * @param format format of the message, see man page of sprintf for available
00312  * tokens.
00313  */
00314 void
00315 Logger::log(LogLevel level, const char *component, const char *format, ...)
00316 {
00317   if ( log_level <= level ) {
00318     va_list va;
00319     va_start(va, format);
00320     vlog(level, component, format, va);
00321     va_end(va);
00322   }
00323 }
00324 
00325 
00326 /** Log exception for given log level.
00327  * @param level log level
00328  * @param component component, used to distuinguish logged messages
00329  * @param e exception to log, exception messages will be logged
00330  */
00331 void
00332 Logger::log(LogLevel level, const char *component, Exception &e)
00333 {
00334   if ( log_level <= level ) {
00335     switch (level) {
00336     case LL_DEBUG:  log_debug(component, e);  break;
00337     case LL_INFO:   log_info(component, e);   break;
00338     case LL_WARN:   log_warn(component, e);   break;
00339     case LL_ERROR:  log_error(component, e);  break;
00340     default: break;
00341     }
00342   }
00343 }
00344 
00345 
00346 
00347 /** Log message of given log level and time.
00348  * @param t time
00349  * @param level log level
00350  * @param component component, used to distuinguish logged messages
00351  * @param format format of the message, see man page of sprintf for available
00352  * tokens.
00353  */
00354 void
00355 Logger::tlog(LogLevel level, struct timeval *t,
00356              const char *component, const char *format, ...)
00357 {
00358   if ( log_level <= level ) {
00359     va_list va;
00360     va_start(va, format);
00361     vtlog(level, t, component, format, va);
00362     va_end(va);
00363   }
00364 }
00365 
00366 
00367 /** Log exception for given log level.
00368  * @param t time
00369  * @param level log level
00370  * @param component component, used to distuinguish logged messages
00371  * @param e exception to log, exception messages will be logged
00372  */
00373 void
00374 Logger::tlog(LogLevel level, struct timeval *t, const char *component, Exception &e)
00375 {
00376   if ( log_level <= level ) {
00377     switch (level) {
00378     case LL_DEBUG:  tlog_debug(t, component, e);  break;
00379     case LL_INFO:   tlog_info(t, component, e);   break;
00380     case LL_WARN:   tlog_warn(t, component, e);   break;
00381     case LL_ERROR:  tlog_error(t, component, e);  break;
00382     default: break;
00383     }
00384   }
00385 }
00386 
00387 
00388 } // end namespace fawkes
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends