chiark / gitweb /
75ecd84939341c88dfb4b11c4ee874eb8da50e45
[elogind.git] / src / basic / log.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8 ***/
9
10 #include <stdarg.h>
11 #include <stdbool.h>
12 #include <stdlib.h>
13 #include <syslog.h>
14
15 #include "macro.h"
16
17 /* Some structures we reference but don't want to pull in headers for */
18 struct iovec;
19 struct signalfd_siginfo;
20
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
31 typedef enum LogTarget{
32         LOG_TARGET_CONSOLE,
33         LOG_TARGET_CONSOLE_PREFIXED,
34         LOG_TARGET_KMSG,
35         LOG_TARGET_JOURNAL,
36         LOG_TARGET_JOURNAL_OR_KMSG,
37         LOG_TARGET_SYSLOG,
38         LOG_TARGET_SYSLOG_OR_KMSG,
39         LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */
40         LOG_TARGET_NULL,
41         _LOG_TARGET_MAX,
42         _LOG_TARGET_INVALID = -1
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))
49
50 void log_set_target(LogTarget target);
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
55 void log_set_facility(int facility);
56
57 int log_set_target_from_string(const char *e);
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))
61
62 void log_show_color(bool b);
63 bool log_get_show_color(void) _pure_;
64 void log_show_location(bool b);
65 bool log_get_show_location(void) _pure_;
66
67 int log_show_color_from_string(const char *e);
68 int log_show_location_from_string(const char *e);
69
70 LogTarget log_get_target(void) _pure_;
71 int log_get_max_level_realm(LogRealm realm) _pure_;
72 #define log_get_max_level()                     \
73         log_get_max_level_realm(LOG_REALM)
74
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
80 int log_open(void);
81 void log_close(void);
82 #if 0 /// UNNEEDED by elogind
83 void log_forget_fds(void);
84 #endif // 0
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_internal(
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, ...) _printf_(10,11);
135
136 int log_struct_internal(
137                 int level,
138                 int error,
139                 const char *file,
140                 int line,
141                 const char *func,
142                 const char *format, ...) _printf_(6,0) _sentinel_;
143
144 int log_oom_internal(
145                 LogRealm realm,
146                 const char *file,
147                 int line,
148                 const char *func);
149
150 int log_format_iovec(
151                 struct iovec *iovec,
152                 size_t iovec_len,
153                 size_t *n,
154                 bool newline_separator,
155                 int error,
156                 const char *format,
157                 va_list ap) _printf_(6, 0);
158
159 int log_struct_iovec_internal(
160                 int level,
161                 int error,
162                 const char *file,
163                 int line,
164                 const char *func,
165                 const struct iovec *input_iovec,
166                 size_t n_input_iovec);
167
168 /* This modifies the buffer passed! */
169 int log_dump_internal(
170                 int level,
171                 int error,
172                 const char *file,
173                 int line,
174                 const char *func,
175                 char *buffer);
176
177 /* Logging for various assertions */
178 _noreturn_ void log_assert_failed_realm(
179                 LogRealm realm,
180                 const char *text,
181                 const char *file,
182                 int line,
183                 const char *func);
184 #define log_assert_failed(text, ...) \
185         log_assert_failed_realm(LOG_REALM, (text), __VA_ARGS__)
186
187 _noreturn_ void log_assert_failed_unreachable_realm(
188                 LogRealm realm,
189                 const char *text,
190                 const char *file,
191                 int line,
192                 const char *func);
193 #define log_assert_failed_unreachable(text, ...) \
194         log_assert_failed_unreachable_realm(LOG_REALM, (text), __VA_ARGS__)
195
196 void log_assert_failed_return_realm(
197                 LogRealm realm,
198                 const char *text,
199                 const char *file,
200                 int line,
201                 const char *func);
202 #define log_assert_failed_return(text, ...) \
203         log_assert_failed_return_realm(LOG_REALM, (text), __VA_ARGS__)
204
205 #define log_dispatch(level, error, buffer)                              \
206         log_dispatch_internal(level, error, __FILE__, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
207
208 /* Logging with level */
209 #define log_full_errno_realm(realm, level, error, ...)                  \
210         ({                                                              \
211                 int _level = (level), _e = (error), _realm = (realm);   \
212                 (log_get_max_level_realm(_realm) >= LOG_PRI(_level))   \
213                         ? log_internal_realm(LOG_REALM_PLUS_LEVEL(_realm, _level), _e, \
214                                              __FILE__, __LINE__, __func__, __VA_ARGS__) \
215                         : -abs(_e);                                     \
216         })
217
218 #define log_full_errno(level, error, ...)                               \
219         log_full_errno_realm(LOG_REALM, (level), (error), __VA_ARGS__)
220
221 #define log_full(level, ...) log_full_errno((level), 0, __VA_ARGS__)
222
223 int log_emergency_level(void);
224
225 /* Normal logging */
226 #define log_debug(...)     log_full(LOG_DEBUG,   __VA_ARGS__)
227 #define log_info(...)      log_full(LOG_INFO,    __VA_ARGS__)
228 #define log_notice(...)    log_full(LOG_NOTICE,  __VA_ARGS__)
229 #define log_warning(...)   log_full(LOG_WARNING, __VA_ARGS__)
230 #define log_error(...)     log_full(LOG_ERR,     __VA_ARGS__)
231 #define log_emergency(...) log_full(log_emergency_level(), __VA_ARGS__)
232
233 /* Logging triggered by an errno-like error */
234 #define log_debug_errno(error, ...)     log_full_errno(LOG_DEBUG,   error, __VA_ARGS__)
235 #define log_info_errno(error, ...)      log_full_errno(LOG_INFO,    error, __VA_ARGS__)
236 #define log_notice_errno(error, ...)    log_full_errno(LOG_NOTICE,  error, __VA_ARGS__)
237 #define log_warning_errno(error, ...)   log_full_errno(LOG_WARNING, error, __VA_ARGS__)
238 #define log_error_errno(error, ...)     log_full_errno(LOG_ERR,     error, __VA_ARGS__)
239 #define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__)
240
241 #ifdef LOG_TRACE
242 #  define log_trace(...) log_debug(__VA_ARGS__)
243 #else
244 #  define log_trace(...) do {} while (0)
245 #endif
246
247 #if ENABLE_DEBUG_ELOGIND
248 #  define log_debug_elogind(...) log_debug(__VA_ARGS__);usleep(25*USEC_PER_MSEC)
249 #else
250 #  define log_debug_elogind(...) do {} while (0)
251 #endif // ENABLE_DEBUG_ELOGIND
252 /* Structured logging */
253 #define log_struct_errno(level, error, ...) \
254         log_struct_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
255                             error, __FILE__, __LINE__, __func__, __VA_ARGS__)
256                             error, __FILE__, __LINE__, __func__, __VA_ARGS__, NULL)
257 #define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__)
258
259 #define log_struct_iovec_errno(level, error, iovec, n_iovec)            \
260         log_struct_iovec_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
261                                   error, __FILE__, __LINE__, __func__, iovec, n_iovec)
262 #define log_struct_iovec(level, iovec, n_iovec) log_struct_iovec_errno(level, 0, iovec, n_iovec)
263
264 /* This modifies the buffer passed! */
265 #define log_dump(level, buffer) \
266         log_dump_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
267                           0, __FILE__, __LINE__, __func__, buffer)
268
269 #define log_oom() log_oom_internal(LOG_REALM, __FILE__, __LINE__, __func__)
270
271 bool log_on_console(void) _pure_;
272
273 const char *log_target_to_string(LogTarget target) _const_;
274 LogTarget log_target_from_string(const char *s) _pure_;
275
276 /* Helper to prepare various field for structured logging */
277 #define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
278
279 #if 0 /// UNNEEDED by elogind
280 void log_received_signal(int level, const struct signalfd_siginfo *si);
281
282 /* If turned on, any requests for a log target involving "syslog" will be implicitly upgraded to the equivalent journal target */
283 void log_set_upgrade_syslog_to_journal(bool b);
284
285 /* If turned on, and log_open() is called, we'll not use STDERR_FILENO for logging ever, but rather open /dev/console */
286 void log_set_always_reopen_console(bool b);
287 #endif // 0
288
289 /* If turned on, we'll open the log stream implicitly if needed on each individual log call. This is normally not
290  * desired as we want to reuse our logging streams. It is useful however  */
291 void log_set_open_when_needed(bool b);
292
293 /* 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
294  * stderr, the console or kmsg */
295 void log_set_prohibit_ipc(bool b);
296
297 int log_dup_console(void);
298
299 int log_syntax_internal(
300                 const char *unit,
301                 int level,
302                 const char *config_file,
303                 unsigned config_line,
304                 int error,
305                 const char *file,
306                 int line,
307                 const char *func,
308                 const char *format, ...) _printf_(9, 10);
309
310 int log_syntax_invalid_utf8_internal(
311                 const char *unit,
312                 int level,
313                 const char *config_file,
314                 unsigned config_line,
315                 const char *file,
316                 int line,
317                 const char *func,
318                 const char *rvalue);
319
320 #define log_syntax(unit, level, config_file, config_line, error, ...)   \
321         ({                                                              \
322                 int _level = (level), _e = (error);                     \
323                 (log_get_max_level() >= LOG_PRI(_level))                \
324                         ? log_syntax_internal(unit, _level, config_file, config_line, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \
325                         : -abs(_e);                                     \
326         })
327
328 #define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \
329         ({                                                              \
330                 int _level = (level);                                   \
331                 (log_get_max_level() >= LOG_PRI(_level))                \
332                         ? log_syntax_invalid_utf8_internal(unit, _level, config_file, config_line, __FILE__, __LINE__, __func__, rvalue) \
333                         : -EINVAL;                                      \
334         })
335
336 #define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG)