87 lines
2.2 KiB
C
87 lines
2.2 KiB
C
|
#include <stdio.h>
|
||
|
#include <time.h>
|
||
|
|
||
|
#include "lnm/common.h"
|
||
|
#include "lnm/log_internal.h"
|
||
|
|
||
|
const char *lnm_log_level_names[] = {"DEBUG ", "INFO ", "NOTICE ",
|
||
|
"WARNING ", "ERROR ", "CRITICAL"};
|
||
|
|
||
|
lnm_logger *global_logger = NULL;
|
||
|
|
||
|
lnm_err lnm_log_init_global() {
|
||
|
global_logger = calloc(1, sizeof(lnm_logger));
|
||
|
|
||
|
return global_logger == NULL ? lnm_err_failed_alloc : lnm_err_ok;
|
||
|
}
|
||
|
|
||
|
lnm_err lnm_logger_stream_register(lnm_logger *logger,
|
||
|
lnm_logger_stream *stream) {
|
||
|
lnm_logger_stream **new =
|
||
|
logger->streams.len == 0
|
||
|
? malloc(sizeof(lnm_logger_stream *))
|
||
|
: realloc(logger->streams.arr,
|
||
|
(logger->streams.len + 1) * sizeof(lnm_logger_stream *));
|
||
|
|
||
|
if (new == NULL) {
|
||
|
return lnm_err_failed_alloc;
|
||
|
}
|
||
|
|
||
|
new[logger->streams.len] = stream;
|
||
|
logger->streams.arr = new;
|
||
|
logger->streams.len++;
|
||
|
|
||
|
return lnm_err_ok;
|
||
|
}
|
||
|
|
||
|
lnm_err lnm_log_register_stdout(lnm_log_level level) {
|
||
|
lnm_logger_stream *stream = malloc(sizeof(lnm_logger_stream));
|
||
|
|
||
|
if (stream == NULL) {
|
||
|
return lnm_err_failed_alloc;
|
||
|
}
|
||
|
|
||
|
stream->type = lnm_logger_stream_type_file;
|
||
|
stream->ptr = stdout;
|
||
|
stream->level = level;
|
||
|
|
||
|
LNM_RES2(lnm_logger_stream_register(global_logger, stream), free(stream));
|
||
|
|
||
|
return lnm_err_ok;
|
||
|
}
|
||
|
|
||
|
void lnm_vlog(lnm_log_level level, const char *section, const char *fmt,
|
||
|
va_list ap) {
|
||
|
char date_str[32];
|
||
|
|
||
|
time_t now = time(NULL);
|
||
|
strftime(date_str, sizeof(date_str) - 1, "%Y-%m-%d %H:%M:%S",
|
||
|
localtime(&now));
|
||
|
|
||
|
for (size_t i = 0; i < global_logger->streams.len; i++) {
|
||
|
lnm_logger_stream *stream = global_logger->streams.arr[i];
|
||
|
|
||
|
if (level < stream->level) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
switch (stream->type) {
|
||
|
case lnm_logger_stream_type_file:
|
||
|
fprintf(stream->ptr, "[%s][%s][%s] ", date_str,
|
||
|
lnm_log_level_names[level], section);
|
||
|
vfprintf(stream->ptr, fmt, ap);
|
||
|
fprintf(stream->ptr, "\n");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
va_end(ap);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void lnm_log(lnm_log_level level, const char *section, const char *fmt, ...) {
|
||
|
va_list ap;
|
||
|
va_start(ap, fmt);
|
||
|
lnm_vlog(level, section, fmt, ap);
|
||
|
va_end(ap);
|
||
|
}
|