aboutsummaryrefslogtreecommitdiff
path: root/src/std/log.c
blob: 68f1f2ec27fc7beba60db81c6db06a7b778fbf79 (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
#include "../../include/std/log.h"
#include "../../include/std/thread.h"
#include "../../include/std/misc.h"
#include <stdio.h>
#include <stdarg.h>

#define LOG_DEBUG

static amal_mutex mutex;
static bool mutex_initialized = bool_false;

/* Safe to call multiple times */
static void mutex_init(void) {
    if(!mutex_initialized) {
        ignore_result_int(amal_mutex_init(&mutex));
        mutex_initialized = bool_true;
    }
}

amal_mutex* amal_log_get_mutex(void) {
    mutex_init();
    return &mutex;
}

void amal_log_debug(const char *fmt, ...) {
#ifdef LOG_DEBUG
    va_list args;
    mutex_init();
    ignore_result_int(amal_mutex_lock(&mutex, NULL));
    fprintf(stderr, "Debug: ");
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
    fprintf(stderr, "\n");
    ignore_result_int(amal_mutex_unlock(&mutex));
#else
    (void)fmt;
#endif
}

void amal_log_error(const char *fmt, ...) {
    va_list args;
    mutex_init();
    ignore_result_int(amal_mutex_lock(&mutex, NULL));
    fprintf(stderr, "\x1b[1;31mError:\x1b[0m ");
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
    fprintf(stderr, "\n");
    ignore_result_int(amal_mutex_unlock(&mutex));
}

void amal_log_info(const char *fmt, ...) {
    va_list args;
    mutex_init();
    ignore_result_int(amal_mutex_lock(&mutex, NULL));
    fprintf(stderr, "Info: ");
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
    fprintf(stderr, "\n");
    ignore_result_int(amal_mutex_unlock(&mutex));
}

void amal_log_warning(const char *fmt, ...) {
    va_list args;
    mutex_init();
    ignore_result_int(amal_mutex_lock(&mutex, NULL));
    fprintf(stderr, "Warning: ");
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
    fprintf(stderr, "\n");
    ignore_result_int(amal_mutex_unlock(&mutex));
}