aboutsummaryrefslogtreecommitdiff
path: root/client/acronc/log.c
blob: feda8c298966050bb64cbde94735d211222406bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Created by yuuta on 1/1/22.
 */

#define _GNU_SOURCE

#include "log.h"
#include "config.h"

#include <stdio.h>
#include <stdarg.h>
#include <assert.h>

#if defined(__linux__)
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#elif defined(_WIN32)
#include <windows.h>
#endif

void g_log(enum log_level level,
                  const char *file,
                  int line,
                  const char *format,
                  ...) {
    FILE *stream = stderr;
    switch (level) {
        case log_fetal:
            fprintf(stream, "F");
            break;
        case log_error:
            fprintf(stream, "E");
            break;
        case log_warn:
            fprintf(stream, "W");
            break;
        case log_info:
            fprintf(stream, "I");
            break;
        case log_debug:
#ifdef DEBUG
            fprintf(stream, "D");
            break;
#else
            return;
#endif
        default:
            fprintf(stderr, "Unknown log level: %d.\n", level);
            assert(0);
    }
    int tid = -1;
#if defined(__linux__)
    tid = (int) syscall(__NR_gettid);
#elif defined(_WIN32)
    tid = (int) GetCurrentThreadId();
#endif
    fprintf(stream, "[%d %s:%d]: ",
            tid, file, line);
    va_list list;
    va_start(list, format);
    vfprintf(stream, format, list);
    va_end(list);
    fprintf(stream, "\n");
}