xorp

xlog.h

00001 /* -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*- */
00002 /* vim:set sts=4 ts=8: */
00003 
00004 /*
00005  * Copyright (c) 2001-2012 XORP, Inc and Others
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU Lesser General Public License, Version
00009  * 2.1, June 1999 as published by the Free Software Foundation.
00010  * Redistribution and/or modification of this program under the terms of
00011  * any other version of the GNU Lesser General Public License is not
00012  * permitted.
00013  * 
00014  * This program is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00017  * see the GNU Lesser General Public License, Version 2.1, a copy of
00018  * which can be found in the XORP LICENSE.lgpl file.
00019  * 
00020  * XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00021  * http://xorp.net
00022  */
00023 
00024 
00025 #ifndef __LIBXORP_XLOG_H__
00026 #define __LIBXORP_XLOG_H__
00027 
00028 #include "libxorp/xorp.h"
00029 
00030 
00031 // TODO:  Make this configurable in scons
00032 #define XORP_LOG_PRINT_USECS
00033 
00034 /*
00035  * The following defines and notes we defined __printfike if it does not
00036  * already exist.  The expansion of this macro uses a gcc extension to
00037  * check format strings.
00038  */
00039 #ifndef __printflike
00040 #ifdef __GNUC__
00041 #define __printflike(fmt,va1) __attribute__((__format__(printf, fmt, va1)))
00042 #define __libxorp_xlog_defined_printflike
00043 #else
00044 #define __printflike(fmt, va1)
00045 #define __libxorp_xlog_defined_printflike
00046 #endif /* __GNUC__ */
00047 #endif /* __printflike */
00048 
00056 #   ifdef __cplusplus
00057 extern "C" {
00058 #   endif
00059 
00064 typedef enum {
00065     XLOG_LEVEL_MIN = 0,     /* 0 */
00066     XLOG_LEVEL_FATAL = 0,   /* 0 */
00067     XLOG_LEVEL_ERROR,       /* 1 */
00068     XLOG_LEVEL_WARNING,     /* 2 */
00069     XLOG_LEVEL_INFO,        /* 3 */
00070     XLOG_LEVEL_TRACE,       /* 4 */
00071     XLOG_LEVEL_RTRMGR_ONLY_NO_PREAMBLE, /* 5 */ /* XXX: temp, to be removed; see Bugzilla entry 795 */
00072     XLOG_LEVEL_MAX
00073 } xlog_level_t;
00074 
00079 typedef enum {
00080     XLOG_VERBOSE_LOW = 0,   /* 0 */
00081     XLOG_VERBOSE_MEDIUM,    /* 1 */
00082     XLOG_VERBOSE_HIGH,      /* 2 */
00083     XLOG_VERBOSE_RTRMGR_ONLY_NO_PREAMBLE, /* 3 */ /* XXX: temp, to be removed; see Bugzilla entry 795 */
00084     XLOG_VERBOSE_MAX
00085 } xlog_verbose_t;
00086 
00087 /* Don't modify this directly outside the xlog.[ch] files */
00088 extern int xlog_level_enabled[XLOG_LEVEL_MAX];
00089 
00093 typedef int (*xlog_output_func_t)(void *obj, xlog_level_t level,
00094     const char *msg);
00095 
00099 #if (!defined XORP_MODULE_NAME) && (!defined XORP_LIBRARY_NAME)
00100 #error  You MUST have in your local directory a file like
00101 #error  "foo_module.h" that has defined inside the module
00102 #error  or library name. E.g.: #define XORP_MODULE_NAME "BGP"
00103 #error  or #define XORP_LIBRARY_NAME
00104 #error  This "foo_module.h" must be the first included by
00105 #error  each *.c and *.cc file.
00106 #endif
00107 
00108 #ifdef XORP_LIBRARY_NAME
00109 #  define _XLOG_MODULE_NAME XORP_LIBRARY_NAME
00110 #else
00111 #  define _XLOG_MODULE_NAME XORP_MODULE_NAME
00112 #endif
00113 
00114 #define XLOG_FN(__log_level, fmt...)                    \
00115     do {                                \
00116     if (xlog_level_enabled[__log_level])                \
00117         _xlog_with_level(__log_level, _XLOG_MODULE_NAME, __LINE__,  \
00118                  __FILE__, __FUNCTION__, fmt);      \
00119     } while (0)
00120 
00136 int xlog_init(const char *argv0, const char *preamble_message);
00137 
00143 int xlog_exit(void);
00144 
00150 int xlog_start(void);
00151 
00157 int xlog_stop(void);
00158 
00164 int xlog_is_running(void);
00165 
00175 int xlog_enable(xlog_level_t log_level);
00176 
00186 int xlog_disable(xlog_level_t log_level);
00187 
00193 void    xlog_set_preamble(const char *text);
00194 
00200 const char* xlog_process_name(void);
00201 
00211 void    xlog_set_verbose(xlog_verbose_t verbose_level);
00212 
00225 void    xlog_level_set_verbose(xlog_level_t log_level,
00226                    xlog_verbose_t verbose_level);
00227 
00234 int     xlog_add_output(FILE *fp);
00235 
00242 int xlog_remove_output(FILE *fp);
00243 
00252 int     xlog_add_output_func(xlog_output_func_t func, void *obj);
00253 
00261 int xlog_add_syslog_output(const char *syslogspec);
00262 
00271 int xlog_remove_output_func(xlog_output_func_t func, void *obj);
00272 
00282 int xlog_add_default_output(void);
00283 
00289 int xlog_remove_default_output(void);
00290 
00302 void _xlog_with_level(int log_level,
00303               const char *module_name,
00304               int line,
00305               const char *file,
00306               const char *function,
00307               const char *format, ...) __printflike(6,7);
00308 
00309 #ifdef L_FATAL
00310 # define XLOG_FATAL(fmt...) \
00311     do {                            \
00312     _xlog_with_level(XLOG_LEVEL_FATAL, _XLOG_MODULE_NAME,   \
00313              __LINE__, __FILE__, __FUNCTION__,  \
00314              fmt);                  \
00315     assert(0);                      \
00316     } while (0)
00317 #else
00318 # define XLOG_FATAL(fmt...) assert(0);
00319 #endif
00320 
00321 #ifdef L_ERROR
00322  #define XLOG_ERROR(fmt...) XLOG_FN(XLOG_LEVEL_ERROR, fmt)
00323 #else
00324  #define XLOG_ERROR(fmt...) do{}while(0)
00325 #endif     
00326 
00327 #ifdef L_WARNING
00328  #define XLOG_WARNING(fmt...)   XLOG_FN(XLOG_LEVEL_WARNING, fmt)
00329 #else
00330  #define XLOG_WARNING(fmt...)   do{}while(0)
00331 #endif
00332 
00333 #ifdef L_INFO
00334  #define XLOG_INFO(fmt...)  XLOG_FN(XLOG_LEVEL_INFO, fmt)
00335 #else
00336  #define XLOG_INFO(fmt...)  do{}while(0)
00337 #endif
00338 
00342 #ifdef L_OTHER
00343  #define XLOG_RTRMGR_ONLY_NO_PREAMBLE(fmt...)   XLOG_FN(XLOG_LEVEL_RTRMGR_ONLY_NO_PREAMBLE, fmt)
00344 #else
00345  #define XLOG_RTRMGR_ONLY_NO_PREAMBLE(fmt...)   do{}while(0)
00346 #endif    
00347 
00348 #ifdef L_ASSERT
00349 
00357  #define XLOG_ASSERT(assertion)                     \
00358  do {                                   \
00359      if (!(assertion)) {                        \
00360      XLOG_FATAL(#assertion);                    \
00361      }                                  \
00362  } while (0)
00363 #else
00364 # define XLOG_ASSERT(assertion) assert(assertion)
00365 #endif
00366 
00374 #ifdef L_OTHER
00375  #define XLOG_UNREACHABLE()                     \
00376  do {                                   \
00377     XLOG_FATAL("Internal fatal error: unreachable code reached");   \
00378     exit(1);    /* unreached: keep the compiler happy */    \
00379  } while (0)
00380 #else
00381 # define XLOG_UNREACHABLE() assert(0)
00382 #endif
00383 
00392 #ifdef L_OTHER
00393  #define XLOG_UNFINISHED()                      \
00394  do {                                   \
00395     XLOG_FATAL("Internal fatal error: unfinished code reached");    \
00396     exit(1);    /* unreached: keep the compiler happy */    \
00397  } while (0)
00398 #else
00399 # define XLOG_UNFINISHED()  assert(0)
00400 #endif
00401 
00402 /*
00403  * The macros below define the XLOG_TRACE(), the macro responsible for
00404  * generating trace messages.  It takes the same arguments as
00405  * printf(), except that the very first argument is a boolean
00406  * that defines whether the trace message will be output.
00407  * Note that a trailing newline is added if none is present.
00408  * E.g.,
00409  *
00410  *      XLOG_TRACE(cond_variable, "The number is %d", 5);
00411  *
00412  */
00413 #ifdef L_TRACE
00414 #   define XLOG_TRACE(__flg, args...)               \
00415     do {                                \
00416     if (__flg && xlog_level_enabled[XLOG_LEVEL_TRACE]) {                            \
00417         _xlog_with_level(XLOG_LEVEL_TRACE, XORP_MODULE_NAME,    \
00418                  __LINE__, __FILE__, __FUNCTION__,  \
00419                  args);                 \
00420     }                               \
00421     } while(0)
00422 #else
00423 #   define XLOG_TRACE(args...) do{} while(0)
00424 #endif
00425 
00426 
00439 const char *xlog_localtime2string(void);
00440 
00459 int x_vasprintf(char **ret, const char *format, va_list ap);
00460 
00476 int x_asprintf(char **ret, const char *format, ...);
00477 
00478 /* Undefine __printflike if we defined it */
00479 #ifdef __libxorp_xlog_defined_printflike
00480 #undef __libxorp_xlog_defined_printflike
00481 #undef __printflike
00482 #endif /* __libxorp_xlog_defined_printflike */
00483 
00484 #   ifdef __cplusplus
00485 }
00486 #   endif
00487 
00488 #endif /* __LIBXORP_XLOG_H__ */
 All Classes Namespaces Functions Variables Typedefs Enumerations