summaryrefslogtreecommitdiff
path: root/lib/error.c
blob: 3d882347f38a42c6283d409c5a73582104dc5c33 (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
#include <merror.h>
#include <stdarg.h>
#include <stdio.h>

char *current_file = NULL;
int  log_disabled = 0;

__attribute__((format(printf, 4, 5)))
void __log_impl_pos(int line, int column, int type, const char *format, ...)
{
        if (log_disabled)
                return;

        va_list list;
        va_start(list, format);

        char *t = NULL;
        switch (type) {
        case __DEBUG:
                t = "\033[34mdebug:\033[0m";
                break;
        case __WARNING:
                t = "\033[35mwarning:\033[0m";
                break;
        case __ERROR:
                t = "\033[31merror:\033[0m";
                break;
        }

        if (current_file != NULL)
                printf("%s:%d:%d: %s ", current_file, line, column, t);
        else
                printf("%s ", t);
        vprintf(format, list);
        putchar('\n');
}

__attribute__((format(printf, 2, 3)))
void __log_impl(int type, const char *format, ...)
{
        if (log_disabled)
                return;

        va_list list;
        va_start(list, format);

        char *t = NULL;
        switch (type) {
        case __DEBUG:
                t = "\033[34mdebug:\033[0m";
                break;
        case __WARNING:
                t = "\033[35mwarning:\033[0m";
                break;
        case __ERROR:
                t = "\033[31merror:\033[0m";
                break;
        }

        if (current_file != NULL)
                printf("%s: %s ", current_file, t);
        else
                printf("%s ", t);
        vprintf(format, list);
        putchar('\n');
}