Merge branch 'dev' of ssh://box.fiane.intra/var/lib/git/libucore into dev
This commit is contained in:
+87
-46
@@ -10,6 +10,11 @@
|
||||
#include "ucore/utils.h"
|
||||
|
||||
|
||||
//used to realise per destination settings
|
||||
struct uc_log_module_setting {
|
||||
int log_level;
|
||||
};
|
||||
|
||||
struct uc_log_destination {
|
||||
SLIST_ENTRY(uc_log_destination) entry;
|
||||
enum UC_LOG_DESTINATION dest_type;
|
||||
@@ -17,6 +22,7 @@ struct uc_log_destination {
|
||||
int log_level;
|
||||
//Whether to log the source file name and line number
|
||||
int log_location;
|
||||
struct uc_log_module_setting *module_settings;
|
||||
union {
|
||||
//for syslog openlog()
|
||||
struct {
|
||||
@@ -112,6 +118,22 @@ static inline int uc_ll_2_syslog(enum UC_LOG_LEVEL l)
|
||||
return LOG_INFO;
|
||||
}
|
||||
|
||||
static int uc_log_init_settings(struct uc_log_destination *dest)
|
||||
{
|
||||
int i;
|
||||
|
||||
dest->module_settings = calloc(g_modules.cnt, sizeof *dest->module_settings);
|
||||
if (dest->module_settings == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < g_modules.cnt; i++) {
|
||||
dest->module_settings[i].log_level = g_modules.mods[i].log_level;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct uc_log_destination *uc_log_new_stderr(int log_level, int log_location)
|
||||
{
|
||||
struct uc_log_destination *dest = calloc(1, sizeof *dest);
|
||||
@@ -119,6 +141,11 @@ struct uc_log_destination *uc_log_new_stderr(int log_level, int log_location)
|
||||
if(dest == NULL)
|
||||
return NULL;
|
||||
|
||||
if (uc_log_init_settings(dest) != 0) {
|
||||
free(dest);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dest->dest_type = UC_LDEST_STDERR;
|
||||
dest->type.file.file = stderr;
|
||||
dest->log_level = log_level;;
|
||||
@@ -135,9 +162,15 @@ struct uc_log_destination *uc_log_new_syslog(const char *ident, int facility, in
|
||||
if (dest == NULL)
|
||||
return NULL;
|
||||
|
||||
if (uc_log_init_settings(dest) != 0) {
|
||||
free(dest);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//copy the ident - though it's unused inside the struct for now
|
||||
id = strdup(ident);
|
||||
if (id == NULL) {
|
||||
free(dest->module_settings);
|
||||
free(dest);
|
||||
return NULL;
|
||||
}
|
||||
@@ -162,16 +195,25 @@ struct uc_log_destination *uc_log_new_file(const char *filename, int log_level,
|
||||
if (dest == NULL)
|
||||
return NULL;
|
||||
|
||||
if (uc_log_init_settings(dest) != 0) {
|
||||
free(dest);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
name = strdup(filename);
|
||||
if (name == NULL) {
|
||||
free(dest->module_settings);
|
||||
free(dest);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
f = fopen(filename, "a");
|
||||
if (f == NULL) {
|
||||
int saved_errno = errno;
|
||||
free(dest->module_settings);
|
||||
free(name);
|
||||
free(dest);
|
||||
errno = saved_errno;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -262,12 +304,22 @@ void uc_log_delete_destination(struct uc_log_destination *dest)
|
||||
free(dest->type.file.file_name);
|
||||
break;
|
||||
}
|
||||
free(dest->module_settings);
|
||||
|
||||
free(dest);
|
||||
|
||||
pthread_mutex_unlock(&g_log_lock);
|
||||
}
|
||||
|
||||
void uc_log_destination_set_loglevel(struct uc_log_destination *dest, enum UC_LOG_LEVEL level)
|
||||
{
|
||||
pthread_mutex_lock(&g_log_lock);
|
||||
|
||||
dest->log_level = level;
|
||||
|
||||
pthread_mutex_unlock(&g_log_lock);
|
||||
}
|
||||
|
||||
static int uc_log_reopen_file(struct uc_log_destination *dest)
|
||||
{
|
||||
int rc = 0;
|
||||
@@ -311,25 +363,6 @@ void uc_log_init(struct uc_log_modules *user_modules)
|
||||
g_modules = *user_modules;
|
||||
}
|
||||
|
||||
int uc_log_module_set_loglevel(int module, enum UC_LOG_LEVEL level)
|
||||
{
|
||||
int rc = -1;
|
||||
int i;
|
||||
|
||||
pthread_mutex_lock(&g_log_lock);
|
||||
|
||||
for (i = 0; i < g_modules.cnt; i++) {
|
||||
if (g_modules.mods[i].id == module) {
|
||||
g_modules.mods[i].log_level = level;
|
||||
rc = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&g_log_lock);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static inline void uc_vlog_file(const struct uc_log_args *args, const char *fmt, va_list ap)
|
||||
{
|
||||
@@ -429,43 +462,51 @@ void uc_logf(int log_level, int module, int raw,
|
||||
else
|
||||
log_module = &g_unknown_module;
|
||||
|
||||
//Check if the module should not be logged
|
||||
if (log_level < log_module->log_level)
|
||||
goto out;
|
||||
|
||||
//do logging for each destination
|
||||
SLIST_FOREACH(dest, &g_destinations, entry) {
|
||||
va_list apc;
|
||||
int module_log_level;
|
||||
|
||||
if (log_level >= dest->log_level) {
|
||||
const struct uc_log_args args = {
|
||||
.dest = dest,
|
||||
.module = log_module,
|
||||
.log_level = log_level,
|
||||
.file = file,
|
||||
.line = line,
|
||||
.raw = raw
|
||||
};
|
||||
|
||||
//log func will mess with the va_list,
|
||||
//so make a copy
|
||||
va_copy(apc, ap);
|
||||
//destnation level
|
||||
if (log_level < dest->log_level)
|
||||
continue;
|
||||
|
||||
switch (dest->dest_type) {
|
||||
case UC_LDEST_SYSLOG:
|
||||
uc_vlog_syslog(&args, fmt, apc);
|
||||
break;
|
||||
case UC_LDEST_STDERR:
|
||||
case UC_LDEST_FILE:
|
||||
uc_vlog_file(&args, fmt, apc);
|
||||
break;
|
||||
}
|
||||
//individual module level
|
||||
if (module >= 0 && module < g_modules.cnt)
|
||||
module_log_level = dest->module_settings[module].log_level;
|
||||
else //application bug. use global/unknown module
|
||||
module_log_level = log_module->log_level;
|
||||
|
||||
va_end(apc);
|
||||
if (log_level < module_log_level)
|
||||
continue;
|
||||
|
||||
const struct uc_log_args args = {
|
||||
.dest = dest,
|
||||
.module = log_module,
|
||||
.log_level = log_level,
|
||||
.file = file,
|
||||
.line = line,
|
||||
.raw = raw
|
||||
};
|
||||
|
||||
//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:
|
||||
case UC_LDEST_FILE:
|
||||
uc_vlog_file(&args, fmt, apc);
|
||||
break;
|
||||
}
|
||||
|
||||
va_end(apc);
|
||||
}
|
||||
|
||||
out:
|
||||
pthread_mutex_unlock(&g_log_lock);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user