]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/log.h
tree-wide: use -EINVAL for enum invalid values
[thirdparty/systemd.git] / src / basic / log.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c2f1db8f 2#pragma once
5899f3b7 3
17a94911 4#include <stdarg.h>
07630cea 5#include <stdbool.h>
6410074b 6#include <stdlib.h>
07630cea 7#include <syslog.h>
5899f3b7 8
158350e8 9#include "macro.h"
dccca82b
LP
10
11/* Some structures we reference but don't want to pull in headers for */
12struct iovec;
13struct signalfd_siginfo;
5899f3b7 14
16801e90
LP
15typedef enum LogTarget{
16 LOG_TARGET_CONSOLE,
aca83a53 17 LOG_TARGET_CONSOLE_PREFIXED,
16801e90 18 LOG_TARGET_KMSG,
5ba081b0
LP
19 LOG_TARGET_JOURNAL,
20 LOG_TARGET_JOURNAL_OR_KMSG,
843d2643
LP
21 LOG_TARGET_SYSLOG,
22 LOG_TARGET_SYSLOG_OR_KMSG,
27ffec08 23 LOG_TARGET_AUTO, /* console if stderr is not journal, JOURNAL_OR_KMSG otherwise */
9fae33d2 24 LOG_TARGET_NULL,
16801e90 25 _LOG_TARGET_MAX,
2d93c20e 26 _LOG_TARGET_INVALID = -EINVAL,
ff524019
ZJS
27} LogTarget;
28
5dc881a3 29/* Note to readers: << and >> have lower precedence than & and | */
52d86690
ZJS
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)
16801e90 33
9fdee66f
YW
34const char *log_target_to_string(LogTarget target) _const_;
35LogTarget log_target_from_string(const char *s) _pure_;
16801e90 36void log_set_target(LogTarget target);
9fdee66f
YW
37int log_set_target_from_string(const char *e);
38LogTarget log_get_target(void) _pure_;
1c36b8bf 39
9fdee66f
YW
40void log_set_max_level(int level);
41int log_set_max_level_from_string(const char *e);
42int log_get_max_level(void) _pure_;
1c36b8bf 43
3eff4208 44void log_set_facility(int facility);
16801e90 45
bbe63281 46void log_show_color(bool b);
b1e90ec5 47bool log_get_show_color(void) _pure_;
bbe63281 48void log_show_location(bool b);
b1e90ec5 49bool log_get_show_location(void) _pure_;
c5673ed0
DS
50void log_show_time(bool b);
51bool log_get_show_time(void) _pure_;
9ee806d1
LP
52void log_show_tid(bool b);
53bool log_get_show_tid(void) _pure_;
bbe63281
LP
54
55int log_show_color_from_string(const char *e);
56int log_show_location_from_string(const char *e);
c5673ed0 57int log_show_time_from_string(const char *e);
9ee806d1 58int log_show_tid_from_string(const char *e);
bbe63281 59
e3e42fc2
ZJS
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
9fdee66f 62 * for the application itself. */
e3e42fc2 63
ab1a1ba5 64assert_cc(STRLEN(__FILE__) > STRLEN(RELATIVE_SOURCE_PATH) + 1);
f455f863 65#define PROJECT_FILE (&__FILE__[STRLEN(RELATIVE_SOURCE_PATH) + 1])
a0b15b41 66
843d2643 67int log_open(void);
871e5809 68void log_close(void);
4d8a7798 69void log_forget_fds(void);
843d2643 70
9fdee66f 71void log_parse_environment(void);
34f0e866 72
93484b47
ZJS
73int 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
9fdee66f 85int log_internal(
877d54e9 86 int level,
086891e5 87 int error,
79008bdd 88 const char *file,
877d54e9
LP
89 int line,
90 const char *func,
086891e5 91 const char *format, ...) _printf_(6,7);
5899f3b7 92
9fdee66f 93int log_internalv(
877d54e9 94 int level,
086891e5 95 int error,
79008bdd 96 const char *file,
877d54e9
LP
97 int line,
98 const char *func,
99 const char *format,
086891e5 100 va_list ap) _printf_(6,0);
877d54e9 101
f6d6d532
YW
102int 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
79008bdd 115int log_object_internal(
fdf9f9bb 116 int level,
086891e5 117 int error,
79008bdd 118 const char *file,
fdf9f9bb
ZJS
119 int line,
120 const char *func,
79008bdd 121 const char *object_field,
fdf9f9bb 122 const char *object,
4b58153d
LP
123 const char *extra_field,
124 const char *extra,
125 const char *format, ...) _printf_(10,11);
fdf9f9bb 126
877d54e9
LP
127int log_struct_internal(
128 int level,
086891e5 129 int error,
877d54e9
LP
130 const char *file,
131 int line,
132 const char *func,
086891e5 133 const char *format, ...) _printf_(6,0) _sentinel_;
877d54e9
LP
134
135int log_oom_internal(
b3a79158 136 int level,
877d54e9
LP
137 const char *file,
138 int line,
139 const char *func);
185986c6 140
8a03c9ef
ZJS
141int log_format_iovec(
142 struct iovec *iovec,
d3070fbd
LP
143 size_t iovec_len,
144 size_t *n,
8a03c9ef
ZJS
145 bool newline_separator,
146 int error,
147 const char *format,
ba360bb0 148 va_list ap) _printf_(6, 0);
8a03c9ef 149
915b1d01
LP
150int log_struct_iovec_internal(
151 int level,
152 int error,
153 const char *file,
154 int line,
155 const char *func,
dccca82b 156 const struct iovec *input_iovec,
915b1d01
LP
157 size_t n_input_iovec);
158
2149e37c
LP
159/* This modifies the buffer passed! */
160int log_dump_internal(
877d54e9 161 int level,
086891e5 162 int error,
79008bdd 163 const char *file,
877d54e9
LP
164 int line,
165 const char *func,
166 char *buffer);
167
086891e5 168/* Logging for various assertions */
9fdee66f 169_noreturn_ void log_assert_failed(
877d54e9
LP
170 const char *text,
171 const char *file,
172 int line,
173 const char *func);
174
9fdee66f 175_noreturn_ void log_assert_failed_unreachable(
877d54e9
LP
176 const char *text,
177 const char *file,
178 int line,
179 const char *func);
80514f9c 180
9fdee66f 181void log_assert_failed_return(
80514f9c
LP
182 const char *text,
183 const char *file,
184 int line,
185 const char *func);
2149e37c 186
93484b47 187#define log_dispatch(level, error, buffer) \
62c6bbbc 188 log_dispatch_internal(level, error, PROJECT_FILE, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
93484b47 189
086891e5 190/* Logging with level */
5df4f46f 191#define log_full_errno(level, error, ...) \
1cfa9a4c 192 ({ \
5df4f46f
YW
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__) \
baaa35ad 196 : -ERRNO_VALUE(_e); \
bf371116 197 })
87ff6b1c 198
44f0dd62 199#define log_full(level, ...) (void) log_full_errno((level), 0, __VA_ARGS__)
086891e5 200
dccca82b
LP
201int log_emergency_level(void);
202
086891e5 203/* Normal logging */
44f0dd62 204#define log_debug(...) log_full_errno(LOG_DEBUG, 0, __VA_ARGS__)
4104970e
ZJS
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__)
dccca82b 209#define log_emergency(...) log_full(log_emergency_level(), __VA_ARGS__)
5899f3b7 210
086891e5
LP
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__)
dccca82b 217#define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__)
086891e5 218
cb41ff29
ZJS
219#ifdef LOG_TRACE
220# define log_trace(...) log_debug(__VA_ARGS__)
221#else
9ed794a3 222# define log_trace(...) do {} while (0)
cb41ff29
ZJS
223#endif
224
086891e5 225/* Structured logging */
9fdee66f
YW
226#define log_struct_errno(level, error, ...) \
227 log_struct_internal(level, error, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__, NULL)
ff524019 228#define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__)
0d0f0c50 229
915b1d01 230#define log_struct_iovec_errno(level, error, iovec, n_iovec) \
9fdee66f 231 log_struct_iovec_internal(level, error, PROJECT_FILE, __LINE__, __func__, iovec, n_iovec)
915b1d01
LP
232#define log_struct_iovec(level, iovec, n_iovec) log_struct_iovec_errno(level, 0, iovec, n_iovec)
233
2149e37c 234/* This modifies the buffer passed! */
9fdee66f
YW
235#define log_dump(level, buffer) \
236 log_dump_internal(level, 0, PROJECT_FILE, __LINE__, __func__, buffer)
086891e5 237
9fdee66f
YW
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__)
2149e37c 240
44a6b1b6 241bool log_on_console(void) _pure_;
81270860 242
2b044526 243/* Helper to prepare various field for structured logging */
e2cc6eca 244#define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
4daf54a8
ZJS
245
246void log_received_signal(int level, const struct signalfd_siginfo *si);
c1dc6153 247
6fdb8de4 248/* If turned on, any requests for a log target involving "syslog" will be implicitly upgraded to the equivalent journal target */
c1dc6153 249void log_set_upgrade_syslog_to_journal(bool b);
7a3be263
LP
250
251/* If turned on, and log_open() is called, we'll not use STDERR_FILENO for logging ever, but rather open /dev/console */
48a601fe 252void log_set_always_reopen_console(bool b);
7a3be263
LP
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 */
16e4fd87 256void log_set_open_when_needed(bool b);
158350e8 257
adf47c91
LP
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 */
260void log_set_prohibit_ipc(bool b);
261
17cac366
LP
262int log_dup_console(void);
263
158350e8
LP
264int 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
d04ce5a9
LP
275int 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
158350e8
LP
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)) \
62c6bbbc 289 ? log_syntax_internal(unit, _level, config_file, config_line, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
ee96382f 290 : -ERRNO_VALUE(_e); \
158350e8 291 })
0e05ee04
LP
292
293#define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \
294 ({ \
295 int _level = (level); \
d04ce5a9 296 (log_get_max_level() >= LOG_PRI(_level)) \
62c6bbbc 297 ? log_syntax_invalid_utf8_internal(unit, _level, config_file, config_line, PROJECT_FILE, __LINE__, __func__, rvalue) \
d04ce5a9 298 : -EINVAL; \
0e05ee04 299 })
f1d34068
LP
300
301#define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG)
6bf3c61c 302
d2acb93d 303void log_setup(void);