2019-11-29 01:52:56 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "log.h"
|
|
|
|
|
2019-12-07 13:07:59 -07:00
|
|
|
/* string equivalents to the severity values */
|
2019-11-29 01:52:56 -07:00
|
|
|
static const char *severities[] = { "FATAL", "ERROR", "WARN ", "INFO ", "DEBUG" };
|
|
|
|
|
2019-12-07 19:44:48 -07:00
|
|
|
static FILE *logfile = NULL; /* log file pointer */
|
2019-12-07 13:07:59 -07:00
|
|
|
|
2019-11-29 02:18:02 -07:00
|
|
|
void Log(int level, const char *format, ...)
|
2019-11-29 01:52:56 -07:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2019-11-29 02:18:02 -07:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
localtime_r(&(tv.tv_sec), &tm);
|
2019-11-29 01:52:56 -07:00
|
|
|
strftime(timestamp, 32, "%F %T", &tm);
|
2019-12-07 19:48:44 -07:00
|
|
|
fprintf(logfile ? logfile : stdout, "%s.%06ld %s %s\n", timestamp, tv.tv_usec, severities[level], buf);
|
2019-11-29 01:52:56 -07:00
|
|
|
}
|
2019-12-06 23:46:21 -07:00
|
|
|
|
|
|
|
void Log_assert_failed(const char *test, const char *file, int line)
|
|
|
|
{
|
|
|
|
Log(LERROR, "ASSERT FAILED: %s at %s:%d", test, file, line);
|
|
|
|
}
|