diff options
Diffstat (limited to 'src/log.c')
-rw-r--r-- | src/log.c | 73 |
1 files changed, 51 insertions, 22 deletions
@@ -7,57 +7,86 @@ | |||
7 | #include <stdarg.h> | 7 | #include <stdarg.h> |
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
10 | #include <string.h> | ||
11 | |||
10 | #include "fido.h" | 12 | #include "fido.h" |
11 | 13 | ||
12 | #ifndef FIDO_NO_DIAGNOSTIC | 14 | #ifndef FIDO_NO_DIAGNOSTIC |
13 | 15 | ||
16 | #define XXDLEN 32 | ||
17 | #define XXDROW 128 | ||
18 | #define LINELEN 256 | ||
19 | |||
14 | #ifndef TLS | 20 | #ifndef TLS |
15 | #define TLS | 21 | #define TLS |
16 | #endif | 22 | #endif |
17 | 23 | ||
18 | static TLS int logging; | 24 | static TLS int logging; |
25 | static TLS fido_log_handler_t *log_handler; | ||
26 | |||
27 | static void | ||
28 | log_on_stderr(const char *str) | ||
29 | { | ||
30 | fprintf(stderr, "%s", str); | ||
31 | } | ||
19 | 32 | ||
20 | void | 33 | void |
21 | fido_log_init(void) | 34 | fido_log_init(void) |
22 | { | 35 | { |
23 | logging = 1; | 36 | logging = 1; |
37 | log_handler = log_on_stderr; | ||
24 | } | 38 | } |
25 | 39 | ||
26 | void | 40 | void |
27 | fido_log_xxd(const void *buf, size_t count) | 41 | fido_log_debug(const char *fmt, ...) |
28 | { | 42 | { |
29 | const uint8_t *ptr = buf; | 43 | char line[LINELEN]; |
30 | size_t i; | 44 | va_list ap; |
45 | int r; | ||
31 | 46 | ||
32 | if (!logging) | 47 | if (!logging || log_handler == NULL) |
33 | return; | 48 | return; |
34 | 49 | ||
35 | fprintf(stderr, " "); | 50 | va_start(ap, fmt); |
36 | 51 | r = vsnprintf(line, sizeof(line) - 1, fmt, ap); | |
37 | for (i = 0; i < count; i++) { | 52 | va_end(ap); |
38 | fprintf(stderr, "%02x ", *ptr++); | 53 | if (r < 0 || (size_t)r >= sizeof(line) - 1) |
39 | if ((i + 1) % 16 == 0 && i + 1 < count) | 54 | return; |
40 | fprintf(stderr, "\n "); | 55 | strlcat(line, "\n", sizeof(line)); |
41 | } | 56 | log_handler(line); |
42 | |||
43 | fprintf(stderr, "\n"); | ||
44 | fflush(stderr); | ||
45 | } | 57 | } |
46 | 58 | ||
47 | void | 59 | void |
48 | fido_log_debug(const char *fmt, ...) | 60 | fido_log_xxd(const void *buf, size_t count) |
49 | { | 61 | { |
50 | va_list ap; | 62 | const uint8_t *ptr = buf; |
63 | char row[XXDROW]; | ||
64 | char xxd[XXDLEN]; | ||
51 | 65 | ||
52 | if (!logging) | 66 | if (!logging || log_handler == NULL || count == 0) |
53 | return; | 67 | return; |
54 | 68 | ||
55 | va_start(ap, fmt); | 69 | *row = '\0'; |
56 | vfprintf(stderr, fmt, ap); | ||
57 | va_end(ap); | ||
58 | 70 | ||
59 | fprintf(stderr, "\n"); | 71 | for (size_t i = 0; i < count; i++) { |
60 | fflush(stderr); | 72 | *xxd = '\0'; |
73 | if (i % 16 == 0) | ||
74 | snprintf(xxd, sizeof(xxd), "%04zu: %02x", i, *ptr++); | ||
75 | else | ||
76 | snprintf(xxd, sizeof(xxd), " %02x", *ptr++); | ||
77 | strlcat(row, xxd, sizeof(row)); | ||
78 | if (i % 16 == 15 || i == count - 1) { | ||
79 | fido_log_debug("%s", row); | ||
80 | *row = '\0'; | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | |||
85 | void | ||
86 | fido_set_log_handler(fido_log_handler_t *handler) | ||
87 | { | ||
88 | if (handler != NULL) | ||
89 | log_handler = handler; | ||
61 | } | 90 | } |
62 | 91 | ||
63 | #endif /* !FIDO_NO_DIAGNOSTIC */ | 92 | #endif /* !FIDO_NO_DIAGNOSTIC */ |