diff options
Diffstat (limited to 'src/log.c')
-rw-r--r-- | src/log.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..982bdb7 --- /dev/null +++ b/src/log.c | |||
@@ -0,0 +1,63 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018 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 | #include <stdarg.h> | ||
8 | #include <stdio.h> | ||
9 | #include <stdlib.h> | ||
10 | #include "fido.h" | ||
11 | |||
12 | #ifndef FIDO_NO_DIAGNOSTIC | ||
13 | |||
14 | #ifndef TLS | ||
15 | #define TLS | ||
16 | #endif | ||
17 | |||
18 | static TLS int logging; | ||
19 | |||
20 | void | ||
21 | fido_log_init(void) | ||
22 | { | ||
23 | logging = 1; | ||
24 | } | ||
25 | |||
26 | void | ||
27 | fido_log_xxd(const void *buf, size_t count) | ||
28 | { | ||
29 | const uint8_t *ptr = buf; | ||
30 | size_t i; | ||
31 | |||
32 | if (!logging) | ||
33 | return; | ||
34 | |||
35 | fprintf(stderr, " "); | ||
36 | |||
37 | for (i = 0; i < count; i++) { | ||
38 | fprintf(stderr, "%02x ", *ptr++); | ||
39 | if ((i + 1) % 16 == 0 && i + 1 < count) | ||
40 | fprintf(stderr, "\n "); | ||
41 | } | ||
42 | |||
43 | fprintf(stderr, "\n"); | ||
44 | fflush(stderr); | ||
45 | } | ||
46 | |||
47 | void | ||
48 | fido_log_debug(const char *fmt, ...) | ||
49 | { | ||
50 | va_list ap; | ||
51 | |||
52 | if (!logging) | ||
53 | return; | ||
54 | |||
55 | va_start(ap, fmt); | ||
56 | vfprintf(stderr, fmt, ap); | ||
57 | va_end(ap); | ||
58 | |||
59 | fprintf(stderr, "\n"); | ||
60 | fflush(stderr); | ||
61 | } | ||
62 | |||
63 | #endif /* !FIDO_NO_DIAGNOSTIC */ | ||