Generalise the logging function to a function pointer

This commit is contained in:
Nils O. Selåsdal
2014-09-16 13:30:51 +02:00
parent 9ce3a4ebf8
commit 13e611c79d
+21 -18
View File
@@ -34,6 +34,9 @@ struct UCLogModule_setting {
int log_level;
};
struct UCLogArgs;
typedef void (*uc_vlog_func)(const struct UCLogArgs *args, const char *fmt, va_list ap);
struct UCLogDestination {
struct TailQ entry;
enum UC_LOG_DESTINATION dest_type;
@@ -57,6 +60,8 @@ struct UCLogDestination {
struct UCLogRotateSettings rotate;
} file;
} type;
uc_vlog_func log_func;
};
/* Helper struct for arguments to our main logging function */
@@ -87,6 +92,9 @@ static const struct UCLogModule g_unknown_module = {
//this list an anything contained within it.
static UC_TAILQ_HEAD(g_destinations);
static int uc_log_reopen_file(struct UCLogDestination *dest);
static inline void uc_log_file_rotate_if_neeed(struct UCLogDestination *dest, time_t tstamp);
static void uc_vlog_file(const struct UCLogArgs *args, const char *fmt, va_list ap);
static void uc_vlog_syslog(const struct UCLogArgs *args, const char *fmt, va_list ap);
//Uses enum UC_LOG_LEVEL as index
static const char *const g_log_levels[] = {
"UNKNOWN_0",
@@ -185,6 +193,7 @@ struct UCLogDestination *uc_log_new_stderr(int log_level, int log_location)
dest->type.file.size = 0;
dest->log_level = log_level;;
dest->log_location = log_location;
dest->log_func = uc_vlog_file;
return dest;
}
@@ -217,6 +226,7 @@ struct UCLogDestination *uc_log_new_syslog(const char *ident, int facility, int
dest->type.syslog.facility = facility;
dest->log_level = log_level;;
dest->log_location = log_location;
dest->log_func = uc_vlog_syslog;
return dest;
}
@@ -261,6 +271,8 @@ struct UCLogDestination *uc_log_new_file(const char *filename, int log_level, in
dest->type.file.rotate.policy = UC_LOG_ROTATE_NONE;
dest->log_func = uc_vlog_file;
return dest;
}
@@ -453,16 +465,19 @@ void uc_log_init(struct UCLogModules *user_modules)
}
//hold g_log_lock while calling this
static inline void uc_vlog_file(const struct UCLogArgs *args, time_t tstamp, const char *fmt, va_list ap)
static void uc_vlog_file(const struct UCLogArgs *args, const char *fmt, va_list ap)
{
struct UCLogDestination *dest = args->dest;
char time_buf[48];
struct tm t;
time_t tstamp;
int rc = 0;
if (dest->type.file.file == NULL)
return;
tstamp = time(NULL);
localtime_r(&tstamp, &t);
if (!args->raw) {
snprintf(time_buf, sizeof time_buf, "%04d-%02d-%02d %02d:%02d:%02d",
@@ -495,10 +510,12 @@ static inline void uc_vlog_file(const struct UCLogArgs *args, time_t tstamp, con
rc = fflush(dest->type.file.file); //strictly not needed for stderr
// though stderr could be redirected to a file
}
uc_log_file_rotate_if_neeed(dest, tstamp);
}
//hold g_log_lock while calling this
static inline void uc_vlog_syslog(const struct UCLogArgs *args, const char *fmt, va_list ap)
static void uc_vlog_syslog(const struct UCLogArgs *args, const char *fmt, va_list ap)
{
struct UCLogDestination *dest = args->dest;
@@ -705,25 +722,11 @@ void uc_logf(int log_level, int module, int raw,
.raw = raw
};
time_t tstamp;
//log func will mess with the va_list,
//log_func will mess with the va_list,
//so make a copy
va_copy(apc, ap);
switch (dest->dest_type) {
case UC_LDEST_SYSLOG:
uc_vlog_syslog(&args, fmt, apc);
break;
case UC_LDEST_STDERR:
tstamp = time(NULL);
uc_vlog_file(&args, tstamp,fmt, apc);
break;
case UC_LDEST_FILE:
tstamp = time(NULL);
uc_vlog_file(&args, tstamp, fmt, apc);
uc_log_file_rotate_if_neeed(dest, tstamp);
break;
}
dest->log_func(&args, fmt, apc);
va_end(apc);
}