/src/util-linux/lib/debug.c
Line | Count | Source |
1 | | /* |
2 | | * No copyright is claimed. This code is in the public domain; do with |
3 | | * it what you wish. |
4 | | */ |
5 | | |
6 | | #include "debug.h" |
7 | | |
8 | | #include <stdarg.h> |
9 | | #include <stdlib.h> |
10 | | #include <stdio.h> |
11 | | #include <string.h> |
12 | | |
13 | | |
14 | | void ul_debug(const char *mesg, ...) |
15 | 0 | { |
16 | 0 | va_list ap; |
17 | 0 | va_start(ap, mesg); |
18 | 0 | vfprintf(stderr, mesg, ap); |
19 | 0 | va_end(ap); |
20 | 0 | fputc('\n', stderr); |
21 | 0 | } |
22 | | |
23 | | int ul_debug_parse_mask(const struct ul_debug_maskname flagnames[], |
24 | | const char *mask) |
25 | 0 | { |
26 | 0 | int res; |
27 | 0 | char *ptr; |
28 | | |
29 | | /* let's check for a numeric mask first */ |
30 | 0 | res = strtoul(mask, &ptr, 0); |
31 | | |
32 | | /* perhaps it's a comma-separated string? */ |
33 | 0 | if (ptr && *ptr && flagnames && flagnames[0].name) { |
34 | 0 | char *msbuf, *ms, *name; |
35 | 0 | res = 0; |
36 | |
|
37 | 0 | ms = msbuf = strdup(mask); |
38 | 0 | if (!ms) |
39 | 0 | return res; |
40 | | |
41 | 0 | while ((name = strtok_r(ms, ",", &ptr))) { |
42 | 0 | const struct ul_debug_maskname *d; |
43 | 0 | ms = ptr; |
44 | |
|
45 | 0 | for (d = flagnames; d && d->name; d++) { |
46 | 0 | if (strcmp(name, d->name) == 0) { |
47 | 0 | res |= d->mask; |
48 | 0 | break; |
49 | 0 | } |
50 | 0 | } |
51 | | /* nothing else we can do by OR-ing the mask */ |
52 | 0 | if (res == 0xffff) |
53 | 0 | break; |
54 | 0 | } |
55 | 0 | free(msbuf); |
56 | 0 | } else if (ptr && strcmp(ptr, "all") == 0) |
57 | 0 | res = 0xffff; |
58 | | |
59 | 0 | return res; |
60 | 0 | } |
61 | | |
62 | | void ul_debug_print_masks(const char *env, |
63 | | const struct ul_debug_maskname flagnames[]) |
64 | 0 | { |
65 | 0 | const struct ul_debug_maskname *d; |
66 | |
|
67 | 0 | if (!flagnames) |
68 | 0 | return; |
69 | | |
70 | 0 | fprintf(stderr, "Available \"%s=<name>[,...]|<mask>\" debug masks:\n", |
71 | 0 | env); |
72 | 0 | for (d = flagnames; d && d->name; d++) { |
73 | 0 | if (!d->help) |
74 | 0 | continue; |
75 | 0 | fprintf(stderr, " %-8s [0x%06x] : %s\n", |
76 | 0 | d->name, d->mask, d->help); |
77 | 0 | } |
78 | 0 | } |