Log

From Sharpfin
Jump to navigation Jump to search

Overview

The Libreciva log function provides a support function to store and output log messages.

In order to use the library function, you must include the header file:

<syntaxhighlight>

  1. include "log.h"

</syntaxhighlight>

void log_init(char *progname, enum log_to dest, enum log_level, int colour)

This function must be called to initialise the log system. The parameters are as follows:

char *progname

This is the name of the program.

enum log_to dest

This is the destination of the logger - it is either:

  • LOG_TO_STDERR
  • LOG_TO_SYSLOG

enum log_level

This is the level of logging to report, it is one of the following:

  • LG_FTL
  • LG_ERR
  • LG_WRN
  • LG_INF
  • LG_DBG

void logf(enum log_level level, char *format, ...)

This function adds a log entry to the logfile. The level is one of:

  • LG_FTL
  • LG_ERR
  • LG_WRN
  • LG_INF
  • LG_DBG

And the "format, ..." arguments are printf-compatible format string and arguments.

void log_hexdump(char *txt, char *data, int len)

This function dumps len bytes of data to the logfile, with a title of txt

Example

<syntaxhighlight>

  1. include "log.h"

main() {

 int x=2 ;
 log_init("testprog", LOG_TO_STDERR, LG_ERR, 0);
 logf(LG_ERR, "This is an error message, x=%d", x);
 logf(LG_WRN, "This will not be output as the log_init level is LG_ERR");
 logf(LG_FTL, "This log entry will cause a program exit");

} </syntaxhighlight>