1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/******************************************************************************
*
* Part of the log4c examples.
*
* Along with example_appenders.c this file is used to create a small
* library of custom appenders and formatters.
*
* This library is excercised using application_2 and a sample log4crc
* config file.
*
*****************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <log4c.h>
/* Defined in example_appenders.c */
extern int init_example_appenders(void);
/**********************************************************************
*
* Formatted to put [category] out at the front of the message
*
**********************************************************************/
static const char* cat_format(
const log4c_layout_t* a_layout,
const log4c_logging_event_t*a_event)
{
static char buffer[4096];
/*
* For this formatter we put the category up front in the log message
*/
sprintf(buffer, "[%s][LINE:%d][FILE:%s] %s", a_event->evt_category,
a_event->evt_loc->loc_line, a_event->evt_loc->loc_file, a_event->evt_msg);
return buffer;
}
const log4c_layout_type_t log4c_layout_type_cat = {
"s13_cat",
cat_format,
};
static const char* none_format(
const log4c_layout_t* a_layout,
const log4c_logging_event_t*a_event)
{
static char buffer[4096];
return buffer;
}
const log4c_layout_type_t log4c_layout_type_none = {
"s13_none",
none_format,
};
/**********************************************************************/
/*
* Formatted to mock up an xml format.
*
**********************************************************************/
static const char* xml_format(
const log4c_layout_t* a_layout,
const log4c_logging_event_t*a_event)
{
static char buffer[4096];
/*
* For this formatter we put the category up front in the log message
*/
sprintf(buffer, "<logmessage><category>%s</category><message>%s</message></logmessage>", a_event->evt_category, a_event->evt_msg);
return buffer;
}
const log4c_layout_type_t log4c_layout_type_xml = {
"s13_xml",
xml_format,
};
/*****************************/
/*
* Here provide an init routine for this lib
*
******************************/
static const log4c_layout_type_t * const layout_types[] = {
&log4c_layout_type_xml,
&log4c_layout_type_none,
&log4c_layout_type_cat
};
static int nlayout_types =
(int)(sizeof(layout_types) / sizeof(layout_types[0]));
int init_example_formatters(){
int rc = 0; int i = 0;
for (i = 0; i < nlayout_types; i++)
log4c_layout_type_set(layout_types[i]);
return(rc);
}
int init_examples_lib() {
init_example_formatters();
init_example_appenders();
return(0);
}
|