Coverage Report

Created: 2025-07-23 06:52

/src/casync/src/log.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: LGPL-2.1+ */
2
3
#include <stdarg.h>
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <syslog.h>
7
8
#include "log.h"
9
#include "util.h"
10
11
static int cached_log_level = -1;
12
13
0
static int level_from_string(const char *str) {
14
0
        if (STR_IN_SET(str, "emerg", "emergency", "0"))
15
0
                return LOG_EMERG;
16
0
        else if (STR_IN_SET(str, "alert", "1"))
17
0
                return LOG_ALERT;
18
0
        else if (STR_IN_SET(str, "crit", "critical", "2"))
19
0
                return LOG_CRIT;
20
0
        else if (STR_IN_SET(str, "err", "error", "3"))
21
0
                return LOG_ERR;
22
0
        else if (STR_IN_SET(str, "warn", "warning", "4"))
23
0
                return LOG_WARNING;
24
0
        else if (STR_IN_SET(str, "notice", "5"))
25
0
                return LOG_NOTICE;
26
0
        else if (STR_IN_SET(str, "info", "6"))
27
0
                return LOG_INFO;
28
0
        else if (STR_IN_SET(str, "debug", "7"))
29
0
                return LOG_DEBUG;
30
0
        else
31
0
                return -EINVAL;
32
0
}
33
34
323
static int get_log_level(void) {
35
323
        if (cached_log_level < 0) {
36
0
                const char *e;
37
38
0
                cached_log_level = LOG_INFO;
39
40
0
                e = getenv("CASYNC_LOG_LEVEL");
41
0
                if (e)
42
0
                        set_log_level_from_string(e);
43
0
        }
44
45
323
        return cached_log_level;
46
323
}
47
48
213
void set_log_level(int level) {
49
213
        cached_log_level = level;
50
213
}
51
52
0
int set_log_level_from_string(const char *str) {
53
0
        int level;
54
55
0
        level = level_from_string(str);
56
0
        if (level < 0)
57
0
                return level;
58
59
0
        cached_log_level = level;
60
0
        return level;
61
0
}
62
63
static int log_fullv(
64
                int level,
65
                int error,
66
                const char *format,
67
323
                va_list ap) {
68
69
323
        int orig_errno = errno;
70
323
        const char *fmt;
71
72
323
        if (level > get_log_level())
73
323
                return -abs(error);
74
75
0
        if (endswith(format, "\n"))
76
0
                fmt = format;
77
0
        else
78
0
                fmt = strjoina(format, "\n");
79
80
0
        if (error != 0)
81
0
                errno = abs(error);
82
83
0
#pragma GCC diagnostic push
84
0
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
85
0
        vfprintf(stderr, fmt, ap);
86
0
#pragma GCC diagnostic pop
87
88
0
        errno = orig_errno;
89
0
        return -abs(error);
90
323
}
91
92
141
int log_info_errno(int error, const char* format, ...) {
93
141
        va_list ap;
94
141
        int r;
95
96
141
        va_start(ap, format);
97
141
        r = log_fullv(LOG_INFO, error, format, ap);
98
141
        va_end(ap);
99
100
141
        return r;
101
141
}
102
103
0
int log_error_errno(int error, const char* format, ...) {
104
0
        va_list ap;
105
0
        int r;
106
107
0
        va_start(ap, format);
108
0
        r = log_fullv(LOG_ERR, error, format, ap);
109
0
        va_end(ap);
110
111
0
        return r;
112
0
}
113
114
182
int log_debug_errno(int error, const char* format, ...) {
115
182
        va_list ap;
116
182
        int r;
117
118
182
        va_start(ap, format);
119
182
        r = log_fullv(LOG_DEBUG, error, format, ap);
120
182
        va_end(ap);
121
122
182
        return r;
123
182
}