1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
12 /* Some structures we reference but don't want to pull in headers for */
14 struct signalfd_siginfo
;
16 typedef enum LogTarget
{
18 LOG_TARGET_CONSOLE_PREFIXED
,
21 LOG_TARGET_JOURNAL_OR_KMSG
,
23 LOG_TARGET_SYSLOG_OR_KMSG
,
24 LOG_TARGET_AUTO
, /* console if stderr is not journal, JOURNAL_OR_KMSG otherwise */
27 _LOG_TARGET_INVALID
= -EINVAL
,
30 /* This log level disables logging completely. It can only be passed to log_set_max_level() and cannot be
31 * used a regular log level. */
32 #define LOG_NULL (LOG_EMERG - 1)
34 /* Note to readers: << and >> have lower precedence (are evaluated earlier) than & and | */
35 #define SYNTHETIC_ERRNO(num) (1 << 30 | (num))
36 #define IS_SYNTHETIC_ERRNO(val) ((val) >> 30 & 1)
37 #define ERRNO_VALUE(val) (abs(val) & ~(1 << 30))
39 /* The callback function to be invoked when syntax warnings are seen
40 * in the unit files. */
41 typedef void (*log_syntax_callback_t
)(const char *unit
, int level
, void *userdata
);
42 void set_log_syntax_callback(log_syntax_callback_t cb
, void *userdata
);
44 static inline void clear_log_syntax_callback(dummy_t
*dummy
) {
45 set_log_syntax_callback(/* cb= */ NULL
, /* userdata= */ NULL
);
48 const char *log_target_to_string(LogTarget target
) _const_
;
49 LogTarget
log_target_from_string(const char *s
) _pure_
;
50 void log_set_target(LogTarget target
);
51 int log_set_target_from_string(const char *e
);
52 LogTarget
log_get_target(void) _pure_
;
54 void log_set_max_level(int level
);
55 int log_set_max_level_from_string(const char *e
);
56 int log_get_max_level(void) _pure_
;
58 void log_set_facility(int facility
);
60 void log_show_color(bool b
);
61 bool log_get_show_color(void) _pure_
;
62 void log_show_location(bool b
);
63 bool log_get_show_location(void) _pure_
;
64 void log_show_time(bool b
);
65 bool log_get_show_time(void) _pure_
;
66 void log_show_tid(bool b
);
67 bool log_get_show_tid(void) _pure_
;
69 int log_show_color_from_string(const char *e
);
70 int log_show_location_from_string(const char *e
);
71 int log_show_time_from_string(const char *e
);
72 int log_show_tid_from_string(const char *e
);
74 /* Functions below that open and close logs or configure logging based on the
75 * environment should not be called from library code — this is always a job
76 * for the application itself. */
78 assert_cc(STRLEN(__FILE__
) > STRLEN(RELATIVE_SOURCE_PATH
) + 1);
79 #define PROJECT_FILE (&__FILE__[STRLEN(RELATIVE_SOURCE_PATH) + 1])
83 void log_forget_fds(void);
85 void log_parse_environment_variables(void);
86 void log_parse_environment(void);
88 int log_dispatch_internal(
94 const char *object_field
,
97 const char *extra_field
,
106 const char *format
, ...) _printf_(6,7);
115 va_list ap
) _printf_(6,0);
117 int log_object_internalv(
123 const char *object_field
,
125 const char *extra_field
,
128 va_list ap
) _printf_(10,0);
130 int log_object_internal(
136 const char *object_field
,
138 const char *extra_field
,
140 const char *format
, ...) _printf_(10,11);
142 int log_struct_internal(
148 const char *format
, ...) _printf_(6,0) _sentinel_
;
150 int log_oom_internal(
156 int log_format_iovec(
160 bool newline_separator
,
163 va_list ap
) _printf_(6, 0);
165 int log_struct_iovec_internal(
171 const struct iovec
*input_iovec
,
172 size_t n_input_iovec
);
174 /* This modifies the buffer passed! */
175 int log_dump_internal(
183 /* Logging for various assertions */
184 _noreturn_
void log_assert_failed(
190 _noreturn_
void log_assert_failed_unreachable(
195 void log_assert_failed_return(
201 #define log_dispatch(level, error, buffer) \
202 log_dispatch_internal(level, error, PROJECT_FILE, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
204 /* Logging with level */
205 #define log_full_errno_zerook(level, error, ...) \
207 int _level = (level), _e = (error); \
208 _e = (log_get_max_level() >= LOG_PRI(_level)) \
209 ? log_internal(_level, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
210 : -ERRNO_VALUE(_e); \
211 _e < 0 ? _e : -ESTRPIPE; \
214 #if BUILD_MODE_DEVELOPER && !defined(TEST_CODE)
215 # define ASSERT_NON_ZERO(x) assert((x) != 0)
217 # define ASSERT_NON_ZERO(x)
220 #define log_full_errno(level, error, ...) \
222 int _error = (error); \
223 ASSERT_NON_ZERO(_error); \
224 log_full_errno_zerook(level, _error, __VA_ARGS__); \
227 #define log_full(level, fmt, ...) \
229 if (BUILD_MODE_DEVELOPER) \
230 assert(!strstr(fmt, "%m")); \
231 (void) log_full_errno_zerook(level, 0, fmt, ##__VA_ARGS__); \
234 int log_emergency_level(void);
237 #define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__)
238 #define log_info(...) log_full(LOG_INFO, __VA_ARGS__)
239 #define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__)
240 #define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
241 #define log_error(...) log_full(LOG_ERR, __VA_ARGS__)
242 #define log_emergency(...) log_full(log_emergency_level(), __VA_ARGS__)
244 /* Logging triggered by an errno-like error */
245 #define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__)
246 #define log_info_errno(error, ...) log_full_errno(LOG_INFO, error, __VA_ARGS__)
247 #define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__)
248 #define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__)
249 #define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__)
250 #define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__)
252 /* This logs at the specified level the first time it is called, and then
253 * logs at debug. If the specified level is debug, this logs only the first
254 * time it is called. */
255 #define log_once(level, ...) \
258 log_full(level, __VA_ARGS__); \
259 else if (LOG_PRI(level) != LOG_DEBUG) \
260 log_debug(__VA_ARGS__); \
263 #define log_once_errno(level, error, ...) \
265 int _err = (error); \
267 _err = log_full_errno(level, _err, __VA_ARGS__); \
268 else if (LOG_PRI(level) != LOG_DEBUG) \
269 _err = log_debug_errno(_err, __VA_ARGS__); \
271 _err = -ERRNO_VALUE(_err); \
276 # define log_trace(...) log_debug(__VA_ARGS__)
277 # define log_trace_errno(...) log_debug_errno(__VA_ARGS__)
279 # define log_trace(...) do {} while (0)
280 # define log_trace_errno(e, ...) (-ERRNO_VALUE(e))
283 /* Structured logging */
284 #define log_struct_errno(level, error, ...) \
285 log_struct_internal(level, error, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__, NULL)
286 #define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__)
288 #define log_struct_iovec_errno(level, error, iovec, n_iovec) \
289 log_struct_iovec_internal(level, error, PROJECT_FILE, __LINE__, __func__, iovec, n_iovec)
290 #define log_struct_iovec(level, iovec, n_iovec) log_struct_iovec_errno(level, 0, iovec, n_iovec)
292 /* This modifies the buffer passed! */
293 #define log_dump(level, buffer) \
294 log_dump_internal(level, 0, PROJECT_FILE, __LINE__, __func__, buffer)
296 #define log_oom() log_oom_internal(LOG_ERR, PROJECT_FILE, __LINE__, __func__)
297 #define log_oom_debug() log_oom_internal(LOG_DEBUG, PROJECT_FILE, __LINE__, __func__)
299 bool log_on_console(void) _pure_
;
301 /* Helper to prepare various field for structured logging */
302 #define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
304 void log_received_signal(int level
, const struct signalfd_siginfo
*si
);
306 /* If turned on, any requests for a log target involving "syslog" will be implicitly upgraded to the equivalent journal target */
307 void log_set_upgrade_syslog_to_journal(bool b
);
309 /* If turned on, and log_open() is called, we'll not use STDERR_FILENO for logging ever, but rather open /dev/console */
310 void log_set_always_reopen_console(bool b
);
312 /* If turned on, we'll open the log stream implicitly if needed on each individual log call. This is normally not
313 * desired as we want to reuse our logging streams. It is useful however */
314 void log_set_open_when_needed(bool b
);
316 /* 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
317 * stderr, the console or kmsg */
318 void log_set_prohibit_ipc(bool b
);
320 int log_dup_console(void);
322 int log_syntax_internal(
325 const char *config_file
,
326 unsigned config_line
,
331 const char *format
, ...) _printf_(9, 10);
333 int log_syntax_invalid_utf8_internal(
336 const char *config_file
,
337 unsigned config_line
,
343 #define log_syntax(unit, level, config_file, config_line, error, ...) \
345 int _level = (level), _e = (error); \
346 (log_get_max_level() >= LOG_PRI(_level)) \
347 ? log_syntax_internal(unit, _level, config_file, config_line, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
348 : -ERRNO_VALUE(_e); \
351 #define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \
353 int _level = (level); \
354 (log_get_max_level() >= LOG_PRI(_level)) \
355 ? log_syntax_invalid_utf8_internal(unit, _level, config_file, config_line, PROJECT_FILE, __LINE__, __func__, rvalue) \
359 #define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG)
361 void log_setup(void);