Coverage Report

Created: 2025-03-01 02:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/libfido2/src/log.c
Line
Count
Source
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
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#undef _GNU_SOURCE /* XSI strerror_r() */
9
10
#include <stdarg.h>
11
#include <stdio.h>
12
13
#include "fido.h"
14
15
#ifndef FIDO_NO_DIAGNOSTIC
16
17
#define XXDLEN  32
18
#define XXDROW  128
19
#define LINELEN 256
20
21
#ifndef TLS
22
#define TLS
23
#endif
24
25
static TLS int logging;
26
static TLS fido_log_handler_t *log_handler;
27
28
static void
29
log_on_stderr(const char *str)
30
0
{
31
0
        fprintf(stderr, "%s", str);
32
0
}
33
34
static void
35
do_log(const char *suffix, const char *fmt, va_list args)
36
63.3M
{
37
63.3M
        char line[LINELEN], body[LINELEN];
38
39
63.3M
        vsnprintf(body, sizeof(body), fmt, args);
40
41
63.3M
        if (suffix != NULL)
42
11.0M
                snprintf(line, sizeof(line), "%.180s: %.70s\n", body, suffix);
43
52.3M
        else
44
52.3M
                snprintf(line, sizeof(line), "%.180s\n", body);
45
46
63.3M
        log_handler(line);
47
63.3M
}
48
49
void
50
fido_log_init(void)
51
170k
{
52
170k
        logging = 1;
53
170k
        log_handler = log_on_stderr;
54
170k
}
55
56
void
57
fido_log_debug(const char *fmt, ...)
58
52.3M
{
59
52.3M
        va_list args;
60
61
52.3M
        if (!logging || log_handler == NULL)
62
0
                return;
63
64
52.3M
        va_start(args, fmt);
65
52.3M
        do_log(NULL, fmt, args);
66
52.3M
        va_end(args);
67
52.3M
}
68
69
void
70
fido_log_xxd(const void *buf, size_t count, const char *fmt, ...)
71
8.53M
{
72
8.53M
        const uint8_t *ptr = buf;
73
8.53M
        char row[XXDROW], xxd[XXDLEN];
74
8.53M
        va_list args;
75
76
8.53M
        if (!logging || log_handler == NULL)
77
0
                return;
78
79
8.53M
        snprintf(row, sizeof(row), "buf=%p, len=%zu", buf, count);
80
8.53M
        va_start(args, fmt);
81
8.53M
        do_log(row, fmt, args);
82
8.53M
        va_end(args);
83
8.53M
        *row = '\0';
84
85
297M
        for (size_t i = 0; i < count; i++) {
86
288M
                *xxd = '\0';
87
288M
                if (i % 16 == 0)
88
21.4M
                        snprintf(xxd, sizeof(xxd), "%04zu: %02x", i, *ptr++);
89
267M
                else
90
267M
                        snprintf(xxd, sizeof(xxd), " %02x", *ptr++);
91
288M
                strlcat(row, xxd, sizeof(row));
92
288M
                if (i % 16 == 15 || i == count - 1) {
93
21.4M
                        fido_log_debug("%s", row);
94
21.4M
                        *row = '\0';
95
21.4M
                }
96
288M
        }
97
8.53M
}
98
99
void
100
fido_log_error(int errnum, const char *fmt, ...)
101
2.51M
{
102
2.51M
        char errstr[LINELEN];
103
2.51M
        va_list args;
104
105
2.51M
        if (!logging || log_handler == NULL)
106
0
                return;
107
2.51M
        if (strerror_r(errnum, errstr, sizeof(errstr)) != 0)
108
0
                snprintf(errstr, sizeof(errstr), "error %d", errnum);
109
110
2.51M
        va_start(args, fmt);
111
2.51M
        do_log(errstr, fmt, args);
112
2.51M
        va_end(args);
113
2.51M
}
114
115
void
116
fido_set_log_handler(fido_log_handler_t *handler)
117
170k
{
118
170k
        if (handler != NULL)
119
170k
                log_handler = handler;
120
170k
}
121
122
#endif /* !FIDO_NO_DIAGNOSTIC */