summaryrefslogtreecommitdiff
path: root/core/logging.c
blob: 06fd95dee03a715aeb08e511d8754160a20d0d45 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <rune/core/logging.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#define COLOR_NONE      ""
#define COLOR_DEFAULT   "\033[0m"
#define COLOR_RED       "\033[31m"
#define COLOR_GREEN     "\033[32m"
#define COLOR_YELLOW    "\033[33m"
#define COLOR_BLUE      "\033[34m"

#define LSTR_FATAL      "[FATAL]"
#define LSTR_ERROR      "[ERROR]"
#define LSTR_WARN       "[WARNING]"
#define LSTR_INFO       "[INFO]"
#define LSTR_DEBUG      "[DEBUG]"

static int debug_enabled = 0;
static int color_enabled = 1;

void log_output(int level, const char *fmt, ...) {
        char out[4096];
        memset(out, 0, sizeof(out));

        va_list arg_ptr;
        va_start(arg_ptr, fmt);
        vsnprintf(out, 4096, fmt, arg_ptr);
        va_end(arg_ptr);

        char *lvl_str;
        char *color = COLOR_NONE;
        switch (level) {
                case LOG_FATAL:
                        if (color_enabled == 1)
                                color = COLOR_RED;
                        lvl_str = LSTR_FATAL;
                        break;
                case LOG_ERROR:
                        if (color_enabled == 1)
                                color = COLOR_RED;
                        lvl_str = LSTR_ERROR;
                        break;
                case LOG_WARN:
                        if (color_enabled == 1)
                                color = COLOR_YELLOW;
                        lvl_str = LSTR_WARN;
                        break;
                case LOG_INFO:
                        lvl_str = LSTR_INFO;
                        break;
                case LOG_DEBUG:
                        if (color_enabled == 1)
                                color = COLOR_GREEN;
                        if (debug_enabled == 0)
                                return;
                        lvl_str = LSTR_DEBUG;
                        break;
        }

        if (color_enabled == 0)
                printf("%s %s\n", lvl_str, out);
        else
                printf("%s%s %s\n%s", color, lvl_str, out, COLOR_DEFAULT);
}

void enable_log_debug(void) {
        debug_enabled = 1;
}

void disable_log_debug(void) {
        debug_enabled = 0;
}

void enable_log_color(void) {
        color_enabled = 1;
}

void disable_log_color(void) {
        color_enabled = 0;
}