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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/******************************************************************************
*
* Part of the log4c examples.
*
* Along with example_formatters.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 <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#include <time.h>
#include <log4c.h>
/* global var to force linking with the appender when using syslog */
static int log4c_to_syslog_priority(int a_priority);
/*
* This one is experimental and not in use. But its a good example to see all the things
* you have access to during the call to the appender
*/
static int s13_file_append(log4c_appender_t* this,
const log4c_logging_event_t* a_event)
{
FILE* fp = log4c_appender_get_udata(this);
/*
return fprintf(fp, "[%s] [%s] [%d] [%d] [%s] [%s] [%d]\n%s",
log4c_appender_get_name(this),
a_event->evt_category,
a_event->evt_priority,
a_event->evt_timestamp.tv_sec*1000000 + a_event->evt_timestamp.tv_usec,
a_event->evt_msg,
a_event->evt_loc->loc_file,
a_event->evt_loc->loc_line,
a_event->evt_rendered_msg);
*/
return fprintf(fp, "%s\n",
a_event->evt_rendered_msg
);
}
/*******************************************************************************/
static int etf_open(log4c_appender_t* this)
{
FILE* fp = log4c_appender_get_udata(this);
if (fp)
return 0;
if ( (fp = fopen(log4c_appender_get_name(this), "a+")) == NULL)
fp = stderr;
/* unbuffered mode */
setbuf(fp, NULL);
log4c_appender_set_udata(this, fp);
return 0;
}
/*******************************************************************************/
static int etf_close(log4c_appender_t* this)
{
FILE* fp = log4c_appender_get_udata(this);
return (fp ? fclose(fp) : 0);
}
const log4c_appender_type_t log4c_appender_type_s13_file = {
"s13_file",
etf_open,
s13_file_append,
etf_close,
};
/*******************************************************************************/
/*
* The log4c stderr adds a "[stderr] " in front of the message
* it is logging. this one doesn't, and leaves the formatting
* descisions up to the formatter
*/
static int s13_stderr_append(log4c_appender_t* this,
const log4c_logging_event_t* a_event)
{
return fprintf(stderr, "%s\n",
a_event->evt_rendered_msg
);
}
static int s13_stderr_open(log4c_appender_t* this)
{
/* fprintf (stderr,"running s13_stderr appender open command now\n"); */
return 0;
}
const log4c_appender_type_t log4c_appender_type_s13_stderr = {
"s13_stderr",
s13_stderr_open,
s13_stderr_append,
NULL,
};
#ifdef HAVE_SYSLOG_H
/**************************/
/* User appender */
/**************************/
/*******************************************************************************/
static int syslog_user_open(log4c_appender_t* this)
{
openlog(log4c_appender_get_name(this), LOG_PID, LOG_USER);
return 0;
}
/*******************************************************************************/
static int syslog_user_append(log4c_appender_t* this,
const log4c_logging_event_t* a_event)
{
syslog(log4c_to_syslog_priority(a_event->evt_priority) | LOG_USER,
"%s", a_event->evt_rendered_msg);
return 0;
}
/*******************************************************************************/
static int syslog_user_close(log4c_appender_t* this)
{
closelog();
return 0;
}
/*******************************************************************************/
const log4c_appender_type_t log4c_appender_type_syslog_user = {
"syslog_user",
syslog_user_open,
syslog_user_append,
syslog_user_close,
};
/**************************/
/* Local 0 appender */
/**************************/
/*******************************************************************************/
static int syslog_local0_open(log4c_appender_t* this)
{
openlog(log4c_appender_get_name(this), LOG_PID, LOG_LOCAL0);
return 0;
}
/*******************************************************************************/
static int syslog_local0_append(log4c_appender_t* this,
const log4c_logging_event_t* a_event)
{
syslog(log4c_to_syslog_priority(a_event->evt_priority) | LOG_LOCAL0,
"%s", a_event->evt_rendered_msg);
return 0;
}
/*******************************************************************************/
static int syslog_local0_close(log4c_appender_t* this)
{
closelog();
return 0;
}
/*******************************************************************************/
const log4c_appender_type_t log4c_appender_type_syslog_local0 = {
"syslog_local0",
syslog_local0_open,
syslog_local0_append,
syslog_local0_close,
};
static int log4c_to_syslog_priority(int a_priority)
{
static int priorities[] = {
LOG_EMERG,
LOG_ALERT,
LOG_CRIT,
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO,
LOG_DEBUG
};
int result;
a_priority++;
a_priority /= 100;
if (a_priority < 0) {
result = LOG_EMERG;
} else if (a_priority > 7) {
result = LOG_DEBUG;
} else {
result = priorities[a_priority];
}
return result;
}
#endif /* HAVE_SYSLOG_H */
/*****************************************************/
static const log4c_appender_type_t * const appender_types[] = {
#ifdef HAVE_SYSLOG_H
&log4c_appender_type_syslog_local0,
&log4c_appender_type_syslog_user,
#endif
&log4c_appender_type_s13_file,
&log4c_appender_type_s13_stderr
};
int nappender_types =
(int)(sizeof(appender_types) / sizeof(appender_types[0]));
int init_example_appenders(){
int rc = 0; int i = 0;
for (i = 0; i < nappender_types; i++)
log4c_appender_type_set(appender_types[i]);
return(rc);
}
|