diff options
author | tanyaionova <isaqtm@gmail.com> | 2019-11-20 23:29:05 +0300 |
---|---|---|
committer | tanyaionova <isaqtm@gmail.com> | 2019-11-20 23:29:05 +0300 |
commit | ac7fd63348553c2a0ad1b776c7039b1a8b60dbe8 (patch) | |
tree | 772b481416230eedfdfb55a03ef1c3e61ec23825 /log.c | |
parent | 48debdf040d0e0b1af8d11a339ce8332e2454d3f (diff) | |
download | ash-sbox-ac7fd63348553c2a0ad1b776c7039b1a8b60dbe8.tar.gz |
Bump
Diffstat (limited to 'log.c')
-rw-r--r-- | log.c | 53 |
1 files changed, 22 insertions, 31 deletions
@@ -24,14 +24,14 @@ #include <stdlib.h> #include <stdarg.h> #include <string.h> + +#include <sys/time.h> #include <time.h> #include "log.h" static struct { - void *udata; FILE *fp; - int level; int quiet; } L; @@ -41,7 +41,7 @@ static const char *level_names[] = { }; static const char *level_colors[] = { - "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m" + "\x1b[35m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[91m" }; @@ -50,39 +50,30 @@ void log_set_fp(FILE *fp) { } -void log_set_level(int level) { - L.level = level; -} - - void log_set_quiet(int enable) { L.quiet = enable ? 1 : 0; } void log_log(int level, const char *file, int line, const char *fmt, ...) { - if (level < L.level) { + if (L.quiet) return; - } - - /* Get current time */ - time_t t = time(NULL); - struct tm *lt = localtime(&t); - - /* Log to stderr */ - if (!L.quiet) { - va_list args; - char buf[16]; - buf[strftime(buf, sizeof(buf), "%H:%M:%S", lt)] = '\0'; - - fprintf( - stderr, "%s %s%-5s\x1b[0m \x1b[96m%s:%d:\x1b[0m ", - buf, level_colors[level], level_names[level], file, line); - - va_start(args, fmt); - vfprintf(stderr, fmt, args); - va_end(args); - fprintf(stderr, "\n"); - fflush(stderr); - } + + struct timeval tv; + gettimeofday(&tv, NULL); + struct tm *lt = localtime(&tv.tv_sec); + + va_list args; + char timebuf[16]; + timebuf[strftime(timebuf, sizeof(timebuf), "%H:%M:%S", lt)] = '\0'; + + fprintf(stderr, + "%s.%04ld %s%-5s\x1b[0m \x1b[96m%s:%d:\x1b[0m ", + timebuf, tv.tv_usec / 100, level_colors[level], level_names[level], file, line); + + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fprintf(stderr, "\n"); + fflush(stderr); }
\ No newline at end of file |