ARPA2 Common Libraries  2.2.25
Macros | Functions
Exception and Error Handling

Macros

#define except_if(lab, nok)   do if (nok) { log_error ("Exceptional outcome for %s at %s:%d", #nok, __FILE__, __LINE__); goto lab; } while (0)
 except_if(lab,nok) More...
 
#define except_ifnot(lab, ok)   do if (!(ok)) { log_error ("Exceptional outcome for %s at %s:%d", #ok , __FILE__, __LINE__); goto lab; } while (0)
 except_ifnot(lab,ok) More...
 
#define assertxt(test, fmt, ...)   do if (!(test)) { log_critical ("\nAssertion Failed on %s at %s:%d (" fmt ")\n", #test, __FILE__, __LINE__ , ##__VA_ARGS__); exit (-6); } while (0)
 assertxt(test,fmt,...) More...
 
#define LOG_STYLE_DEFAULT   LOG_STYLE_STDERR
 
#define LOG_STYLE_NULL   0
 
#define LOG_STYLE_STDOUT   1
 
#define LOG_STYLE_STDERR   2
 
#define LOG_STYLE_SYSLOG   3
 
#define LOG_STYLE   LOG_STYLE_DEFAULT
 
#define log_critical(fmt, ...)   do { fprintf (stderr, "##\n## CRITICAL: " fmt "\n##\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
 
#define log_error(fmt, ...)   do { fprintf (stderr, "Error: " fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
 
#define log_warning(fmt, ...)   do { fprintf (stderr, "Warning: " fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
 
#define log_info(fmt, ...)   do { fprintf (stderr, fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
 
#define log_notice(fmt, ...)   do { fprintf (stderr, fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
 
#define log_debug(fmt, ...)   do { fprintf (stderr, "DEBUG: " fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)
 
#define log_errno(fmt, ...)   log_error ("%s: " fmt, strerror (errno) , ##__VA_ARGS__)
 log_errno(fmt,...) More...
 
#define log_detail(fmt, ...)   log_debug (fmt , ##__VA_ARGS__)
 log_detail(fmt,...) More...
 
#define _ARPA2_EXCEPT_H
 

Functions

static void log_data (const char *descr, const void *blk, uint32_t blklen, uint32_t maxsz)
 log_data(descr,ptr,len,maxsz) More...
 

Detailed Description

The purpose of <arpa2/except.h> is to provide with lavish functionality for providing user feedback. Easy macro definitions allow the output to be redirected or suppressed. As a result, you can be highly verbose without necessarily weighing on runtime performance. There is even an extra symbol DEBUG_DETAIL which allows extra macros log_detail() to dump specifics beyond even the debugging level, and log_data() to make dumps of data packets (such as might travel over a network).

If you include this file, your CMakeLists should include(ExceptionHandling)

Configuration option: LOG_STYLE may be set to one of:

Macro Definition Documentation

◆ assertxt

#define assertxt (   test,
  fmt,
  ... 
)    do if (!(test)) { log_critical ("\nAssertion Failed on %s at %s:%d (" fmt ")\n", #test, __FILE__, __LINE__ , ##__VA_ARGS__); exit (-6); } while (0)

assertxt(test,fmt,...)

Ascertain that test is true; otherwise, log a message with the test, file/line information and a formatted string before exiting just like assert() would.

This is the versions used when DEBUG is defined.

◆ except_if

#define except_if (   lab,
  nok 
)    do if (nok) { log_error ("Exceptional outcome for %s at %s:%d", #nok, __FILE__, __LINE__); goto lab; } while (0)

except_if(lab,nok)

Test a condition and take an exceptional course on nok.

The exceptional course is to log an error including the condition and file/line information, then jump to the label lab (presumably for cleanup). Though simple, this mimics a reasonable part of exception handling as it is known in more advanced languages. If a value in errno is needed, this is assumed to already be present.

These are the versions used when DEBUG is defined.

◆ except_ifnot

#define except_ifnot (   lab,
  ok 
)    do if (!(ok)) { log_error ("Exceptional outcome for %s at %s:%d", #ok , __FILE__, __LINE__); goto lab; } while (0)

except_ifnot(lab,ok)

Test a condition and take an exceptional course on !ok.

The exceptional course is to log an error including the condition and file/line information, then jump to the label lab (presumably for cleanup). Though simple, this mimics a reasonable part of exception handling as it is known in more advanced languages. If a value in errno is needed, this is assumed to already be present.

These are the versions used when DEBUG is defined.

◆ log_critical

#define log_critical (   fmt,
  ... 
)    do { fprintf (stderr, "##\n## CRITICAL: " fmt "\n##\n" , ##__VA_ARGS__); fflush (stderr); } while (0)

Produce error output. This routes to the logging facility of choice, at the critical level.

◆ log_debug

#define log_debug (   fmt,
  ... 
)    do { fprintf (stderr, "DEBUG: " fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)

Produce debugging output. This routes to the logging facility of choice, but only when DEBUG is defined; otherwise, no formatting or logging takes place.

◆ log_detail

#define log_detail (   fmt,
  ... 
)    log_debug (fmt , ##__VA_ARGS__)

log_detail(fmt,...)

Produce incredibly detailed debugging output. For instance, the choices made while parsing a new document type. Or the bytes that pop in and out of a network interface. Only do this when DEBUG_DETAIL is defined (which implies DEBUG) by passing it over to the debug() code.

◆ log_errno

#define log_errno (   fmt,
  ... 
)    log_error ("%s: " fmt, strerror (errno) , ##__VA_ARGS__)

log_errno(fmt,...)

This produces a similar output to perror(), including a string form of errno. By default, the string is formed with strerror(), but if used after #include <arpa2/com_err.h> then the COM_ERR extension with error_message() is used instead. Note that this works best when all error tables XXXX have been previously setup by calling the generated initialize_XXXX_error_table() functions.

◆ log_error

#define log_error (   fmt,
  ... 
)    do { fprintf (stderr, "Error: " fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)

Produce error output. This routes to the logging facility of choice, at the error level.

◆ log_info

#define log_info (   fmt,
  ... 
)    do { fprintf (stderr, fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)

Produce error output. This routes to the logging facility of choice, at the info level.

◆ log_notice

#define log_notice (   fmt,
  ... 
)    do { fprintf (stderr, fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)

Produce error output. This routes to the logging facility of choice, at the notice level.

◆ log_warning

#define log_warning (   fmt,
  ... 
)    do { fprintf (stderr, "Warning: " fmt "\n" , ##__VA_ARGS__); fflush (stderr); } while (0)

Produce error output. This routes to the logging facility of choice, at the warning level.

Function Documentation

◆ log_data()

static void log_data ( const char *  descr,
const void *  blk,
uint32_t  blklen,
uint32_t  maxsz 
)
inlinestatic

log_data(descr,ptr,len,maxsz)

Print a packet in hexadecimal, after first printing the description and an equals sign. The entire dump is on a single line, so it is easily accessible to copy/paste. Normally the size is printed as offered, but if maxsz is over zero it constrains the number of bytes to that maximum.