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