Coverage Report

Created: 2021-03-26 11:35

/libfido2/src/log.c
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
2.02M
{
36
2.02M
        char line[LINELEN], body[LINELEN - 3];
37
2.02M
38
2.02M
        vsnprintf(body, sizeof(body), fmt, args);
39
2.02M
40
2.02M
        if (suffix != NULL)
41
2.02M
                snprintf(line, sizeof(line), "%s: %s\n", body, suffix);
42
1.78M
        else
43
1.78M
                snprintf(line, sizeof(line), "%s\n", body);
44
2.02M
45
2.02M
        log_handler(line);
46
2.02M
}
47
48
void
49
fido_log_init(void)
50
11.5k
{
51
11.5k
        logging = 1;
52
11.5k
        log_handler = log_on_stderr;
53
11.5k
}
54
55
void
56
fido_log_debug(const char *fmt, ...)
57
1.78M
{
58
1.78M
        va_list args;
59
1.78M
60
1.78M
        if (!logging || log_handler == NULL)
61
1.78M
                return;
62
1.78M
63
1.78M
        va_start(args, fmt);
64
1.78M
        do_log(NULL, fmt, args);
65
1.78M
        va_end(args);
66
1.78M
}
67
68
void
69
fido_log_xxd(const void *buf, size_t count, const char *fmt, ...)
70
239k
{
71
239k
        const uint8_t *ptr = buf;
72
239k
        char row[XXDROW], xxd[XXDLEN];
73
239k
        va_list args;
74
239k
75
239k
        if (!logging || log_handler == NULL)
76
239k
                return;
77
239k
78
239k
        snprintf(row, sizeof(row), "buf=%p, len=%zu", buf, count);
79
239k
        va_start(args, fmt);
80
239k
        do_log(row, fmt, args);
81
239k
        va_end(args);
82
239k
        *row = '\0';
83
239k
84
20.1M
        for (size_t i = 0; i < count; i++) {
85
19.9M
                *xxd = '\0';
86
19.9M
                if (i % 16 == 0)
87
1.33M
                        snprintf(xxd, sizeof(xxd), "%04zu: %02x", i, *ptr++);
88
18.5M
                else
89
18.5M
                        snprintf(xxd, sizeof(xxd), " %02x", *ptr++);
90
19.9M
                strlcat(row, xxd, sizeof(row));
91
19.9M
                if (i % 16 == 15 || i == count - 1) {
92
1.33M
                        fido_log_debug("%s", row);
93
1.33M
                        *row = '\0';
94
1.33M
                }
95
19.9M
        }
96
239k
}
97
98
void
99
fido_log_error(int errnum, const char *fmt, ...)
100
66
{
101
66
        char errstr[LINELEN];
102
66
        va_list args;
103
66
104
66
        if (!logging || log_handler == NULL)
105
66
                return;
106
66
        if (strerror_r(errnum, errstr, sizeof(errstr)) != 0)
107
0
                snprintf(errstr, sizeof(errstr), "error %d", errnum);
108
66
109
66
        va_start(args, fmt);
110
66
        do_log(errstr, fmt, args);
111
66
        va_end(args);
112
66
}
113
114
void
115
fido_set_log_handler(fido_log_handler_t *handler)
116
11.5k
{
117
11.5k
        if (handler != NULL)
118
11.5k
                log_handler = handler;
119
11.5k
}
120
121
#endif /* !FIDO_NO_DIAGNOSTIC */