Coverage Report

Created: 2021-07-20 18:14

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