]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
c2f1db8f | 2 | #pragma once |
5899f3b7 | 3 | |
0c15577a | 4 | #include <stdio.h> |
63275a70 | 5 | #include <string.h> |
07630cea | 6 | #include <syslog.h> |
5899f3b7 | 7 | |
0c15577a | 8 | #include "forward.h" |
dccca82b | 9 | |
16801e90 LP |
10 | typedef enum LogTarget{ |
11 | LOG_TARGET_CONSOLE, | |
16801e90 | 12 | LOG_TARGET_KMSG, |
5ba081b0 | 13 | LOG_TARGET_JOURNAL, |
843d2643 | 14 | LOG_TARGET_SYSLOG, |
e8815abf DDM |
15 | LOG_TARGET_CONSOLE_PREFIXED, |
16 | LOG_TARGET_JOURNAL_OR_KMSG, | |
843d2643 | 17 | LOG_TARGET_SYSLOG_OR_KMSG, |
27ffec08 | 18 | LOG_TARGET_AUTO, /* console if stderr is not journal, JOURNAL_OR_KMSG otherwise */ |
9fae33d2 | 19 | LOG_TARGET_NULL, |
e8815abf DDM |
20 | _LOG_TARGET_SINGLE_MAX = LOG_TARGET_SYSLOG + 1, |
21 | _LOG_TARGET_MAX = LOG_TARGET_NULL + 1, | |
2d93c20e | 22 | _LOG_TARGET_INVALID = -EINVAL, |
ff524019 ZJS |
23 | } LogTarget; |
24 | ||
9c416180 | 25 | /* This log level disables logging completely. It can only be passed to log_set_max_level() and cannot be |
e55db9e7 | 26 | * used as a regular log level. */ |
9c416180 | 27 | #define LOG_NULL (LOG_EMERG - 1) |
fa3137f9 | 28 | assert_cc(LOG_NULL == -1); |
9c416180 | 29 | |
40a6cdc2 | 30 | #define SYNTHETIC_ERRNO(num) (ABS(num) | (1 << 30)) |
268f5807 | 31 | #define IS_SYNTHETIC_ERRNO(val) (((val) >> 30) == 1) |
40a6cdc2 | 32 | #define ERRNO_VALUE(val) (ABS(val) & ~(1 << 30)) |
16801e90 | 33 | |
3cc3dc77 MG |
34 | /* The callback function to be invoked when syntax warnings are seen |
35 | * in the unit files. */ | |
36 | typedef void (*log_syntax_callback_t)(const char *unit, int level, void *userdata); | |
37 | void set_log_syntax_callback(log_syntax_callback_t cb, void *userdata); | |
38 | ||
39 | static inline void clear_log_syntax_callback(dummy_t *dummy) { | |
40 | set_log_syntax_callback(/* cb= */ NULL, /* userdata= */ NULL); | |
41 | } | |
42 | ||
bfd5a068 | 43 | const char* log_target_to_string(LogTarget target) _const_; |
9fdee66f | 44 | LogTarget log_target_from_string(const char *s) _pure_; |
16801e90 | 45 | void log_set_target(LogTarget target); |
1e344c1d | 46 | void log_set_target_and_open(LogTarget target); |
9fdee66f YW |
47 | int log_set_target_from_string(const char *e); |
48 | LogTarget log_get_target(void) _pure_; | |
a3b00f91 | 49 | void log_settle_target(void); |
1c36b8bf | 50 | |
7881f485 | 51 | int log_set_max_level(int level); |
9fdee66f YW |
52 | int log_set_max_level_from_string(const char *e); |
53 | int log_get_max_level(void) _pure_; | |
92e8e2b0 | 54 | int log_get_target_max_level(LogTarget target); |
e8815abf | 55 | int log_max_levels_to_string(int level, char **ret); |
1c36b8bf | 56 | |
3eff4208 | 57 | void log_set_facility(int facility); |
16801e90 | 58 | |
bbe63281 | 59 | void log_show_color(bool b); |
b1e90ec5 | 60 | bool log_get_show_color(void) _pure_; |
bbe63281 | 61 | void log_show_location(bool b); |
b1e90ec5 | 62 | bool log_get_show_location(void) _pure_; |
c5673ed0 DS |
63 | void log_show_time(bool b); |
64 | bool log_get_show_time(void) _pure_; | |
9ee806d1 LP |
65 | void log_show_tid(bool b); |
66 | bool log_get_show_tid(void) _pure_; | |
bbe63281 LP |
67 | |
68 | int log_show_color_from_string(const char *e); | |
69 | int log_show_location_from_string(const char *e); | |
c5673ed0 | 70 | int log_show_time_from_string(const char *e); |
9ee806d1 | 71 | int log_show_tid_from_string(const char *e); |
bbe63281 | 72 | |
e3e42fc2 ZJS |
73 | /* Functions below that open and close logs or configure logging based on the |
74 | * environment should not be called from library code — this is always a job | |
9fdee66f | 75 | * for the application itself. */ |
e3e42fc2 | 76 | |
7a7d7a2e | 77 | bool stderr_is_journal(void); |
843d2643 | 78 | int log_open(void); |
871e5809 | 79 | void log_close(void); |
4d8a7798 | 80 | void log_forget_fds(void); |
843d2643 | 81 | |
a7d15a24 | 82 | void log_parse_environment_variables(void); |
9fdee66f | 83 | void log_parse_environment(void); |
34f0e866 | 84 | |
93484b47 ZJS |
85 | int log_dispatch_internal( |
86 | int level, | |
87 | int error, | |
88 | const char *file, | |
89 | int line, | |
90 | const char *func, | |
91 | const char *object_field, | |
92 | const char *object, | |
93484b47 | 93 | const char *extra_field, |
0425fc5e | 94 | const char *extra, |
93484b47 ZJS |
95 | char *buffer); |
96 | ||
9fdee66f | 97 | int log_internal( |
877d54e9 | 98 | int level, |
086891e5 | 99 | int error, |
79008bdd | 100 | const char *file, |
877d54e9 LP |
101 | int line, |
102 | const char *func, | |
086891e5 | 103 | const char *format, ...) _printf_(6,7); |
5899f3b7 | 104 | |
9fdee66f | 105 | int log_internalv( |
877d54e9 | 106 | int level, |
086891e5 | 107 | int error, |
79008bdd | 108 | const char *file, |
877d54e9 LP |
109 | int line, |
110 | const char *func, | |
111 | const char *format, | |
086891e5 | 112 | va_list ap) _printf_(6,0); |
877d54e9 | 113 | |
f6d6d532 YW |
114 | int log_object_internalv( |
115 | int level, | |
116 | int error, | |
117 | const char *file, | |
118 | int line, | |
119 | const char *func, | |
120 | const char *object_field, | |
121 | const char *object, | |
122 | const char *extra_field, | |
123 | const char *extra, | |
124 | const char *format, | |
125 | va_list ap) _printf_(10,0); | |
126 | ||
79008bdd | 127 | int log_object_internal( |
fdf9f9bb | 128 | int level, |
086891e5 | 129 | int error, |
79008bdd | 130 | const char *file, |
fdf9f9bb ZJS |
131 | int line, |
132 | const char *func, | |
79008bdd | 133 | const char *object_field, |
fdf9f9bb | 134 | const char *object, |
4b58153d LP |
135 | const char *extra_field, |
136 | const char *extra, | |
137 | const char *format, ...) _printf_(10,11); | |
fdf9f9bb | 138 | |
877d54e9 LP |
139 | int log_struct_internal( |
140 | int level, | |
086891e5 | 141 | int error, |
877d54e9 LP |
142 | const char *file, |
143 | int line, | |
144 | const char *func, | |
3cf6a3a3 | 145 | const char *format, ...) _sentinel_; |
877d54e9 LP |
146 | |
147 | int log_oom_internal( | |
b3a79158 | 148 | int level, |
877d54e9 LP |
149 | const char *file, |
150 | int line, | |
151 | const char *func); | |
185986c6 | 152 | |
8a03c9ef ZJS |
153 | int log_format_iovec( |
154 | struct iovec *iovec, | |
d3070fbd LP |
155 | size_t iovec_len, |
156 | size_t *n, | |
8a03c9ef ZJS |
157 | bool newline_separator, |
158 | int error, | |
159 | const char *format, | |
ba360bb0 | 160 | va_list ap) _printf_(6, 0); |
8a03c9ef | 161 | |
915b1d01 LP |
162 | int log_struct_iovec_internal( |
163 | int level, | |
164 | int error, | |
165 | const char *file, | |
166 | int line, | |
167 | const char *func, | |
dccca82b | 168 | const struct iovec *input_iovec, |
915b1d01 LP |
169 | size_t n_input_iovec); |
170 | ||
2149e37c LP |
171 | /* This modifies the buffer passed! */ |
172 | int log_dump_internal( | |
877d54e9 | 173 | int level, |
086891e5 | 174 | int error, |
79008bdd | 175 | const char *file, |
877d54e9 LP |
176 | int line, |
177 | const char *func, | |
178 | char *buffer); | |
179 | ||
93484b47 | 180 | #define log_dispatch(level, error, buffer) \ |
62c6bbbc | 181 | log_dispatch_internal(level, error, PROJECT_FILE, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer) |
93484b47 | 182 | |
086891e5 | 183 | /* Logging with level */ |
a626cb15 | 184 | #define log_full_errno_zerook(level, error, ...) \ |
1cfa9a4c | 185 | ({ \ |
5df4f46f | 186 | int _level = (level), _e = (error); \ |
cbe97b9c | 187 | _e = (log_get_max_level() >= LOG_PRI(_level)) \ |
5df4f46f | 188 | ? log_internal(_level, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \ |
baaa35ad | 189 | : -ERRNO_VALUE(_e); \ |
59e8042e | 190 | _e < 0 ? _e : -ESTRPIPE; \ |
bf371116 | 191 | }) |
87ff6b1c | 192 | |
a626cb15 ZJS |
193 | #if BUILD_MODE_DEVELOPER && !defined(TEST_CODE) |
194 | # define ASSERT_NON_ZERO(x) assert((x) != 0) | |
195 | #else | |
196 | # define ASSERT_NON_ZERO(x) | |
197 | #endif | |
198 | ||
199 | #define log_full_errno(level, error, ...) \ | |
200 | ({ \ | |
201 | int _error = (error); \ | |
202 | ASSERT_NON_ZERO(_error); \ | |
203 | log_full_errno_zerook(level, _error, __VA_ARGS__); \ | |
204 | }) | |
205 | ||
63275a70 ZJS |
206 | #define log_full(level, fmt, ...) \ |
207 | ({ \ | |
208 | if (BUILD_MODE_DEVELOPER) \ | |
209 | assert(!strstr(fmt, "%m")); \ | |
a626cb15 | 210 | (void) log_full_errno_zerook(level, 0, fmt, ##__VA_ARGS__); \ |
63275a70 | 211 | }) |
086891e5 | 212 | |
dccca82b LP |
213 | int log_emergency_level(void); |
214 | ||
086891e5 | 215 | /* Normal logging */ |
75029e15 | 216 | #define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__) |
4104970e ZJS |
217 | #define log_info(...) log_full(LOG_INFO, __VA_ARGS__) |
218 | #define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__) | |
219 | #define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__) | |
220 | #define log_error(...) log_full(LOG_ERR, __VA_ARGS__) | |
dccca82b | 221 | #define log_emergency(...) log_full(log_emergency_level(), __VA_ARGS__) |
5899f3b7 | 222 | |
086891e5 LP |
223 | /* Logging triggered by an errno-like error */ |
224 | #define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__) | |
225 | #define log_info_errno(error, ...) log_full_errno(LOG_INFO, error, __VA_ARGS__) | |
226 | #define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__) | |
227 | #define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__) | |
228 | #define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__) | |
dccca82b | 229 | #define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__) |
086891e5 | 230 | |
264f0afe DS |
231 | /* This logs at the specified level the first time it is called, and then |
232 | * logs at debug. If the specified level is debug, this logs only the first | |
233 | * time it is called. */ | |
234 | #define log_once(level, ...) \ | |
235 | ({ \ | |
236 | if (ONCE) \ | |
237 | log_full(level, __VA_ARGS__); \ | |
238 | else if (LOG_PRI(level) != LOG_DEBUG) \ | |
239 | log_debug(__VA_ARGS__); \ | |
240 | }) | |
241 | ||
242 | #define log_once_errno(level, error, ...) \ | |
243 | ({ \ | |
244 | int _err = (error); \ | |
245 | if (ONCE) \ | |
246 | _err = log_full_errno(level, _err, __VA_ARGS__); \ | |
247 | else if (LOG_PRI(level) != LOG_DEBUG) \ | |
248 | _err = log_debug_errno(_err, __VA_ARGS__); \ | |
249 | else \ | |
250 | _err = -ERRNO_VALUE(_err); \ | |
251 | _err; \ | |
252 | }) | |
253 | ||
e355fb6f | 254 | #if LOG_TRACE |
68a2ed61 YW |
255 | # define log_trace(...) log_debug(__VA_ARGS__) |
256 | # define log_trace_errno(...) log_debug_errno(__VA_ARGS__) | |
cb41ff29 | 257 | #else |
68a2ed61 YW |
258 | # define log_trace(...) do {} while (0) |
259 | # define log_trace_errno(e, ...) (-ERRNO_VALUE(e)) | |
cb41ff29 ZJS |
260 | #endif |
261 | ||
086891e5 | 262 | /* Structured logging */ |
9fdee66f YW |
263 | #define log_struct_errno(level, error, ...) \ |
264 | log_struct_internal(level, error, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__, NULL) | |
ff524019 | 265 | #define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__) |
0d0f0c50 | 266 | |
915b1d01 | 267 | #define log_struct_iovec_errno(level, error, iovec, n_iovec) \ |
9fdee66f | 268 | log_struct_iovec_internal(level, error, PROJECT_FILE, __LINE__, __func__, iovec, n_iovec) |
915b1d01 LP |
269 | #define log_struct_iovec(level, iovec, n_iovec) log_struct_iovec_errno(level, 0, iovec, n_iovec) |
270 | ||
2149e37c | 271 | /* This modifies the buffer passed! */ |
9fdee66f YW |
272 | #define log_dump(level, buffer) \ |
273 | log_dump_internal(level, 0, PROJECT_FILE, __LINE__, __func__, buffer) | |
086891e5 | 274 | |
572d031e YW |
275 | #define log_oom_full(level) log_oom_internal(level, PROJECT_FILE, __LINE__, __func__) |
276 | #define log_oom() log_oom_full(LOG_ERR) | |
277 | #define log_oom_debug() log_oom_full(LOG_DEBUG) | |
278 | #define log_oom_warning() log_oom_full(LOG_WARNING) | |
2149e37c | 279 | |
44a6b1b6 | 280 | bool log_on_console(void) _pure_; |
81270860 | 281 | |
92663a5e ZJS |
282 | /* Helper to wrap the main message in structured logging. The macro doesn't do much, |
283 | * except to provide visual grouping of the format string and its arguments. */ | |
1ec7c156 | 284 | #if LOG_MESSAGE_VERIFICATION || defined(__COVERITY__) |
011a03a3 ZJS |
285 | /* Do a fake formatting of the message string to let the scanner verify the arguments against the format |
286 | * message. The variable will never be set to true, but we don't tell the compiler that :) */ | |
287 | extern bool _log_message_dummy; | |
3cf6a3a3 YW |
288 | # define LOG_ITEM(fmt, ...) "%.0d" fmt, (_log_message_dummy && printf(fmt, ##__VA_ARGS__)), ##__VA_ARGS__ |
289 | # define LOG_MESSAGE(fmt, ...) LOG_ITEM("MESSAGE=" fmt, ##__VA_ARGS__) | |
b9ce5cf9 | 290 | #else |
3cf6a3a3 | 291 | # define LOG_ITEM(fmt, ...) fmt, ##__VA_ARGS__ |
b9ce5cf9 ZJS |
292 | # define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__ |
293 | #endif | |
4daf54a8 | 294 | |
3cf6a3a3 YW |
295 | #define LOG_MESSAGE_ID(id) LOG_ITEM("MESSAGE_ID=" id) |
296 | ||
4daf54a8 | 297 | void log_received_signal(int level, const struct signalfd_siginfo *si); |
c1dc6153 | 298 | |
6fdb8de4 | 299 | /* If turned on, any requests for a log target involving "syslog" will be implicitly upgraded to the equivalent journal target */ |
c1dc6153 | 300 | void log_set_upgrade_syslog_to_journal(bool b); |
7a3be263 LP |
301 | |
302 | /* If turned on, and log_open() is called, we'll not use STDERR_FILENO for logging ever, but rather open /dev/console */ | |
48a601fe | 303 | void log_set_always_reopen_console(bool b); |
7a3be263 LP |
304 | |
305 | /* If turned on, we'll open the log stream implicitly if needed on each individual log call. This is normally not | |
306 | * desired as we want to reuse our logging streams. It is useful however */ | |
16e4fd87 | 307 | void log_set_open_when_needed(bool b); |
158350e8 | 308 | |
adf47c91 LP |
309 | /* If turned on, then we'll never use IPC-based logging, i.e. never log to syslog or the journal. We'll only log to |
310 | * stderr, the console or kmsg */ | |
311 | void log_set_prohibit_ipc(bool b); | |
312 | ||
17cac366 LP |
313 | int log_dup_console(void); |
314 | ||
158350e8 LP |
315 | int log_syntax_internal( |
316 | const char *unit, | |
317 | int level, | |
318 | const char *config_file, | |
319 | unsigned config_line, | |
320 | int error, | |
321 | const char *file, | |
322 | int line, | |
323 | const char *func, | |
324 | const char *format, ...) _printf_(9, 10); | |
325 | ||
d04ce5a9 LP |
326 | int log_syntax_invalid_utf8_internal( |
327 | const char *unit, | |
328 | int level, | |
329 | const char *config_file, | |
330 | unsigned config_line, | |
331 | const char *file, | |
332 | int line, | |
333 | const char *func, | |
334 | const char *rvalue); | |
335 | ||
1e04eb00 YW |
336 | int log_syntax_parse_error_internal( |
337 | const char *unit, | |
338 | const char *config_file, | |
339 | unsigned config_line, | |
340 | int error, | |
341 | bool critical, /* When true, propagate the passed error, otherwise this always returns 0. */ | |
342 | const char *file, | |
343 | int line, | |
344 | const char *func, | |
345 | const char *lvalue, | |
346 | const char *rvalue); | |
347 | ||
158350e8 LP |
348 | #define log_syntax(unit, level, config_file, config_line, error, ...) \ |
349 | ({ \ | |
350 | int _level = (level), _e = (error); \ | |
351 | (log_get_max_level() >= LOG_PRI(_level)) \ | |
62c6bbbc | 352 | ? log_syntax_internal(unit, _level, config_file, config_line, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \ |
ee96382f | 353 | : -ERRNO_VALUE(_e); \ |
158350e8 | 354 | }) |
0e05ee04 LP |
355 | |
356 | #define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \ | |
357 | ({ \ | |
358 | int _level = (level); \ | |
d04ce5a9 | 359 | (log_get_max_level() >= LOG_PRI(_level)) \ |
62c6bbbc | 360 | ? log_syntax_invalid_utf8_internal(unit, _level, config_file, config_line, PROJECT_FILE, __LINE__, __func__, rvalue) \ |
d04ce5a9 | 361 | : -EINVAL; \ |
0e05ee04 | 362 | }) |
f1d34068 | 363 | |
1e04eb00 YW |
364 | #define log_syntax_parse_error_full(unit, config_file, config_line, error, critical, lvalue, rvalue) \ |
365 | log_syntax_parse_error_internal(unit, config_file, config_line, error, critical, PROJECT_FILE, __LINE__, __func__, lvalue, rvalue) | |
366 | ||
367 | #define log_syntax_parse_error(unit, config_file, config_line, error, lvalue, rvalue) \ | |
368 | log_syntax_parse_error_full(unit, config_file, config_line, error, /* critical = */ false, lvalue, rvalue) | |
369 | ||
f1d34068 | 370 | #define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG) |
6bf3c61c | 371 | |
d2acb93d | 372 | void log_setup(void); |
34683dbd | 373 | |
bfd5a068 ZJS |
374 | const char* _log_set_prefix(const char *prefix, bool force); |
375 | static inline const char* _log_unset_prefixp(const char **p) { | |
ee2975a9 DDM |
376 | assert(p); |
377 | _log_set_prefix(*p, true); | |
378 | return NULL; | |
379 | } | |
380 | ||
381 | #define LOG_SET_PREFIX(prefix) \ | |
382 | _cleanup_(_log_unset_prefixp) _unused_ const char *CONCATENATE(_cleanup_log_unset_prefix_, UNIQ) = _log_set_prefix(prefix, false); |