Coverage Report

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