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