aboutsummaryrefslogtreecommitdiff
path: root/log.c
blob: bdf7198baf4926cc386c9b4ca4b1e16d7454224b (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
/*
 * Created by yuuta on 1/1/22.
 */

#include "common.h"
#include "log.h"

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

void g_log(enum log_level level,
                  const char *file,
                  int line,
                  const char *format,
                  ...) {
    int stream = level <= log_warn || level == log_debug ? p_env.err : p_env.out;
    switch (level) {
        case log_fetal:
            dprintf(stream, "F");
            break;
        case log_error:
            dprintf(stream, "E");
            break;
        case log_warn:
            dprintf(stream, "W");
            break;
        case log_info:
            dprintf(stream, "I");
            break;
        case log_debug:
            dprintf(stream, "D");
            break;
        default:
            dprintf(p_env.err, "Unknown log level: %d.\n", level);
            assert(0);
    }
    dprintf(stream, "[%s:%d]: ",
            file, line);
    va_list list;
    va_start(list, format);
    vdprintf(stream, format, list);
    va_end(list);
    dprintf(stream, "\n");
}