26 lines
576 B
C
26 lines
576 B
C
|
#include <stdio.h>
|
||
|
#include <stdarg.h>
|
||
|
#include <time.h>
|
||
|
#include <sys/time.h>
|
||
|
#include "log.h"
|
||
|
|
||
|
static const char *severities[] = { "FATAL", "ERROR", "WARN ", "INFO ", "DEBUG" };
|
||
|
|
||
|
void log(int level, const char *format, ...)
|
||
|
{
|
||
|
va_list argp;
|
||
|
struct timeval tv;
|
||
|
struct tm tm;
|
||
|
char timestamp[32];
|
||
|
char buf[1024];
|
||
|
|
||
|
va_start(argp, format);
|
||
|
vsnprintf(buf, 1024, format, argp);
|
||
|
va_end(argp);
|
||
|
|
||
|
gettimeofday(&tv);
|
||
|
localtime_r(&(tv.tv_sec), &tm)
|
||
|
strftime(timestamp, 32, "%F %T", &tm);
|
||
|
printf("%s.%06u %s %s\n", timestamp, tv.tv_usec, severities[level], buf);
|
||
|
}
|