Restructure logging internal. Add raw logging capability
This commit is contained in:
+60
-27
@@ -37,6 +37,7 @@ struct uc_log_args {
|
||||
int log_level;
|
||||
const char *file;
|
||||
int line;
|
||||
int raw;
|
||||
};
|
||||
|
||||
|
||||
@@ -299,17 +300,19 @@ int uc_log_module_set_loglevel(int module, enum UC_LOG_LEVEL level)
|
||||
return rc;
|
||||
}
|
||||
|
||||
static inline void uc_vlog_internal(const struct uc_log_args *args, const char *fmt, va_list ap)
|
||||
static inline void uc_vlog_file(const struct uc_log_args *args, const char *fmt, va_list ap)
|
||||
{
|
||||
struct uc_log_destination *dest = args->dest;
|
||||
|
||||
if(dest->dest_type != UC_LDEST_SYSLOG && dest->type.file.file != NULL) {
|
||||
char time_buf[48];
|
||||
time_t now;
|
||||
struct tm t;
|
||||
char time_buf[48];
|
||||
time_t now;
|
||||
struct tm t;
|
||||
|
||||
now = time(NULL);
|
||||
localtime_r(&now, &t);
|
||||
if(dest->type.file.file == NULL)
|
||||
return;
|
||||
|
||||
now = time(NULL);
|
||||
localtime_r(&now, &t);
|
||||
if(!args->raw) {
|
||||
snprintf(time_buf, sizeof time_buf, "%04d-%02d-%02d %02d:%02d:%02d",
|
||||
t.tm_year + 1900,
|
||||
t.tm_mon + 1,
|
||||
@@ -318,42 +321,61 @@ static inline void uc_vlog_internal(const struct uc_log_args *args, const char *
|
||||
t.tm_min,
|
||||
t.tm_sec);
|
||||
|
||||
time_buf[sizeof time_buf -1] = 0;
|
||||
|
||||
|
||||
if(dest->log_location) {
|
||||
fprintf(dest->type.file.file, "[%s] %s %s:%d [%s] : ", uc_ll_2_str(args->log_level),
|
||||
time_buf, args->file, args->line, args->module->short_name);
|
||||
fprintf(dest->type.file.file, "[%s] %s %s:%d [%s] : ",
|
||||
uc_ll_2_str(args->log_level), time_buf, args->file,
|
||||
args->line, args->module->short_name);
|
||||
} else {
|
||||
fprintf(dest->type.file.file, "[%s] %s [%s]: ",
|
||||
uc_ll_2_str(args->log_level), time_buf, args->module->short_name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
vfprintf(dest->type.file.file, fmt, ap);
|
||||
vfprintf(dest->type.file.file, fmt, ap);
|
||||
|
||||
fflush(dest->type.file.file); //strictly not needed for stderr
|
||||
// though stderr could be redirected to a file
|
||||
fflush(dest->type.file.file); //strictly not needed for stderr
|
||||
// though stderr could be redirected to a file
|
||||
}
|
||||
|
||||
} else if(dest->dest_type == UC_LDEST_SYSLOG) {
|
||||
int pri = uc_ll_2_syslog(args->log_level);
|
||||
static inline void uc_vlog_syslog(const struct uc_log_args *args, const char *fmt, va_list ap)
|
||||
{
|
||||
struct uc_log_destination *dest = args->dest;
|
||||
|
||||
int pri = uc_ll_2_syslog(args->log_level);
|
||||
|
||||
//go via a buffer as we must do just 1 syslog call
|
||||
char buf[4096];
|
||||
int len = 0;
|
||||
//go via a buffer as we must do just 1 syslog call
|
||||
char buf[4096];
|
||||
int offset = 0;
|
||||
|
||||
buf[0] = 0;
|
||||
if(!args->raw) {
|
||||
int rc;
|
||||
if(dest->log_location) {
|
||||
len = snprintf(buf, sizeof buf, "%s:%d ", args->file, args->line);
|
||||
rc = snprintf(buf, sizeof buf, "%s:%d ", args->file, args->line);
|
||||
if(rc < 0)
|
||||
goto log;
|
||||
offset += rc;
|
||||
|
||||
}
|
||||
|
||||
len += snprintf(&buf[len], sizeof buf - (size_t)len, "[%s] ", args->module->short_name);
|
||||
|
||||
vsnprintf(&buf[len], sizeof buf - (size_t)len, fmt, ap);
|
||||
|
||||
syslog(pri, "%s", buf);
|
||||
|
||||
rc = snprintf(&buf[offset], sizeof buf - (size_t)offset, "[%s] ",
|
||||
args->module->short_name);
|
||||
if(rc < 0)
|
||||
goto log;
|
||||
offset +=rc;
|
||||
}
|
||||
|
||||
vsnprintf(&buf[offset], sizeof buf - (size_t)offset, fmt, ap);
|
||||
log:
|
||||
syslog(pri, "%s", buf);
|
||||
}
|
||||
|
||||
void uc_logf(int log_level, int module, const char *file, int line, const char *fmt, ...)
|
||||
void uc_logf(int log_level, int module, int raw,
|
||||
const char *file, int line, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
struct uc_log_destination *dest;
|
||||
@@ -386,12 +408,23 @@ void uc_logf(int log_level, int module, const char *file, int line, const char *
|
||||
.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);
|
||||
uc_vlog_internal(&args, fmt, apc);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user