Generalise the logging function to a function pointer
This commit is contained in:
+21
-18
@@ -34,6 +34,9 @@ struct UCLogModule_setting {
|
|||||||
int log_level;
|
int log_level;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct UCLogArgs;
|
||||||
|
typedef void (*uc_vlog_func)(const struct UCLogArgs *args, const char *fmt, va_list ap);
|
||||||
|
|
||||||
struct UCLogDestination {
|
struct UCLogDestination {
|
||||||
struct TailQ entry;
|
struct TailQ entry;
|
||||||
enum UC_LOG_DESTINATION dest_type;
|
enum UC_LOG_DESTINATION dest_type;
|
||||||
@@ -57,6 +60,8 @@ struct UCLogDestination {
|
|||||||
struct UCLogRotateSettings rotate;
|
struct UCLogRotateSettings rotate;
|
||||||
} file;
|
} file;
|
||||||
} type;
|
} type;
|
||||||
|
|
||||||
|
uc_vlog_func log_func;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Helper struct for arguments to our main logging function */
|
/* 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.
|
//this list an anything contained within it.
|
||||||
static UC_TAILQ_HEAD(g_destinations);
|
static UC_TAILQ_HEAD(g_destinations);
|
||||||
static int uc_log_reopen_file(struct UCLogDestination *dest);
|
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
|
//Uses enum UC_LOG_LEVEL as index
|
||||||
static const char *const g_log_levels[] = {
|
static const char *const g_log_levels[] = {
|
||||||
"UNKNOWN_0",
|
"UNKNOWN_0",
|
||||||
@@ -185,6 +193,7 @@ struct UCLogDestination *uc_log_new_stderr(int log_level, int log_location)
|
|||||||
dest->type.file.size = 0;
|
dest->type.file.size = 0;
|
||||||
dest->log_level = log_level;;
|
dest->log_level = log_level;;
|
||||||
dest->log_location = log_location;
|
dest->log_location = log_location;
|
||||||
|
dest->log_func = uc_vlog_file;
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
@@ -217,6 +226,7 @@ struct UCLogDestination *uc_log_new_syslog(const char *ident, int facility, int
|
|||||||
dest->type.syslog.facility = facility;
|
dest->type.syslog.facility = facility;
|
||||||
dest->log_level = log_level;;
|
dest->log_level = log_level;;
|
||||||
dest->log_location = log_location;
|
dest->log_location = log_location;
|
||||||
|
dest->log_func = uc_vlog_syslog;
|
||||||
|
|
||||||
return dest;
|
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->type.file.rotate.policy = UC_LOG_ROTATE_NONE;
|
||||||
|
|
||||||
|
dest->log_func = uc_vlog_file;
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -453,16 +465,19 @@ void uc_log_init(struct UCLogModules *user_modules)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//hold g_log_lock while calling this
|
//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;
|
struct UCLogDestination *dest = args->dest;
|
||||||
char time_buf[48];
|
char time_buf[48];
|
||||||
struct tm t;
|
struct tm t;
|
||||||
|
time_t tstamp;
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
if (dest->type.file.file == NULL)
|
if (dest->type.file.file == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
tstamp = time(NULL);
|
||||||
|
|
||||||
localtime_r(&tstamp, &t);
|
localtime_r(&tstamp, &t);
|
||||||
if (!args->raw) {
|
if (!args->raw) {
|
||||||
snprintf(time_buf, sizeof time_buf, "%04d-%02d-%02d %02d:%02d:%02d",
|
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
|
rc = fflush(dest->type.file.file); //strictly not needed for stderr
|
||||||
// though stderr could be redirected to a file
|
// though stderr could be redirected to a file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uc_log_file_rotate_if_neeed(dest, tstamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
//hold g_log_lock while calling this
|
//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;
|
struct UCLogDestination *dest = args->dest;
|
||||||
|
|
||||||
@@ -705,25 +722,11 @@ void uc_logf(int log_level, int module, int raw,
|
|||||||
.raw = 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
|
//so make a copy
|
||||||
va_copy(apc, ap);
|
va_copy(apc, ap);
|
||||||
|
|
||||||
switch (dest->dest_type) {
|
dest->log_func(&args, fmt, apc);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
va_end(apc);
|
va_end(apc);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user