44 lines
948 B
C
44 lines
948 B
C
#include "log.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
#include <time.h>
|
|
|
|
void log_init(void) {
|
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
|
}
|
|
|
|
static void ts(char *out, size_t out_sz) {
|
|
time_t now = time(NULL);
|
|
struct tm tm;
|
|
localtime_r(&now, &tm);
|
|
strftime(out, out_sz, "%Y-%m-%d %H:%M:%S", &tm);
|
|
}
|
|
|
|
static void vlog(FILE *f, const char *level, const char *fmt, va_list ap) {
|
|
char t[32];
|
|
ts(t, sizeof(t));
|
|
fprintf(f, "%s %s: ", t, level);
|
|
vfprintf(f, fmt, ap);
|
|
fputc('\n', f);
|
|
fflush(f);
|
|
}
|
|
|
|
void log_info(const char *fmt, ...) {
|
|
va_list ap; va_start(ap, fmt);
|
|
vlog(stdout, "INFO", fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
void log_warn(const char *fmt, ...) {
|
|
va_list ap; va_start(ap, fmt);
|
|
vlog(stderr, "WARN", fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
void log_error(const char *fmt, ...) {
|
|
va_list ap; va_start(ap, fmt);
|
|
vlog(stderr, "ERROR", fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
|