Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2018-2021 Yubico AB. All rights reserved. |
3 | | * Use of this source code is governed by a BSD-style |
4 | | * license that can be found in the LICENSE file. |
5 | | */ |
6 | | |
7 | | #undef _GNU_SOURCE /* XSI strerror_r() */ |
8 | | |
9 | | #include <stdarg.h> |
10 | | #include <stdio.h> |
11 | | |
12 | | #include "fido.h" |
13 | | |
14 | | #ifndef FIDO_NO_DIAGNOSTIC |
15 | | |
16 | | #define XXDLEN 32 |
17 | | #define XXDROW 128 |
18 | | #define LINELEN 256 |
19 | | |
20 | | #ifndef TLS |
21 | | #define TLS |
22 | | #endif |
23 | | |
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 | 0 | { |
30 | 0 | fprintf(stderr, "%s", str); |
31 | 0 | } |
32 | | |
33 | | static void |
34 | | do_log(const char *suffix, const char *fmt, va_list args) |
35 | 8.88M | { |
36 | 8.88M | char line[LINELEN], body[LINELEN]; |
37 | | |
38 | 8.88M | vsnprintf(body, sizeof(body), fmt, args); |
39 | | |
40 | 8.88M | if (suffix != NULL) |
41 | 1.57M | snprintf(line, sizeof(line), "%.180s: %.70s\n", body, suffix); |
42 | 7.30M | else |
43 | 7.30M | snprintf(line, sizeof(line), "%.180s\n", body); |
44 | | |
45 | 8.88M | log_handler(line); |
46 | 8.88M | } |
47 | | |
48 | | void |
49 | | fido_log_init(void) |
50 | 11.3k | { |
51 | 11.3k | logging = 1; |
52 | 11.3k | log_handler = log_on_stderr; |
53 | 11.3k | } |
54 | | |
55 | | void |
56 | | fido_log_debug(const char *fmt, ...) |
57 | 7.30M | { |
58 | 7.30M | va_list args; |
59 | | |
60 | 7.30M | if (!logging || log_handler == NULL) |
61 | 0 | return; |
62 | | |
63 | 7.30M | va_start(args, fmt); |
64 | 7.30M | do_log(NULL, fmt, args); |
65 | 7.30M | va_end(args); |
66 | 7.30M | } |
67 | | |
68 | | void |
69 | | fido_log_xxd(const void *buf, size_t count, const char *fmt, ...) |
70 | 1.12M | { |
71 | 1.12M | const uint8_t *ptr = buf; |
72 | 1.12M | char row[XXDROW], xxd[XXDLEN]; |
73 | 1.12M | va_list args; |
74 | | |
75 | 1.12M | if (!logging || log_handler == NULL) |
76 | 0 | return; |
77 | | |
78 | 1.12M | snprintf(row, sizeof(row), "buf=%p, len=%zu", buf, count); |
79 | 1.12M | va_start(args, fmt); |
80 | 1.12M | do_log(row, fmt, args); |
81 | 1.12M | va_end(args); |
82 | 1.12M | *row = '\0'; |
83 | | |
84 | 35.6M | for (size_t i = 0; i < count; i++) { |
85 | 34.4M | *xxd = '\0'; |
86 | 34.4M | if (i % 16 == 0) |
87 | 2.59M | snprintf(xxd, sizeof(xxd), "%04zu: %02x", i, *ptr++); |
88 | 31.8M | else |
89 | 31.8M | snprintf(xxd, sizeof(xxd), " %02x", *ptr++); |
90 | 34.4M | strlcat(row, xxd, sizeof(row)); |
91 | 34.4M | if (i % 16 == 15 || i == count - 1) { |
92 | 2.59M | fido_log_debug("%s", row); |
93 | 2.59M | *row = '\0'; |
94 | 2.59M | } |
95 | 34.4M | } |
96 | 1.12M | } |
97 | | |
98 | | void |
99 | | fido_log_error(int errnum, const char *fmt, ...) |
100 | 454k | { |
101 | 454k | char errstr[LINELEN]; |
102 | 454k | va_list args; |
103 | | |
104 | 454k | if (!logging || log_handler == NULL) |
105 | 0 | return; |
106 | 454k | if (strerror_r(errnum, errstr, sizeof(errstr)) != 0) |
107 | 0 | snprintf(errstr, sizeof(errstr), "error %d", errnum); |
108 | | |
109 | 454k | va_start(args, fmt); |
110 | 454k | do_log(errstr, fmt, args); |
111 | 454k | va_end(args); |
112 | 454k | } |
113 | | |
114 | | void |
115 | | fido_set_log_handler(fido_log_handler_t *handler) |
116 | 11.3k | { |
117 | 11.3k | if (handler != NULL) |
118 | 11.3k | log_handler = handler; |
119 | 11.3k | } |
120 | | |
121 | | #endif /* !FIDO_NO_DIAGNOSTIC */ |