aboutsummaryrefslogtreecommitdiff
path: root/lib/libXDAAP/debug.c
blob: 6e5dd4f3ae5e96403ed8638d942313ac928b5a5c (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
176
177
178
179
180
181
182
183
/* Copyright (c) David Hammerton 2003
 *
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <memory.h>

#include "debug.h"

#define DEFAULT_DEBUG_CHANNEL "debug"

static int tracesEnabled = 0;
static int errEnabled = 1;
static int fixmeEnabled = 1;

/* returns -1 on failure, 0 on success */
int daap_debug_init(const char *const debug_init_string)
{
    const char *p = debug_init_string;

    while (*p)
    {
        char *class_or_channel;
        int class_or_channel_size;
        const char *end;
        int the_switch;

        switch (*p)
        {
        case '+': the_switch = 1;
                  break;
        case '-': the_switch = 0;
                  break;
        default:
                  goto err;
        }

        p++;
        if (!p) goto err;
        end = strchr(p, ',');
        if (end) end--;
        else end = p + strlen(p);

        class_or_channel_size = end - p + 1;

        class_or_channel = (char*)malloc(class_or_channel_size + 1);
        strncpy(class_or_channel, p, class_or_channel_size);
        class_or_channel[class_or_channel_size] = 0;

        /* now check if it is a class */
        if (strcmp(class_or_channel, "err") == 0)
            errEnabled = the_switch;
        else if (strcmp(class_or_channel, "fixme") == 0)
            fixmeEnabled = the_switch;
        else if (strcmp(class_or_channel, "trace") == 0)
            tracesEnabled = the_switch;
        else /* check channels */
        {
            FIXME("sorry, but currently you can only toggle debug classes. Not switching '%s'.\n",
                  class_or_channel);
        }

        p = end + 1;
        if (*p == ',') p++;
    };
    return 0;
err:
    return -1;
}

static char *get_debug_class_str(enum __DEBUG_CLASS debug_class)
{
    switch (debug_class)
    {
        case __DEBUG_TRACE: return "trace";
        case __DEBUG_ERR: return "err";
        case __DEBUG_FIXME: return "fixme";
        default: return "";
    }
}

/* FIXME: don't care about channel for now */
int debug_get_debugging(enum __DEBUG_CLASS debug_class, const char *debug_channel)
{
    switch (debug_class)
    {
        case __DEBUG_TRACE: return tracesEnabled;
        case __DEBUG_ERR: return errEnabled;
        case __DEBUG_FIXME: return fixmeEnabled;
        default: return 0;
    }
}

int debug_log(enum __DEBUG_CLASS debug_class, const char *module,
              const char *function,
              const int line, const char *format, ...)
{
    va_list valist;
    int ret = 0;
    char *debug_class_str = get_debug_class_str(debug_class);

    va_start(valist, format);

    ret += fprintf(stderr, "%s:%s:", debug_class_str, module);
    ret += fprintf(stderr, "%s:%i ", function, line);

    ret += vfprintf(stderr, format, valist);

    va_end(valist);

    return ret;
}

int debug_printf(const char *format, ...)
{
    va_list valist;
    int ret = 0;

    va_start(valist, format);

    ret += vfprintf(stderr, format, valist);

    va_end(valist);

    return ret;
}

void debug_hexdump(void *data, unsigned long len)
{
    unsigned int i;
    char *cdata = (char *)data;
    char textout[16];

    for (i = 0; i < len; i++)
    {
        if ((i % 8) == 0 && i) fprintf(stderr, "  ");
        if ((i % 16) == 0 && i) fprintf(stderr, "     '%.8s' '%.8s'\n", &textout[0], &textout[8]);
        textout[i % 16] = (cdata[i] && isprint(cdata[i])) ? cdata[i] : '.';
        fprintf(stderr, "%02hhx ", cdata[i]);
    }
    if (i % 16)
    {
        unsigned int j;
        for (j = 0; j < (16 - (i % 16)); j++)
        {
            if (((16 - (i % 16)) - j) == 8) fprintf(stderr, "  ");
            fprintf(stderr, ".. ");
        }
        fprintf(stderr, "       '");
        for (j = 0; j < (i % 16); j++)
        {
            fprintf(stderr, "%c", textout[j]);
            if (j == 8) fprintf(stderr, "' '");
        }
        fprintf(stderr, "'\n");
    }
    fprintf(stderr, "\n");
}