Restructure logging internal. Add raw logging capability
This commit is contained in:
+48
-15
@@ -37,6 +37,7 @@ struct uc_log_args {
|
|||||||
int log_level;
|
int log_level;
|
||||||
const char *file;
|
const char *file;
|
||||||
int line;
|
int line;
|
||||||
|
int raw;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -299,17 +300,19 @@ int uc_log_module_set_loglevel(int module, enum UC_LOG_LEVEL level)
|
|||||||
return rc;
|
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;
|
struct uc_log_destination *dest = args->dest;
|
||||||
|
|
||||||
if(dest->dest_type != UC_LDEST_SYSLOG && dest->type.file.file != NULL) {
|
|
||||||
char time_buf[48];
|
char time_buf[48];
|
||||||
time_t now;
|
time_t now;
|
||||||
struct tm t;
|
struct tm t;
|
||||||
|
|
||||||
|
if(dest->type.file.file == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
now = time(NULL);
|
now = time(NULL);
|
||||||
localtime_r(&now, &t);
|
localtime_r(&now, &t);
|
||||||
|
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",
|
||||||
t.tm_year + 1900,
|
t.tm_year + 1900,
|
||||||
t.tm_mon + 1,
|
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_min,
|
||||||
t.tm_sec);
|
t.tm_sec);
|
||||||
|
|
||||||
|
time_buf[sizeof time_buf -1] = 0;
|
||||||
|
|
||||||
|
|
||||||
if(dest->log_location) {
|
if(dest->log_location) {
|
||||||
fprintf(dest->type.file.file, "[%s] %s %s:%d [%s] : ", uc_ll_2_str(args->log_level),
|
fprintf(dest->type.file.file, "[%s] %s %s:%d [%s] : ",
|
||||||
time_buf, args->file, args->line, args->module->short_name);
|
uc_ll_2_str(args->log_level), time_buf, args->file,
|
||||||
|
args->line, args->module->short_name);
|
||||||
} else {
|
} else {
|
||||||
fprintf(dest->type.file.file, "[%s] %s [%s]: ",
|
fprintf(dest->type.file.file, "[%s] %s [%s]: ",
|
||||||
uc_ll_2_str(args->log_level), time_buf, args->module->short_name);
|
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
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
} else if(dest->dest_type == UC_LDEST_SYSLOG) {
|
|
||||||
int pri = uc_ll_2_syslog(args->log_level);
|
int pri = uc_ll_2_syslog(args->log_level);
|
||||||
|
|
||||||
//go via a buffer as we must do just 1 syslog call
|
//go via a buffer as we must do just 1 syslog call
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
int len = 0;
|
int offset = 0;
|
||||||
|
|
||||||
|
buf[0] = 0;
|
||||||
|
if(!args->raw) {
|
||||||
|
int rc;
|
||||||
if(dest->log_location) {
|
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);
|
rc = snprintf(&buf[offset], sizeof buf - (size_t)offset, "[%s] ",
|
||||||
|
args->module->short_name);
|
||||||
vsnprintf(&buf[len], sizeof buf - (size_t)len, fmt, ap);
|
if(rc < 0)
|
||||||
|
goto log;
|
||||||
|
offset +=rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
vsnprintf(&buf[offset], sizeof buf - (size_t)offset, fmt, ap);
|
||||||
|
log:
|
||||||
syslog(pri, "%s", buf);
|
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;
|
va_list ap;
|
||||||
struct uc_log_destination *dest;
|
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,
|
.log_level = log_level,
|
||||||
.file = file,
|
.file = file,
|
||||||
.line = line,
|
.line = line,
|
||||||
|
.raw = raw
|
||||||
};
|
};
|
||||||
|
|
||||||
//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);
|
||||||
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);
|
va_end(apc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-3
@@ -210,12 +210,18 @@ int uc_log_reopen_files(void);
|
|||||||
*/
|
*/
|
||||||
void uc_log_init(struct uc_log_modules *user_modules);
|
void uc_log_init(struct uc_log_modules *user_modules);
|
||||||
|
|
||||||
void uc_logf(int log_level, int module, const char *file, int line, const char *fmt, ...) __attribute__((format(printf, 5, 6)));
|
void uc_logf(int log_level, int module, int raw,
|
||||||
|
const char *file, int line, const char *fmt, ...)
|
||||||
|
__attribute__((format(printf, 6, 7)));
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
# define UC_DEBUGF(mod, fmt, ...) uc_logf(UC_LL_DEBUG, mod, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
|
# define UC_DEBUGF(mod, fmt, ...)\
|
||||||
|
uc_logf(UC_LL_DEBUG, mod,0 , __FILE__, __LINE__, fmt, ## __VA_ARGS__)
|
||||||
|
# define UC_DEBUGFR(mod, fmt, ...)\
|
||||||
|
uc_logf(UC_LL_DEBUG, mod,1 , , __LINE__, fmt, ## __VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
# define UC_DEBUGF(mod, fmt, ...)
|
# define UC_DEBUGF(mod, fmt, ...)
|
||||||
|
# define UC_DEBUGFR(mod, fmt, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** Main macro that should be used for logging.
|
/** Main macro that should be used for logging.
|
||||||
@@ -228,9 +234,16 @@ void uc_logf(int log_level, int module, const char *file, int line, const char *
|
|||||||
* @param fmt printf style format string
|
* @param fmt printf style format string
|
||||||
* @param ... printf style arguments
|
* @param ... printf style arguments
|
||||||
*/
|
*/
|
||||||
#define UC_LOGF(lvl, mod, fmt, ...) uc_logf(lvl, mod, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
|
#define UC_LOGF(lvl, mod, fmt, ...) \
|
||||||
|
uc_logf(lvl, mod, 0, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
|
||||||
|
|
||||||
|
|
||||||
|
/** Raw logging ,Like UC_LOGF, but only prints the supplied
|
||||||
|
* data, not timestamp, log level etc.
|
||||||
|
*/
|
||||||
|
#define UC_LOGFR(lvl, mod, fmt, ...) \
|
||||||
|
uc_logf(lvl, mod, 1, __FILE__, __LINE__, fmt, ## __VA_ARGS__)
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user