Coverage Report

Created: 2024-02-05 19:20

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