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