]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/log.h
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / basic / log.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c2f1db8f 2#pragma once
5899f3b7 3
17a94911 4#include <stdarg.h>
07630cea 5#include <stdbool.h>
6410074b 6#include <stdlib.h>
07630cea 7#include <syslog.h>
5899f3b7 8
158350e8 9#include "macro.h"
dccca82b
LP
10
11/* Some structures we reference but don't want to pull in headers for */
12struct iovec;
13struct signalfd_siginfo;
5899f3b7 14
ff524019
ZJS
15typedef enum LogRealm {
16 LOG_REALM_SYSTEMD,
17 LOG_REALM_UDEV,
18 _LOG_REALM_MAX,
19} LogRealm;
20
21#ifndef LOG_REALM
22# define LOG_REALM LOG_REALM_SYSTEMD
23#endif
24
16801e90
LP
25typedef enum LogTarget{
26 LOG_TARGET_CONSOLE,
aca83a53 27 LOG_TARGET_CONSOLE_PREFIXED,
16801e90 28 LOG_TARGET_KMSG,
5ba081b0
LP
29 LOG_TARGET_JOURNAL,
30 LOG_TARGET_JOURNAL_OR_KMSG,
843d2643
LP
31 LOG_TARGET_SYSLOG,
32 LOG_TARGET_SYSLOG_OR_KMSG,
5ba081b0 33 LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */
9fae33d2 34 LOG_TARGET_NULL,
16801e90
LP
35 _LOG_TARGET_MAX,
36 _LOG_TARGET_INVALID = -1
ff524019
ZJS
37} LogTarget;
38
5dc881a3 39/* Note to readers: << and >> have lower precedence than & and | */
52d86690
ZJS
40#define LOG_REALM_PLUS_LEVEL(realm, level) ((realm) << 10 | (level))
41#define LOG_REALM_REMOVE_LEVEL(realm_level) ((realm_level) >> 10)
42#define SYNTHETIC_ERRNO(num) (1 << 30 | (num))
43#define IS_SYNTHETIC_ERRNO(val) ((val) >> 30 & 1)
44#define ERRNO_VALUE(val) (abs(val) & 255)
16801e90
LP
45
46void log_set_target(LogTarget target);
ff524019
ZJS
47void log_set_max_level_realm(LogRealm realm, int level);
48#define log_set_max_level(level) \
49 log_set_max_level_realm(LOG_REALM, (level))
50
3eff4208 51void log_set_facility(int facility);
16801e90 52
34f0e866 53int log_set_target_from_string(const char *e);
ff524019
ZJS
54int log_set_max_level_from_string_realm(LogRealm realm, const char *e);
55#define log_set_max_level_from_string(e) \
56 log_set_max_level_from_string_realm(LOG_REALM, (e))
34f0e866 57
bbe63281 58void log_show_color(bool b);
b1e90ec5 59bool log_get_show_color(void) _pure_;
bbe63281 60void log_show_location(bool b);
b1e90ec5 61bool log_get_show_location(void) _pure_;
bbe63281
LP
62
63int log_show_color_from_string(const char *e);
64int log_show_location_from_string(const char *e);
65
44a6b1b6 66LogTarget log_get_target(void) _pure_;
ff524019
ZJS
67int log_get_max_level_realm(LogRealm realm) _pure_;
68#define log_get_max_level() \
69 log_get_max_level_realm(LOG_REALM)
1adf1049 70
e3e42fc2
ZJS
71/* Functions below that open and close logs or configure logging based on the
72 * environment should not be called from library code — this is always a job
73 * for the application itself.
74 */
75
843d2643 76int log_open(void);
871e5809 77void log_close(void);
4d8a7798 78void log_forget_fds(void);
843d2643 79
ff524019
ZJS
80void log_parse_environment_realm(LogRealm realm);
81#define log_parse_environment() \
82 log_parse_environment_realm(LOG_REALM)
34f0e866 83
93484b47
ZJS
84int log_dispatch_internal(
85 int level,
86 int error,
87 const char *file,
88 int line,
89 const char *func,
90 const char *object_field,
91 const char *object,
92 const char *extra,
93 const char *extra_field,
94 char *buffer);
95
ff524019 96int log_internal_realm(
877d54e9 97 int level,
086891e5 98 int error,
79008bdd 99 const char *file,
877d54e9
LP
100 int line,
101 const char *func,
086891e5 102 const char *format, ...) _printf_(6,7);
ff524019
ZJS
103#define log_internal(level, ...) \
104 log_internal_realm(LOG_REALM_PLUS_LEVEL(LOG_REALM, (level)), __VA_ARGS__)
5899f3b7 105
ff524019 106int log_internalv_realm(
877d54e9 107 int level,
086891e5 108 int error,
79008bdd 109 const char *file,
877d54e9
LP
110 int line,
111 const char *func,
112 const char *format,
086891e5 113 va_list ap) _printf_(6,0);
ff524019
ZJS
114#define log_internalv(level, ...) \
115 log_internalv_realm(LOG_REALM_PLUS_LEVEL(LOG_REALM, (level)), __VA_ARGS__)
877d54e9 116
ff524019 117/* Realm is fixed to LOG_REALM_SYSTEMD for those */
79008bdd 118int log_object_internal(
fdf9f9bb 119 int level,
086891e5 120 int error,
79008bdd 121 const char *file,
fdf9f9bb
ZJS
122 int line,
123 const char *func,
79008bdd 124 const char *object_field,
fdf9f9bb 125 const char *object,
4b58153d
LP
126 const char *extra_field,
127 const char *extra,
128 const char *format, ...) _printf_(10,11);
fdf9f9bb 129
877d54e9
LP
130int log_struct_internal(
131 int level,
086891e5 132 int error,
877d54e9
LP
133 const char *file,
134 int line,
135 const char *func,
086891e5 136 const char *format, ...) _printf_(6,0) _sentinel_;
877d54e9
LP
137
138int log_oom_internal(
ff524019 139 LogRealm realm,
877d54e9
LP
140 const char *file,
141 int line,
142 const char *func);
185986c6 143
8a03c9ef
ZJS
144int log_format_iovec(
145 struct iovec *iovec,
d3070fbd
LP
146 size_t iovec_len,
147 size_t *n,
8a03c9ef
ZJS
148 bool newline_separator,
149 int error,
150 const char *format,
ba360bb0 151 va_list ap) _printf_(6, 0);
8a03c9ef 152
915b1d01
LP
153int log_struct_iovec_internal(
154 int level,
155 int error,
156 const char *file,
157 int line,
158 const char *func,
dccca82b 159 const struct iovec *input_iovec,
915b1d01
LP
160 size_t n_input_iovec);
161
2149e37c
LP
162/* This modifies the buffer passed! */
163int log_dump_internal(
877d54e9 164 int level,
086891e5 165 int error,
79008bdd 166 const char *file,
877d54e9
LP
167 int line,
168 const char *func,
169 char *buffer);
170
086891e5 171/* Logging for various assertions */
848e863a 172_noreturn_ void log_assert_failed_realm(
ff524019 173 LogRealm realm,
877d54e9
LP
174 const char *text,
175 const char *file,
176 int line,
177 const char *func);
ff524019
ZJS
178#define log_assert_failed(text, ...) \
179 log_assert_failed_realm(LOG_REALM, (text), __VA_ARGS__)
877d54e9 180
848e863a 181_noreturn_ void log_assert_failed_unreachable_realm(
ff524019 182 LogRealm realm,
877d54e9
LP
183 const char *text,
184 const char *file,
185 int line,
186 const char *func);
ff524019
ZJS
187#define log_assert_failed_unreachable(text, ...) \
188 log_assert_failed_unreachable_realm(LOG_REALM, (text), __VA_ARGS__)
80514f9c 189
ff524019
ZJS
190void log_assert_failed_return_realm(
191 LogRealm realm,
80514f9c
LP
192 const char *text,
193 const char *file,
194 int line,
195 const char *func);
ff524019
ZJS
196#define log_assert_failed_return(text, ...) \
197 log_assert_failed_return_realm(LOG_REALM, (text), __VA_ARGS__)
2149e37c 198
93484b47
ZJS
199#define log_dispatch(level, error, buffer) \
200 log_dispatch_internal(level, error, __FILE__, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
201
086891e5 202/* Logging with level */
ff524019 203#define log_full_errno_realm(realm, level, error, ...) \
1cfa9a4c 204 ({ \
c1e83af8 205 int _level = (level), _e = (error), _realm = (realm); \
baaa35ad 206 (log_get_max_level_realm(_realm) >= LOG_PRI(_level)) \
c1e83af8 207 ? log_internal_realm(LOG_REALM_PLUS_LEVEL(_realm, _level), _e, \
ff524019 208 __FILE__, __LINE__, __func__, __VA_ARGS__) \
baaa35ad 209 : -ERRNO_VALUE(_e); \
bf371116 210 })
87ff6b1c 211
ff524019
ZJS
212#define log_full_errno(level, error, ...) \
213 log_full_errno_realm(LOG_REALM, (level), (error), __VA_ARGS__)
214
215#define log_full(level, ...) log_full_errno((level), 0, __VA_ARGS__)
086891e5 216
dccca82b
LP
217int log_emergency_level(void);
218
086891e5 219/* Normal logging */
4104970e
ZJS
220#define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__)
221#define log_info(...) log_full(LOG_INFO, __VA_ARGS__)
222#define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__)
223#define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
224#define log_error(...) log_full(LOG_ERR, __VA_ARGS__)
dccca82b 225#define log_emergency(...) log_full(log_emergency_level(), __VA_ARGS__)
5899f3b7 226
086891e5
LP
227/* Logging triggered by an errno-like error */
228#define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__)
229#define log_info_errno(error, ...) log_full_errno(LOG_INFO, error, __VA_ARGS__)
230#define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__)
231#define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__)
232#define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__)
dccca82b 233#define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__)
086891e5 234
cb41ff29
ZJS
235#ifdef LOG_TRACE
236# define log_trace(...) log_debug(__VA_ARGS__)
237#else
9ed794a3 238# define log_trace(...) do {} while (0)
cb41ff29
ZJS
239#endif
240
086891e5 241/* Structured logging */
ff524019
ZJS
242#define log_struct_errno(level, error, ...) \
243 log_struct_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
a1230ff9 244 error, __FILE__, __LINE__, __func__, __VA_ARGS__, NULL)
ff524019 245#define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__)
0d0f0c50 246
915b1d01
LP
247#define log_struct_iovec_errno(level, error, iovec, n_iovec) \
248 log_struct_iovec_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
249 error, __FILE__, __LINE__, __func__, iovec, n_iovec)
250#define log_struct_iovec(level, iovec, n_iovec) log_struct_iovec_errno(level, 0, iovec, n_iovec)
251
2149e37c 252/* This modifies the buffer passed! */
ff524019
ZJS
253#define log_dump(level, buffer) \
254 log_dump_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
255 0, __FILE__, __LINE__, __func__, buffer)
086891e5 256
ff524019 257#define log_oom() log_oom_internal(LOG_REALM, __FILE__, __LINE__, __func__)
2149e37c 258
44a6b1b6 259bool log_on_console(void) _pure_;
81270860 260
44a6b1b6
ZJS
261const char *log_target_to_string(LogTarget target) _const_;
262LogTarget log_target_from_string(const char *s) _pure_;
706911fb 263
2b044526 264/* Helper to prepare various field for structured logging */
e2cc6eca 265#define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
4daf54a8
ZJS
266
267void log_received_signal(int level, const struct signalfd_siginfo *si);
c1dc6153 268
6fdb8de4 269/* If turned on, any requests for a log target involving "syslog" will be implicitly upgraded to the equivalent journal target */
c1dc6153 270void log_set_upgrade_syslog_to_journal(bool b);
7a3be263
LP
271
272/* If turned on, and log_open() is called, we'll not use STDERR_FILENO for logging ever, but rather open /dev/console */
48a601fe 273void log_set_always_reopen_console(bool b);
7a3be263
LP
274
275/* If turned on, we'll open the log stream implicitly if needed on each individual log call. This is normally not
276 * desired as we want to reuse our logging streams. It is useful however */
16e4fd87 277void log_set_open_when_needed(bool b);
158350e8 278
adf47c91
LP
279/* 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
280 * stderr, the console or kmsg */
281void log_set_prohibit_ipc(bool b);
282
17cac366
LP
283int log_dup_console(void);
284
158350e8
LP
285int log_syntax_internal(
286 const char *unit,
287 int level,
288 const char *config_file,
289 unsigned config_line,
290 int error,
291 const char *file,
292 int line,
293 const char *func,
294 const char *format, ...) _printf_(9, 10);
295
d04ce5a9
LP
296int log_syntax_invalid_utf8_internal(
297 const char *unit,
298 int level,
299 const char *config_file,
300 unsigned config_line,
301 const char *file,
302 int line,
303 const char *func,
304 const char *rvalue);
305
158350e8
LP
306#define log_syntax(unit, level, config_file, config_line, error, ...) \
307 ({ \
308 int _level = (level), _e = (error); \
309 (log_get_max_level() >= LOG_PRI(_level)) \
310 ? log_syntax_internal(unit, _level, config_file, config_line, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \
311 : -abs(_e); \
312 })
0e05ee04
LP
313
314#define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \
315 ({ \
316 int _level = (level); \
d04ce5a9
LP
317 (log_get_max_level() >= LOG_PRI(_level)) \
318 ? log_syntax_invalid_utf8_internal(unit, _level, config_file, config_line, __FILE__, __LINE__, __func__, rvalue) \
319 : -EINVAL; \
0e05ee04 320 })
f1d34068
LP
321
322#define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG)
6bf3c61c
LP
323
324void log_setup_service(void);