summaryrefslogtreecommitdiff
path: root/base/base_error.c
blob: fa1c9878f7c4e21dc25d96c571834007948f107b (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
#include "fitz-base.h"

fz_error *
fz_keeperror(fz_error *eo)
{
    eo->refs++;
    return eo;
}

void
fz_droperror(fz_error *eo)
{
    if (eo->refs > 0)
        eo->refs--;
    if (eo->refs == 0)
    {
        if (eo->cause)
            fz_droperror(eo->cause);
        fz_free(eo);
    }
}

void
fz_printerror(fz_error *eo)
{
#if 1
    if (eo->cause)
    {
        fz_printerror(eo->cause);
        fprintf(stderr, "| %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg);
    }
    else
    {
        fprintf(stderr, "+ %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg);
    }
#else
    fprintf(stderr, "+ %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg);
    eo = eo->cause;

    while (eo)
    {
        fprintf(stderr, "| %s:%d: %s(): %s\n", eo->file, eo->line, eo->func, eo->msg);
        eo = eo->cause;
    }
#endif
}


void
fz_warn(char *fmt, ...)
{
    va_list ap;
    fprintf(stderr, "warning: ");
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, "\n");
}

fz_error *
fz_throwimp(fz_error *cause, const char *func, const char *file, int line, char *fmt, ...)
{
    va_list ap;
    fz_error *eo;

    eo = fz_malloc(sizeof(fz_error));
    if (!eo)
        return fz_outofmem; /* oops. we're *really* out of memory here. */

    eo->refs = 1;

    va_start(ap, fmt);
    vsnprintf(eo->msg, sizeof eo->msg, fmt, ap);
    eo->msg[sizeof(eo->msg) - 1] = '\0';
    va_end(ap);

    strlcpy(eo->func, func, sizeof eo->func);
    strlcpy(eo->file, file, sizeof eo->file);
    eo->line = line;

    if (cause)
        eo->cause = fz_keeperror(cause);
    else
        eo->cause = nil;

    return eo;
}