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