• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.59
/src/basic/log-context.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <threads.h>
4

5
#include "alloc-util.h"
6
#include "env-util.h"
7
#include "iovec-util.h"
8
#include "log.h"
9
#include "log-context.h"
10
#include "string-util.h"
11
#include "strv.h"
12

13
static int saved_log_context_enabled = -1;
14
thread_local LIST_HEAD(LogContext, _log_context) = NULL;
15
thread_local size_t _log_context_num_fields = 0;
16

17
bool log_context_enabled(void) {
749,857✔
18
        int r;
749,857✔
19

20
        if (log_get_max_level() == LOG_DEBUG)
749,857✔
21
                return true;
22

23
        if (saved_log_context_enabled >= 0)
114,675✔
24
                return saved_log_context_enabled;
107,322✔
25

26
        r = secure_getenv_bool("SYSTEMD_ENABLE_LOG_CONTEXT");
7,353✔
27
        if (r < 0 && r != -ENXIO)
7,353✔
UNCOV
28
                log_debug_errno(r, "Failed to parse $SYSTEMD_ENABLE_LOG_CONTEXT, ignoring: %m");
×
29

30
        saved_log_context_enabled = r > 0;
7,353✔
31

32
        return saved_log_context_enabled;
7,353✔
33
}
34

35
static LogContext* log_context_attach(LogContext *c) {
663,915✔
36
        assert(c);
663,915✔
37

38
        _log_context_num_fields += strv_length(c->fields);
663,915✔
39
        _log_context_num_fields += c->n_input_iovec;
663,915✔
40
        _log_context_num_fields += !!c->key;
663,915✔
41

42
        return LIST_PREPEND(ll, _log_context, c);
663,915✔
43
}
44

45
static LogContext* log_context_detach(LogContext *c) {
640,731✔
46
        if (!c)
640,731✔
47
                return NULL;
48

49
        assert(_log_context_num_fields >= strv_length(c->fields) + c->n_input_iovec +!!c->key);
640,731✔
50
        _log_context_num_fields -= strv_length(c->fields);
640,731✔
51
        _log_context_num_fields -= c->n_input_iovec;
640,731✔
52
        _log_context_num_fields -= !!c->key;
640,731✔
53

54
        LIST_REMOVE(ll, _log_context, c);
640,731✔
55
        return NULL;
640,731✔
56
}
57

58
LogContext* log_context_new(const char *key, const char *value) {
50,467✔
59
        assert(key);
50,467✔
60
        assert(endswith(key, "="));
50,467✔
61
        assert(value);
50,467✔
62

63
        LIST_FOREACH(ll, i, _log_context)
76,088✔
64
                if (i->key == key && i->value == value)
48,622✔
65
                        return log_context_ref(i);
23,001✔
66

67
        LogContext *c = new(LogContext, 1);
27,466✔
68
        if (!c)
27,466✔
69
                return NULL;
70

71
        *c = (LogContext) {
27,466✔
72
                .n_ref = 1,
73
                .key = (char *) key,
74
                .value = (char *) value,
75
        };
76

77
        return log_context_attach(c);
27,466✔
78
}
79

80
LogContext* log_context_new_strv(char **fields, bool owned) {
637,327✔
81
        if (!fields)
637,327✔
82
                return NULL;
83

84
        LIST_FOREACH(ll, i, _log_context)
637,316✔
85
                if (i->fields == fields) {
1,085✔
86
                        assert(!owned);
18✔
87
                        return log_context_ref(i);
18✔
88
                }
89

90
        LogContext *c = new(LogContext, 1);
636,231✔
91
        if (!c)
636,231✔
92
                return NULL;
93

94
        *c = (LogContext) {
636,231✔
95
                .n_ref = 1,
96
                .fields = fields,
97
                .owned = owned,
98
        };
99

100
        return log_context_attach(c);
636,231✔
101
}
102

103
LogContext* log_context_new_iov(struct iovec *input_iovec, size_t n_input_iovec, bool owned) {
289,001✔
104
        if (!input_iovec || n_input_iovec == 0)
289,001✔
105
                return NULL;
106

107
        LIST_FOREACH(ll, i, _log_context)
821✔
108
                if (i->input_iovec == input_iovec && i->n_input_iovec == n_input_iovec) {
603✔
109
                        assert(!owned);
204✔
110
                        return log_context_ref(i);
204✔
111
                }
112

113
        LogContext *c = new(LogContext, 1);
218✔
114
        if (!c)
218✔
115
                return NULL;
116

117
        *c = (LogContext) {
218✔
118
                .n_ref = 1,
119
                .input_iovec = input_iovec,
120
                .n_input_iovec = n_input_iovec,
121
                .owned = owned,
122
        };
123

124
        return log_context_attach(c);
218✔
125
}
126

127
static LogContext* log_context_free(LogContext *c) {
640,731✔
128
        if (!c)
640,731✔
129
                return NULL;
130

131
        log_context_detach(c);
640,731✔
132

133
        if (c->owned) {
640,731✔
134
                strv_free(c->fields);
635,145✔
135
                iovec_array_free(c->input_iovec, c->n_input_iovec);
635,145✔
136
                free(c->key);
635,145✔
137
                free(c->value);
635,145✔
138
        }
139

140
        return mfree(c);
640,731✔
141
}
142

143
DEFINE_TRIVIAL_REF_UNREF_FUNC(LogContext, log_context, log_context_free);
664,048✔
144

145
LogContext* log_context_new_strv_consume(char **fields) {
635,191✔
146
        LogContext *c = log_context_new_strv(fields, /*owned=*/ true);
635,191✔
147
        if (!c)
635,191✔
UNCOV
148
                strv_free(fields);
×
149

150
        return c;
635,191✔
151
}
152

153
LogContext* log_context_new_iov_consume(struct iovec *input_iovec, size_t n_input_iovec) {
9✔
154
        LogContext *c = log_context_new_iov(input_iovec, n_input_iovec, /*owned=*/ true);
9✔
155
        if (!c)
9✔
UNCOV
156
                iovec_array_free(input_iovec, n_input_iovec);
×
157

158
        return c;
9✔
159
}
160

161
LogContext* log_context_head(void) {
4,121,454✔
162
        return _log_context;
4,121,454✔
163
}
164

165
size_t log_context_num_contexts(void) {
81✔
166
        size_t n = 0;
81✔
167

168
        LIST_FOREACH(ll, c, _log_context)
225✔
169
                n++;
144✔
170

171
        return n;
81✔
172
}
173

174
size_t log_context_num_fields(void) {
4,121,535✔
175
        return _log_context_num_fields;
4,121,535✔
176
}
177

178
void _reset_log_level(int *saved_log_level) {
266,040✔
179
        assert(saved_log_level);
266,040✔
180

181
        log_set_max_level(*saved_log_level);
266,040✔
182
}
266,040✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc