summaryrefslogtreecommitdiff
path: root/src/dbg.c
blob: 13c3f18d3f7c929e8bc03f029eee259e2fd078d8 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <tinyff/stream.h>
#include <tinyff/dbg.h>
#include <stdarg.h>
#include <stdint.h>

#ifdef USE_THREAD
#include <pthread.h>
#endif

static size_t ff_internal_itoa(int value, char* buf)
{
    char temp[12];
    size_t i = 0, j = 0;

    if (value == 0) {
        buf[0] = '0';
        return 1;
    }

    unsigned int uvalue;

    if (value < 0) {
        buf[j++] = '-';
        uvalue = (unsigned int)(-(long)value);
    } else {
        uvalue = (unsigned int)value;
    }

    while (uvalue > 0) {
        temp[i++] = (uvalue % 10) + '0';
        uvalue /= 10;
    }

    while (i > 0) {
        buf[j++] = temp[--i];
    }

    return j;
}

ff_result ff_set_debug_stream(ff_stream* stream, ff_ctx* ctx)
{
    if (!ctx) return FF_RESULT_ERROR_NULL_PTR;

    if (stream) {
        ctx->ff_debug_stream = *stream;
        ctx->ff_debug_enabled = true;
    } else {
        ctx->ff_debug_enabled = false;
    }

    return FF_RESULT_OK;
}

static inline void ff_debug_flush(ff_ctx* ctx, char* buffer, size_t* idx)
{
    if (*idx == 0) return;

#ifdef USE_THREAD
    pthread_mutex_lock(&ctx->ff_lock);
#endif
    ctx->ff_debug_stream.write(
        buffer,
        *idx,
        ctx->ff_debug_stream.user
    );

    *idx = 0;
#ifdef USE_THREAD
    pthread_mutex_unlock(&ctx->ff_lock);
#endif
}

ff_result ff_dprintf(ff_ctx* ctx, const char *format, ...)
{
    if (!ctx || !ctx->ff_debug_enabled || !ctx->ff_debug_stream.write) {
        return FF_RESULT_WARN_DEBUG_DISABLED;
    }

    va_list args;
    va_start(args, format);

    char buffer[512];
    size_t buf_idx = 0;
    const char* p = format;

#ifdef USE_THREAD
    pthread_mutex_lock(&ctx->ff_lock);
#endif

    while (*p) {

        if (buf_idx >= sizeof(buffer) - 64) {
            ff_debug_flush(ctx, buffer, &buf_idx);
        }

        if (*p == '%') {
            p++;

            if (!*p) break;

            if (*p == 's') {
                const char* s = va_arg(args, const char*);
                if (!s) s = "(null)";

                while (*s) {
                    buffer[buf_idx++] = *s++;

                    if (buf_idx >= sizeof(buffer)) {
                        ff_debug_flush(ctx, buffer, &buf_idx);
                    }
                }
            }
            else if (*p == 'd') {
                int d = va_arg(args, int);
                buf_idx += ff_internal_itoa(d, &buffer[buf_idx]);
            }
            else if (*p == 'u') {
                unsigned int u = va_arg(args, unsigned int);
                buf_idx += ff_internal_itoa((int)u, &buffer[buf_idx]);
            }
            else if (*p == 'l' && *(p + 1) == 'd') {
                long d = va_arg(args, long);
                buf_idx += ff_internal_itoa((int)d, &buffer[buf_idx]);
                p++;
            }
            else if (*p == 'f') {
                double f = va_arg(args, double);

                if (f < 0) {
                    buffer[buf_idx++] = '-';
                    f = -f;
                }

                int whole = (int)f;
                double frac = f - whole;

                buf_idx += ff_internal_itoa(whole, &buffer[buf_idx]);

                buffer[buf_idx++] = '.';

                for (int i = 0; i < 3; i++) {
                    frac *= 10.0;
                    int digit = (int)frac;
                    buffer[buf_idx++] = '0' + digit;
                    frac -= digit;
                }
            }
            else if (*p == 'c') {
                buffer[buf_idx++] = (char)va_arg(args, int);
            }
            else if (*p == '%') {
                buffer[buf_idx++] = '%';
            }
            else {
                buffer[buf_idx++] = '%';
                buffer[buf_idx++] = *p;
            }
        }
        else {
            buffer[buf_idx++] = *p;
        }

        p++;
    }

    ff_debug_flush(ctx, buffer, &buf_idx);

#ifdef USE_THREAD
    pthread_mutex_unlock(&ctx->ff_lock);
#endif

    va_end(args);
    return FF_RESULT_OK;
}