log.H File Reference

Logging facility. More...

#include <exception>
#include <syslog.h>
#include <string.h>
Include dependency graph for log.H:

Go to the source code of this file.

Classes

class  lfatal_exception
 Type of exception that is thrown from LFATAL if (LOG_FLAGS&LFATAL_THROWS). More...

Defines

#define LBS2   64
 max size of function or file name
#define SENDLOG   MYLOG_USEID=false, MYLOG_USEP=false, sendlog
#define SENDPLOG   MYLOG_USEID=false, MYLOG_USEP=true, sendlog
#define SENDIDLOG   MYLOG_USEID=true, MYLOG_USEP=false, sendlog
#define SENDIDPLOG   MYLOG_USEID=true, MYLOG_USEP=true, sendlog
#define LDEBUG
#define LINFO
#define LERROR
#define LFATAL
#define PLDEBUG
#define PLINFO
#define PLERROR
#define PLFATAL
#define IDLDEBUG
#define IDLINFO
#define IDLERROR
#define IDLFATAL
#define IDPLDEBUG
#define IDPLINFO
#define IDPLERROR
#define IDPLFATAL
#define CLOGFILE   ("[" + descriptiveName() + "]").c_str()
#define CLDEBUG
#define CLINFO
#define CLERROR
#define CLFATAL
#define CPLDEBUG
#define CPLINFO
#define CPLERROR
#define CPLFATAL
#define REPORT_CURRENT_EXCEPTION   report_current_exception(__LINE__, __FILE__)
 Use this inside of a catch(...) to report info about the caught exception.

Functions

void sendlog (const char *msg,...) __attribute__((format(__printf__
void report_current_exception (const int line, const char *file) throw ()
 Helper function for REPORT_CURRENT_EXCEPTION macro.

Variables

const int LFATAL_THROWS = (1 << 0)
 Make LFATAL() throw a std::exception rather than abort()ing.
const int LFATAL_PRINTS_ABORT = (1 << 1)
 Make LFATAL() print " -- ABORT" after each message.
const int LFATAL_XTRA_NOISY = (1 << 2)
 Make LFATAL() print its msg to stderr/syslog AND put it in the std::exception.
const int LOG_FULLTRACE = (1 << 3)
 Whether to send log messages to stderr or to syslog.
int LOG_FLAGS
 Bitwise-or'ed combination of above flags.
int MYLOGVERB
const char * MYLOGPREFIX
 Null by default, but if set to non-null this string will be prefixed to every log message.
void char MYLOGFUN []
void char MYLOGFIL []
int MYLOGLIN
int MYLOGLEV
int MYLOGIDVAL
bool MYLOG_USEID
bool MYLOG_USEP

Detailed Description

Logging facility.

This is a comprehensive logging facility. Use the functions provided below to display messages to the user and/or syslog. Never use printf() or std::cout<< in your code, use the functions provided here instead.

Basic function call is [ID | C][P]L<LOGLEV>("format", args, ...)

Although macros, all functions are atomic, so 'if (bad) LERROR(bad);' is ok; also arguments that span several lines of source code are ok too. Using these functions in macros however is probably bogus: MACRO(LERROR("xx")). CAUTION: message size is limited to LBS chars (including final ''); longer messages will be cleanly truncated.

All log functions act like printf, so be careful when passing strings from outside directly as the format string for these functions, as outside strings may contain '' signs which could segfault your program! so, use LERROR("%s", unknown_string) rather than LERROR(unknown_string).

DEBUG, INFO and ERROR simply report an error. FATAL also throws an exception of class "bug" (or exits; see implementation).

Log entries will look like:


>>>>>>>>>>>>>>>>>>>> LERROR("success! %d %s", 10, "bozo")
Jan 11 07:21:26 jukebox dntwebcfgd: SockServ::check: success! 10 bozo
^^^^^^^^^^^^^^^ ^^^^^^^ ^^^^^^^^^^  ^^^^^^^^  ^^^^^  ^^^^^^^^^^^^^^^^
   date/time    machine   program    class    funct     message

>>>>>>>>>>>>>>>>>>>> PLERROR("Cannot open");
Jan 11 07:21:26 jukebox dntwebcfg: Cool::check: Cannot open (Bad descriptor)
^^^^^^^^^^^^^^^ ^^^^^^^ ^^^^^^^^^  ^^^^  ^^^^^  ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
  date/time    machine   program   class funct    message    errno in clear

>>>>>>>>>>>>>>>>>>>> IDLINFO("Connected");
with '#define MYLOGID fd' and current value of fd being 10
Jan 11 07:21:26 jukebox dntwebcfgd: Cool::check:(10) Connected
^^^^^^^^^^^^^^^ ^^^^^^^ ^^^^^^^^^^  ^^^^  ^^^^^  ^^  ^^^^^^^^^
   date/time    machine   program   class funct  ID   message

>>>>>>>>>>>>>>>>>>>> IDPLFATAL("Abort");
with '#define MYLOGID fd' and current value of fd being 10
Jan 11 07:21:26 jukebox dntwebcfgd: Cool::check:(10) Abort (Out of memory)
^^^^^^^^^^^^^^^ ^^^^^^^ ^^^^^^^^^^  ^^^^  ^^^^^  ^^  ^^^^^  ^^^^^^^^^^^^^
   date/time    machine   program   class funct  ID   mes   errno in clear

>>>>>>>>>>>>>>>>>>>> CLERROR("success! %d %s", 10, "bozo")
Jan 11 07:21:26 jukebox dntwebcfgd: [Gabor(45)]::check: success! 10 bozo
^^^^^^^^^^^^^^^ ^^^^^^^ ^^^^^^^^^^  ^^^^^^^^^^^  ^^^^^  ^^^^^^^^^^^^^^^^
   date/time    machine   program ModelComponent funct     message

If the message starts with a '(', '[' or '{' char, no space is written between the colon following function name and the message

A last macro is provided, SHOWMEMORY(format, ...) which will display your message via LERROR, and append the total memory usage for the current process (as per the VmSize field in /proc/<pid>/status; this is useful to check for possible memory leaks). It only works under Linux and hence we have only defined it in the GNU C section here.

Definition in file log.H.


Define Documentation

#define CLDEBUG
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, CLOGFILE, LBS2-1),MYLOGLEV=LOG_DEBUG,SENDLOG

Definition at line 319 of file log.H.

#define CLERROR
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, CLOGFILE, LBS2-1),MYLOGLEV=LOG_ERR,SENDLOG

Definition at line 323 of file log.H.

#define CLFATAL
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, CLOGFILE, LBS2-1),MYLOGLEV=LOG_CRIT,SENDLOG

Definition at line 325 of file log.H.

#define CLINFO
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, CLOGFILE, LBS2-1),MYLOGLEV=LOG_INFO,SENDLOG

Definition at line 321 of file log.H.

#define CPLDEBUG
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, CLOGFILE, LBS2-1),MYLOGLEV=LOG_DEBUG,SENDPLOG

Definition at line 328 of file log.H.

#define CPLERROR
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, CLOGFILE, LBS2-1),MYLOGLEV=LOG_ERR,SENDPLOG

Definition at line 332 of file log.H.

#define CPLFATAL
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, CLOGFILE, LBS2-1),MYLOGLEV=LOG_CRIT,SENDPLOG

Definition at line 334 of file log.H.

#define CPLINFO
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, CLOGFILE, LBS2-1),MYLOGLEV=LOG_INFO,SENDPLOG

Definition at line 330 of file log.H.

#define IDLDEBUG
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
MYLOGIDVAL=MYLOGID,strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_DEBUG,\
SENDIDLOG

Definition at line 291 of file log.H.

#define IDLERROR
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
MYLOGIDVAL=MYLOGID,strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_ERR,\
SENDIDLOG

Definition at line 297 of file log.H.

#define IDLFATAL
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
MYLOGIDVAL=MYLOGID,strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_CRIT,\
SENDIDLOG

Definition at line 300 of file log.H.

#define IDLINFO
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
MYLOGIDVAL=MYLOGID,strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_INFO,\
SENDIDLOG

Definition at line 294 of file log.H.

#define IDPLDEBUG
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
MYLOGIDVAL=MYLOGID,strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_DEBUG,\
SENDIDPLOG

Definition at line 304 of file log.H.

#define IDPLERROR
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
MYLOGIDVAL=MYLOGID,strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_ERR,\
SENDIDPLOG

Definition at line 310 of file log.H.

#define IDPLFATAL
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
MYLOGIDVAL=MYLOGID,strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_CRIT,\
SENDIDPLOG

Definition at line 313 of file log.H.

#define IDPLINFO
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
MYLOGIDVAL=MYLOGID,strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_INFO,\
SENDIDPLOG

Definition at line 307 of file log.H.

#define LBS2   64

max size of function or file name

Definition at line 265 of file log.H.

Referenced by sendlog().

#define LDEBUG
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_DEBUG,SENDLOG

Definition at line 273 of file log.H.

#define LERROR
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_ERR,SENDLOG

Definition at line 277 of file log.H.

#define LFATAL
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_CRIT,SENDLOG

Definition at line 279 of file log.H.

#define LINFO
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_INFO,SENDLOG

Definition at line 275 of file log.H.

#define PLDEBUG
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_DEBUG,SENDPLOG

Definition at line 282 of file log.H.

#define PLERROR
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_ERR,SENDPLOG

Definition at line 286 of file log.H.

#define PLFATAL
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_CRIT,SENDPLOG

Definition at line 288 of file log.H.

#define PLINFO
Value:
MYLOGLIN=__LINE__,strncpy(MYLOGFUN, __FUNCTION__, LBS2-1),\
strncpy(MYLOGFIL, __FILE__, LBS2-1),MYLOGLEV=LOG_INFO,SENDPLOG

Definition at line 284 of file log.H.

#define REPORT_CURRENT_EXCEPTION   report_current_exception(__LINE__, __FILE__)

Use this inside of a catch(...) to report info about the caught exception.

There MUST be a current exception object in order for this to work (i.e., you must be inside of a catch block when you call REPORT_CURRENT_EXCEPTION).

Definition at line 359 of file log.H.

Referenced by main(), and NeoBrain::paramChanged().


Function Documentation

void report_current_exception ( const int  line,
const char *  file 
) throw ()

Helper function for REPORT_CURRENT_EXCEPTION macro.

Definition at line 315 of file log.C.

References rutz::demangled_name().


Variable Documentation

const int LFATAL_PRINTS_ABORT = (1 << 1)

Make LFATAL() print " -- ABORT" after each message.

This flag is on by default.

Definition at line 131 of file log.H.

Referenced by mexFunction().

const int LFATAL_THROWS = (1 << 0)

Make LFATAL() throw a std::exception rather than abort()ing.

This flag is on by default.

Definition at line 128 of file log.H.

const int LFATAL_XTRA_NOISY = (1 << 2)

Make LFATAL() print its msg to stderr/syslog AND put it in the std::exception.

This flag is on by default. Without it, LFATAL will only print its message to stderr or syslog if it will NOT be throwing a std::exception with that same message in std::exception::what().

Definition at line 137 of file log.H.

Referenced by mexFunction().

int LOG_FLAGS

Bitwise-or'ed combination of above flags.

Definition at line 56 of file log.C.

Referenced by main(), mexFunction(), and sendlog().

const int LOG_FULLTRACE = (1 << 3)

Whether to send log messages to stderr or to syslog.

Messages are sent to stderr if the LOG_FULLTRACE flag is set in LOG_FLAGS (this is the default), or are sent to syslog if the LOG_FULLTRACE flag is not set. Executables don't need to do anything special unless they need a non-default value.

Definition at line 143 of file log.H.

Referenced by main().

const char* MYLOGPREFIX

Null by default, but if set to non-null this string will be prefixed to every log message.

This can be useful if you have multiple threads or multiple processes all writing to stdout, and you want to have some way to distinguish their log messages.

Definition at line 66 of file log.C.

int MYLOGVERB
Generated on Sun May 8 08:42:52 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3