Usage

You sprinkle log statements throughout your C code, and can turn them on/off via the config file.

Finer control of the log statements is achieved by logging to categories, like URLDecodeStep?, or MyBug67Fix?. Each category can be turned on/off via the config file. So, you could turn off all the log messages except for those concerning URL-decoding, without recompiling.

Categories are hierachical. If you have a category whose name is "root", then a category whose name is "root.dhcp" is a child of root. In the config file, all the properties of a category are inherited by its children unless they override it.

Yet finer control is achieved by assigning a priority to each message (each call of log()). If the config file specifies a priority for a category, then a message won't be logged unless the log() call gives an equal/higher priority. Some constants are predefined for you. So, for example, you could limit logging to FATAL for some category, and TRACE for some other category. You can think of priority as "severity".

NB There seem to be bugs in log4c's XML parser: comments terminate the parsing, thus ignoring further definitions.

The output looks like this (depending on the appender you choose):

[stdout] WARN     root.bob - bob
[stderr] 20040209 17:17:58.013 ERROR    root.jane - jane

Steps

  1. Edit your code
    1. Add a log() call to your code.
      • log4c_category_xxx(catxx,"message you want, as a sprintf string %d",99)
    2. Decide on the category (e.g. what feature, function, purpose), and priority/severity.
One of:
  LOG4C_PRIORITY_FATAL = 000,
  LOG4C_PRIORITY_ALERT = 100,
  LOG4C_PRIORITY_CRIT = 200,
  LOG4C_PRIORITY_ERROR = 300,
  LOG4C_PRIORITY_WARN = 400,
  LOG4C_PRIORITY_NOTICE = 500,
  LOG4C_PRIORITY_INFO = 600,
  LOG4C_PRIORITY_DEBUG = 700,
  LOG4C_PRIORITY_TRACE = 800,
  LOG4C_PRIORITY_NOTSET = 900,
  LOG4C_PRIORITY_UNKNOWN = 1000
  1. Edit the config file
    1. Create your config file (./log4crc, or ~/.log4crc, or ${LOG4C_RCPATH}/log4rc)
    2. Setup the skeleton log4crc file... 6 Add your category, the priority is the cutoff point for messages (log() calls of lower priority won't print).
      • <category name="TheCategoryNameToUseIn_category_get" priority="notice" appender="stdout"/>
    3. Decide on the "appender", it controls where the message goes, and it's format. Apparently "stdout", "stderr", and "syslog" are provided. Each formats the message a little differently.
  2. Make your code: don't forget to add the -l log4c switch to gcc to link in log4c.
  3. Edit the config file to turn on/off messages.

In log4crc
define a category
   <category name="SCSIOp" priority="trace" appender="stdout">
   where:
     Your C program will log messages in the category NAME,
     If the attempt to log has a lower priority, the message is ignored,
     The message will be output in a format and to a location defined by the APPENDER.

someRoutine
{
    // get the "category" 
    log4c_category_t*   cat = log4c_category_get("a category");

    ...

    // "print" to it
    log4c_category_log(cat,priority,sprintfFmt,args...)
// where priority is equiv to routine _error, _warn, _trace, etc:
  LOG4C_PRIORITY_FATAL = 000,
  LOG4C_PRIORITY_ALERT = 100,
  LOG4C_PRIORITY_CRIT = 200,
  LOG4C_PRIORITY_ERROR = 300,
  LOG4C_PRIORITY_WARN = 400,
  LOG4C_PRIORITY_NOTICE = 500,
  LOG4C_PRIORITY_INFO = 600,
  LOG4C_PRIORITY_DEBUG = 700,
  LOG4C_PRIORITY_TRACE = 800,
  LOG4C_PRIORITY_NOTSET = 900,
  LOG4C_PRIORITY_UNKNOWN = 1000 

}