summaryrefslogtreecommitdiff
path: root/src/log.c
blob: 748f0d50030589611af72fd3039cf171dc1af167 (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
#include "../include/al/log.h"

//#define AL_LOG_SKIP
#define AL_LOG_USE_SECTION
#ifdef AL_LOG_USE_SECTION
#define AL_LOG_TEMPLATE "%s:%d %s(): %s -> (%s) "
#else
#define AL_LOG_TEMPLATE "%s:%d %s(): %s -> "
#endif

#define AL_LOG_MESSAGE_MAX (AL_LOG_MESSAGE_SIZE - 1) // Space for newline.

#ifndef AL_FORCE_DISABLE_OUTPUT
static s32 (*_al_print)(void *, u8, char *) = NULL;
static void *_al_log_userdata = NULL;

void al_set_print(s32 (*print_func)(void *, u8, char *), void *userdata)
{
    _al_print = print_func;
    _al_log_userdata = userdata;
}

static const char *levels[] = { "info", "warn", "error", "debug" };

#ifndef AL_LOG_SKIP
static char *get_message_buffer(u8 level, const char *section, const char *fmt, const char *name, const s32 line, const char *func, va_list args)
{
    char *buffer = al_malloc(AL_LOG_MESSAGE_SIZE);
    s32 prefix = 0;
#ifdef AL_LOG_USE_SECTION
    prefix = al_snprintf(buffer, AL_LOG_MESSAGE_MAX, AL_LOG_TEMPLATE, name, line, func, levels[level], section);
#else
    (void)section;
    prefix = al_snprintf(buffer, AL_LOG_MESSAGE_MAX, AL_LOG_TEMPLATE, name, line, func, levels[level]);
#endif
    al_vsnprintf(buffer + prefix, AL_LOG_MESSAGE_MAX - prefix, fmt, args);
    buffer[strcspn(buffer, "\r\n")] = '\0';
    return buffer;
}
#endif

s32 _al_logv(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, va_list args)
{
    if (!section) section = "";
#ifdef AL_LOG_SKIP
    s32 ret = 0;
#ifdef AL_LOG_USE_SECTION
    ret += al_printf(AL_LOG_TEMPLATE, name, line, func, levels[level], section);
#else
    ret += al_printf(AL_LOG_TEMPLATE, name, line, func, levels[level]);
#endif
    ret += al_vprintf(fmt, args);
    if (strcspn(fmt, "\r\n") == al_strlen(fmt)) {
        ret += al_printf("\n");
    }
    return ret;
#else
    return _al_print(_al_log_userdata, level, get_message_buffer(level, section, fmt, name, line, func, args));
#endif
}

s32 _al_log(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    s32 ret = _al_logv(level, section, name, line, func, fmt, args);
    va_end(args);
    return ret;
}
#else
void al_set_print(s32 (*print_func)(void *, u8, char *), void *userdata) { (void)print_func; (void)userdata; }
s32 _al_logv(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, va_list args) { (void)level; (void)section; (void)name; (void)line; (void)func; (void)fmt; (void)args; return 0; }
s32 _al_log(u8 level, const char *section, const char *name, const s32 line, const char *func, const char *fmt, ...) { (void)level; (void)section; (void)name; (void)line; (void)func; (void)fmt; return 0; }
#endif