]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/log.h
log: add support for prefixing console log messages with current timestamp
[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 /* Note to readers: << and >> have lower precedence than & and | */
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)
45
46 void log_set_target(LogTarget target);
47 void 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
51 void log_set_facility(int facility);
52
53 int log_set_target_from_string(const char *e);
54 int 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))
57
58 void log_show_color(bool b);
59 bool log_get_show_color(void) _pure_;
60 void log_show_location(bool b);
61 bool log_get_show_location(void) _pure_;
62 void log_show_time(bool b);
63 bool log_get_show_time(void) _pure_;
64
65 int log_show_color_from_string(const char *e);
66 int log_show_location_from_string(const char *e);
67 int log_show_time_from_string(const char *e);
68
69 LogTarget log_get_target(void) _pure_;
70 int log_get_max_level_realm(LogRealm realm) _pure_;
71 #define log_get_max_level() \
72 log_get_max_level_realm(LOG_REALM)
73
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.
77 */
78
79 assert_cc(STRLEN(__FILE__) > STRLEN(RELATIVE_SOURCE_PATH) + 1);
80 #define PROJECT_FILE (__FILE__ + STRLEN(RELATIVE_SOURCE_PATH) + 1)
81
82 int log_open(void);
83 void log_close(void);
84 void log_forget_fds(void);
85
86 void log_parse_environment_realm(LogRealm realm);
87 #define log_parse_environment() \
88 log_parse_environment_realm(LOG_REALM)
89
90 int log_dispatch_internal(
91 int level,
92 int error,
93 const char *file,
94 int line,
95 const char *func,
96 const char *object_field,
97 const char *object,
98 const char *extra,
99 const char *extra_field,
100 char *buffer);
101
102 int log_internal_realm(
103 int level,
104 int error,
105 const char *file,
106 int line,
107 const char *func,
108 const char *format, ...) _printf_(6,7);
109 #define log_internal(level, ...) \
110 log_internal_realm(LOG_REALM_PLUS_LEVEL(LOG_REALM, (level)), __VA_ARGS__)
111
112 int log_internalv_realm(
113 int level,
114 int error,
115 const char *file,
116 int line,
117 const char *func,
118 const char *format,
119 va_list ap) _printf_(6,0);
120 #define log_internalv(level, ...) \
121 log_internalv_realm(LOG_REALM_PLUS_LEVEL(LOG_REALM, (level)), __VA_ARGS__)
122
123 /* Realm is fixed to LOG_REALM_SYSTEMD for those */
124 int log_object_internalv(
125 int level,
126 int error,
127 const char *file,
128 int line,
129 const char *func,
130 const char *object_field,
131 const char *object,
132 const char *extra_field,
133 const char *extra,
134 const char *format,
135 va_list ap) _printf_(10,0);
136
137 int log_object_internal(
138 int level,
139 int error,
140 const char *file,
141 int line,
142 const char *func,
143 const char *object_field,
144 const char *object,
145 const char *extra_field,
146 const char *extra,
147 const char *format, ...) _printf_(10,11);
148
149 int log_struct_internal(
150 int level,
151 int error,
152 const char *file,
153 int line,
154 const char *func,
155 const char *format, ...) _printf_(6,0) _sentinel_;
156
157 int log_oom_internal(
158 LogRealm realm,
159 const char *file,
160 int line,
161 const char *func);
162
163 int log_format_iovec(
164 struct iovec *iovec,
165 size_t iovec_len,
166 size_t *n,
167 bool newline_separator,
168 int error,
169 const char *format,
170 va_list ap) _printf_(6, 0);
171
172 int log_struct_iovec_internal(
173 int level,
174 int error,
175 const char *file,
176 int line,
177 const char *func,
178 const struct iovec *input_iovec,
179 size_t n_input_iovec);
180
181 /* This modifies the buffer passed! */
182 int log_dump_internal(
183 int level,
184 int error,
185 const char *file,
186 int line,
187 const char *func,
188 char *buffer);
189
190 /* Logging for various assertions */
191 _noreturn_ void log_assert_failed_realm(
192 LogRealm realm,
193 const char *text,
194 const char *file,
195 int line,
196 const char *func);
197 #define log_assert_failed(text, ...) \
198 log_assert_failed_realm(LOG_REALM, (text), __VA_ARGS__)
199
200 _noreturn_ void log_assert_failed_unreachable_realm(
201 LogRealm realm,
202 const char *text,
203 const char *file,
204 int line,
205 const char *func);
206 #define log_assert_failed_unreachable(text, ...) \
207 log_assert_failed_unreachable_realm(LOG_REALM, (text), __VA_ARGS__)
208
209 void log_assert_failed_return_realm(
210 LogRealm realm,
211 const char *text,
212 const char *file,
213 int line,
214 const char *func);
215 #define log_assert_failed_return(text, ...) \
216 log_assert_failed_return_realm(LOG_REALM, (text), __VA_ARGS__)
217
218 #define log_dispatch(level, error, buffer) \
219 log_dispatch_internal(level, error, PROJECT_FILE, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
220
221 /* Logging with level */
222 #define log_full_errno_realm(realm, level, error, ...) \
223 ({ \
224 int _level = (level), _e = (error), _realm = (realm); \
225 (log_get_max_level_realm(_realm) >= LOG_PRI(_level)) \
226 ? log_internal_realm(LOG_REALM_PLUS_LEVEL(_realm, _level), _e, \
227 PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
228 : -ERRNO_VALUE(_e); \
229 })
230
231 #define log_full_errno(level, error, ...) \
232 log_full_errno_realm(LOG_REALM, (level), (error), __VA_ARGS__)
233
234 #define log_full(level, ...) log_full_errno((level), 0, __VA_ARGS__)
235
236 int log_emergency_level(void);
237
238 /* Normal logging */
239 #define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__)
240 #define log_info(...) log_full(LOG_INFO, __VA_ARGS__)
241 #define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__)
242 #define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
243 #define log_error(...) log_full(LOG_ERR, __VA_ARGS__)
244 #define log_emergency(...) log_full(log_emergency_level(), __VA_ARGS__)
245
246 /* Logging triggered by an errno-like error */
247 #define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__)
248 #define log_info_errno(error, ...) log_full_errno(LOG_INFO, error, __VA_ARGS__)
249 #define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__)
250 #define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__)
251 #define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__)
252 #define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__)
253
254 #ifdef LOG_TRACE
255 # define log_trace(...) log_debug(__VA_ARGS__)
256 #else
257 # define log_trace(...) do {} while (0)
258 #endif
259
260 /* Structured logging */
261 #define log_struct_errno(level, error, ...) \
262 log_struct_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
263 error, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__, NULL)
264 #define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__)
265
266 #define log_struct_iovec_errno(level, error, iovec, n_iovec) \
267 log_struct_iovec_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
268 error, PROJECT_FILE, __LINE__, __func__, iovec, n_iovec)
269 #define log_struct_iovec(level, iovec, n_iovec) log_struct_iovec_errno(level, 0, iovec, n_iovec)
270
271 /* This modifies the buffer passed! */
272 #define log_dump(level, buffer) \
273 log_dump_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
274 0, PROJECT_FILE, __LINE__, __func__, buffer)
275
276 #define log_oom() log_oom_internal(LOG_REALM, PROJECT_FILE, __LINE__, __func__)
277
278 bool log_on_console(void) _pure_;
279
280 const char *log_target_to_string(LogTarget target) _const_;
281 LogTarget log_target_from_string(const char *s) _pure_;
282
283 /* Helper to prepare various field for structured logging */
284 #define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
285
286 void log_received_signal(int level, const struct signalfd_siginfo *si);
287
288 /* If turned on, any requests for a log target involving "syslog" will be implicitly upgraded to the equivalent journal target */
289 void log_set_upgrade_syslog_to_journal(bool b);
290
291 /* If turned on, and log_open() is called, we'll not use STDERR_FILENO for logging ever, but rather open /dev/console */
292 void log_set_always_reopen_console(bool b);
293
294 /* If turned on, we'll open the log stream implicitly if needed on each individual log call. This is normally not
295 * desired as we want to reuse our logging streams. It is useful however */
296 void log_set_open_when_needed(bool b);
297
298 /* 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
299 * stderr, the console or kmsg */
300 void log_set_prohibit_ipc(bool b);
301
302 int log_dup_console(void);
303
304 int log_syntax_internal(
305 const char *unit,
306 int level,
307 const char *config_file,
308 unsigned config_line,
309 int error,
310 const char *file,
311 int line,
312 const char *func,
313 const char *format, ...) _printf_(9, 10);
314
315 int log_syntax_invalid_utf8_internal(
316 const char *unit,
317 int level,
318 const char *config_file,
319 unsigned config_line,
320 const char *file,
321 int line,
322 const char *func,
323 const char *rvalue);
324
325 #define log_syntax(unit, level, config_file, config_line, error, ...) \
326 ({ \
327 int _level = (level), _e = (error); \
328 (log_get_max_level() >= LOG_PRI(_level)) \
329 ? log_syntax_internal(unit, _level, config_file, config_line, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
330 : -ERRNO_VALUE(_e); \
331 })
332
333 #define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \
334 ({ \
335 int _level = (level); \
336 (log_get_max_level() >= LOG_PRI(_level)) \
337 ? log_syntax_invalid_utf8_internal(unit, _level, config_file, config_line, PROJECT_FILE, __LINE__, __func__, rvalue) \
338 : -EINVAL; \
339 })
340
341 #define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG)
342
343 void log_setup_service(void);