1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "basic-forward.h"
10 typedef enum LogTarget
{
15 LOG_TARGET_CONSOLE_PREFIXED
,
16 LOG_TARGET_JOURNAL_OR_KMSG
,
17 LOG_TARGET_SYSLOG_OR_KMSG
,
18 LOG_TARGET_AUTO
, /* console if stderr is not journal, JOURNAL_OR_KMSG otherwise */
20 _LOG_TARGET_SINGLE_MAX
= LOG_TARGET_SYSLOG
+ 1,
21 _LOG_TARGET_MAX
= LOG_TARGET_NULL
+ 1,
22 _LOG_TARGET_INVALID
= -EINVAL
,
25 /* This log level disables logging completely. It can only be passed to log_set_max_level() and cannot be
26 * used as a regular log level. */
27 #define LOG_NULL (LOG_EMERG - 1)
28 assert_cc(LOG_NULL
== -1);
30 #define SYNTHETIC_ERRNO(num) (ABS(num) | (1 << 30))
31 #define IS_SYNTHETIC_ERRNO(val) (((val) >> 30) == 1)
32 #define ERRNO_VALUE(val) (ABS(val) & ~(1 << 30))
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
);
39 static inline void clear_log_syntax_callback(dummy_t
*dummy
) {
40 set_log_syntax_callback(/* cb= */ NULL
, /* userdata= */ NULL
);
43 const char* log_target_to_string(LogTarget target
) _const_
;
44 LogTarget
log_target_from_string(const char *s
) _pure_
;
45 void log_set_target(LogTarget target
);
46 void log_set_target_and_open(LogTarget target
);
47 int log_set_target_from_string(const char *e
);
48 LogTarget
log_get_target(void) _pure_
;
49 void log_settle_target(void);
51 int log_set_max_level(int level
);
52 int log_set_max_level_from_string(const char *e
);
53 int log_get_max_level(void) _pure_
;
54 int log_get_target_max_level(LogTarget target
);
55 int log_max_levels_to_string(int level
, char **ret
);
57 void log_set_facility(int facility
);
59 void log_show_color(bool b
);
60 bool log_get_show_color(void) _pure_
;
61 void log_show_location(bool b
);
62 bool log_get_show_location(void) _pure_
;
63 void log_show_time(bool b
);
64 bool log_get_show_time(void) _pure_
;
65 void log_show_tid(bool b
);
66 bool log_get_show_tid(void) _pure_
;
68 int log_show_color_from_string(const char *e
);
69 int log_show_location_from_string(const char *e
);
70 int log_show_time_from_string(const char *e
);
71 int log_show_tid_from_string(const char *e
);
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
75 * for the application itself. */
77 bool stderr_is_journal(void);
80 void log_forget_fds(void);
82 void log_parse_environment_variables(void);
83 void log_parse_environment(void);
85 int log_dispatch_internal(
91 const char *object_field
,
93 const char *extra_field
,
103 const char *format
, ...) _printf_(6,7);
112 va_list ap
) _printf_(6,0);
114 int log_object_internalv(
120 const char *object_field
,
122 const char *extra_field
,
125 va_list ap
) _printf_(10,0);
127 int log_object_internal(
133 const char *object_field
,
135 const char *extra_field
,
137 const char *format
, ...) _printf_(10,11);
139 int log_struct_internal(
145 const char *format
, ...) _sentinel_
;
147 int log_oom_internal(
153 int log_format_iovec(
157 bool newline_separator
,
160 va_list ap
) _printf_(6, 0);
162 int log_struct_iovec_internal(
168 const struct iovec
*input_iovec
,
169 size_t n_input_iovec
);
171 /* This modifies the buffer passed! */
172 int log_dump_internal(
180 #define log_dispatch(level, error, buffer) \
181 log_dispatch_internal(level, error, PROJECT_FILE, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
183 /* Logging with level */
184 #define log_full_errno_zerook(level, error, ...) \
186 int _level = (level), _e = (error); \
187 _e = (log_get_max_level() >= LOG_PRI(_level)) \
188 ? log_internal(_level, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
189 : -ERRNO_VALUE(_e); \
190 _e < 0 ? _e : -ESTRPIPE; \
193 #if BUILD_MODE_DEVELOPER && !defined(TEST_CODE)
194 # define ASSERT_NON_ZERO(x) assert((x) != 0)
195 # define ASSERT_UNDERFLOW(x) assert((x) >= INT_MIN)
197 # define ASSERT_NON_ZERO(x)
198 # define ASSERT_UNDERFLOW(x)
201 /* We often call log macros with ssize_t instead of int, so check for underflows,
202 * as ssize_t is not guaranteed to be the same as int, and we usually do
203 * 'return log_errno...' from functions that return 'int' */
204 #define log_full_errno(level, error, ...) \
206 int64_t _error = (error); \
207 ASSERT_UNDERFLOW(_error); \
208 ASSERT_NON_ZERO(_error); \
209 log_full_errno_zerook(level, (int)_error, __VA_ARGS__); \
212 #define log_full(level, fmt, ...) \
214 if (BUILD_MODE_DEVELOPER) \
215 assert(!strstr(fmt, "%m")); \
216 (void) log_full_errno_zerook(level, 0, fmt, ##__VA_ARGS__); \
219 int log_emergency_level(void);
222 #define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__)
223 #define log_info(...) log_full(LOG_INFO, __VA_ARGS__)
224 #define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__)
225 #define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
226 #define log_error(...) log_full(LOG_ERR, __VA_ARGS__)
227 #define log_emergency(...) log_full(log_emergency_level(), __VA_ARGS__)
229 /* Logging triggered by an errno-like error */
230 #define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__)
231 #define log_info_errno(error, ...) log_full_errno(LOG_INFO, error, __VA_ARGS__)
232 #define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__)
233 #define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__)
234 #define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__)
235 #define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__)
237 /* This logs at the specified level the first time it is called, and then
238 * logs at debug. If the specified level is debug, this logs only the first
239 * time it is called. */
240 #define log_once(level, ...) \
243 log_full(level, __VA_ARGS__); \
244 else if (LOG_PRI(level) != LOG_DEBUG) \
245 log_debug(__VA_ARGS__); \
248 #define log_once_errno(level, error, ...) \
250 int _err = (error); \
252 _err = log_full_errno(level, _err, __VA_ARGS__); \
253 else if (LOG_PRI(level) != LOG_DEBUG) \
254 _err = log_debug_errno(_err, __VA_ARGS__); \
256 _err = -ERRNO_VALUE(_err); \
261 # define log_trace(...) log_debug(__VA_ARGS__)
262 # define log_trace_errno(...) log_debug_errno(__VA_ARGS__)
264 # define log_trace(...) do {} while (0)
265 # define log_trace_errno(e, ...) (-ERRNO_VALUE(e))
268 /* Structured logging */
269 #define log_struct_errno(level, error, ...) \
270 log_struct_internal(level, error, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__, NULL)
271 #define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__)
273 #define log_struct_iovec_errno(level, error, iovec, n_iovec) \
274 log_struct_iovec_internal(level, error, PROJECT_FILE, __LINE__, __func__, iovec, n_iovec)
275 #define log_struct_iovec(level, iovec, n_iovec) log_struct_iovec_errno(level, 0, iovec, n_iovec)
277 /* This modifies the buffer passed! */
278 #define log_dump(level, buffer) \
279 log_dump_internal(level, 0, PROJECT_FILE, __LINE__, __func__, buffer)
281 #define log_oom_full(level) log_oom_internal(level, PROJECT_FILE, __LINE__, __func__)
282 #define log_oom() log_oom_full(LOG_ERR)
283 #define log_oom_debug() log_oom_full(LOG_DEBUG)
284 #define log_oom_warning() log_oom_full(LOG_WARNING)
286 bool log_on_console(void) _pure_
;
288 /* Helper to wrap the main message in structured logging. The macro doesn't do much,
289 * except to provide visual grouping of the format string and its arguments. */
290 #if LOG_MESSAGE_VERIFICATION || defined(__COVERITY__)
291 /* Do a fake formatting of the message string to let the scanner verify the arguments against the format
292 * message. The variable will never be set to true, but we don't tell the compiler that :) */
293 extern bool _log_message_dummy
;
294 # define LOG_ITEM(fmt, ...) "%.0d" fmt, (_log_message_dummy && printf(fmt, ##__VA_ARGS__)), ##__VA_ARGS__
295 # define LOG_MESSAGE(fmt, ...) LOG_ITEM("MESSAGE=" fmt, ##__VA_ARGS__)
297 # define LOG_ITEM(fmt, ...) fmt, ##__VA_ARGS__
298 # define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
301 #define LOG_MESSAGE_ID(id) LOG_ITEM("MESSAGE_ID=" id)
303 void log_received_signal(int level
, const struct signalfd_siginfo
*si
);
305 /* If turned on, any requests for a log target involving "syslog" will be implicitly upgraded to the equivalent journal target */
306 void log_set_upgrade_syslog_to_journal(bool b
);
308 /* If turned on, and log_open() is called, we'll not use STDERR_FILENO for logging ever, but rather open /dev/console */
309 void log_set_always_reopen_console(bool b
);
311 /* If turned on, we'll open the log stream implicitly if needed on each individual log call. This is normally not
312 * desired as we want to reuse our logging streams. It is useful however */
313 void log_set_open_when_needed(bool b
);
315 /* 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
316 * stderr, the console or kmsg */
317 void log_set_prohibit_ipc(bool b
);
319 int log_dup_console(void);
321 int log_syntax_internal(
324 const char *config_file
,
325 unsigned config_line
,
330 const char *format
, ...) _printf_(9, 10);
332 int log_syntax_invalid_utf8_internal(
335 const char *config_file
,
336 unsigned config_line
,
342 int log_syntax_parse_error_internal(
344 const char *config_file
,
345 unsigned config_line
,
347 bool critical
, /* When true, propagate the passed error, otherwise this always returns 0. */
354 #define log_syntax(unit, level, config_file, config_line, error, ...) \
356 int _level = (level), _e = (error); \
357 (log_get_max_level() >= LOG_PRI(_level)) \
358 ? log_syntax_internal(unit, _level, config_file, config_line, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
359 : -ERRNO_VALUE(_e); \
362 #define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \
364 int _level = (level); \
365 (log_get_max_level() >= LOG_PRI(_level)) \
366 ? log_syntax_invalid_utf8_internal(unit, _level, config_file, config_line, PROJECT_FILE, __LINE__, __func__, rvalue) \
370 #define log_syntax_parse_error_full(unit, config_file, config_line, error, critical, lvalue, rvalue) \
371 log_syntax_parse_error_internal(unit, config_file, config_line, error, critical, PROJECT_FILE, __LINE__, __func__, lvalue, rvalue)
373 #define log_syntax_parse_error(unit, config_file, config_line, error, lvalue, rvalue) \
374 log_syntax_parse_error_full(unit, config_file, config_line, error, /* critical = */ false, lvalue, rvalue)
376 #define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG)
378 void log_setup(void);
380 const char* _log_set_prefix(const char *prefix
, bool force
);
381 static inline const char* _log_unset_prefixp(const char **p
) {
383 _log_set_prefix(*p
, true);
387 #define LOG_SET_PREFIX(prefix) \
388 _cleanup_(_log_unset_prefixp) _unused_ const char *CONCATENATE(_cleanup_log_unset_prefix_, UNIQ) = _log_set_prefix(prefix, false);