]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/log.h
Merge pull request #5600 from fbuihuu/make-logind-restartable
[thirdparty/systemd.git] / src / basic / log.h
1 #pragma once
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <sys/signalfd.h>
27 #include <sys/socket.h>
28 #include <syslog.h>
29
30 #include "sd-id128.h"
31
32 #include "macro.h"
33
34 typedef enum LogRealm {
35 LOG_REALM_SYSTEMD,
36 LOG_REALM_UDEV,
37 _LOG_REALM_MAX,
38 } LogRealm;
39
40 #ifndef LOG_REALM
41 # define LOG_REALM LOG_REALM_SYSTEMD
42 #endif
43
44 typedef enum LogTarget{
45 LOG_TARGET_CONSOLE,
46 LOG_TARGET_CONSOLE_PREFIXED,
47 LOG_TARGET_KMSG,
48 LOG_TARGET_JOURNAL,
49 LOG_TARGET_JOURNAL_OR_KMSG,
50 LOG_TARGET_SYSLOG,
51 LOG_TARGET_SYSLOG_OR_KMSG,
52 LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */
53 LOG_TARGET_SAFE, /* console if stderr is tty, KMSG otherwise */
54 LOG_TARGET_NULL,
55 _LOG_TARGET_MAX,
56 _LOG_TARGET_INVALID = -1
57 } LogTarget;
58
59 #define LOG_REALM_PLUS_LEVEL(realm, level) \
60 ((realm) << 10 | (level))
61 #define LOG_REALM_REMOVE_LEVEL(realm_level) \
62 ((realm_level >> 10))
63
64 void log_set_target(LogTarget target);
65 void log_set_max_level_realm(LogRealm realm, int level);
66 #define log_set_max_level(level) \
67 log_set_max_level_realm(LOG_REALM, (level))
68
69 void log_set_facility(int facility);
70
71 int log_set_target_from_string(const char *e);
72 int log_set_max_level_from_string_realm(LogRealm realm, const char *e);
73 #define log_set_max_level_from_string(e) \
74 log_set_max_level_from_string_realm(LOG_REALM, (e))
75
76 void log_show_color(bool b);
77 bool log_get_show_color(void) _pure_;
78 void log_show_location(bool b);
79 bool log_get_show_location(void) _pure_;
80
81 int log_show_color_from_string(const char *e);
82 int log_show_location_from_string(const char *e);
83
84 LogTarget log_get_target(void) _pure_;
85 int log_get_max_level_realm(LogRealm realm) _pure_;
86 #define log_get_max_level() \
87 log_get_max_level_realm(LOG_REALM)
88
89 int log_open(void);
90 void log_close(void);
91 void log_forget_fds(void);
92
93 void log_close_syslog(void);
94 void log_close_journal(void);
95 void log_close_kmsg(void);
96 void log_close_console(void);
97
98 void log_parse_environment_realm(LogRealm realm);
99 #define log_parse_environment() \
100 log_parse_environment_realm(LOG_REALM)
101
102 int log_dispatch_internal(
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,
111 const char *extra_field,
112 char *buffer);
113
114 int log_internal_realm(
115 int level,
116 int error,
117 const char *file,
118 int line,
119 const char *func,
120 const char *format, ...) _printf_(6,7);
121 #define log_internal(level, ...) \
122 log_internal_realm(LOG_REALM_PLUS_LEVEL(LOG_REALM, (level)), __VA_ARGS__)
123
124 int log_internalv_realm(
125 int level,
126 int error,
127 const char *file,
128 int line,
129 const char *func,
130 const char *format,
131 va_list ap) _printf_(6,0);
132 #define log_internalv(level, ...) \
133 log_internalv_realm(LOG_REALM_PLUS_LEVEL(LOG_REALM, (level)), __VA_ARGS__)
134
135 /* Realm is fixed to LOG_REALM_SYSTEMD for those */
136 int log_object_internal(
137 int level,
138 int error,
139 const char *file,
140 int line,
141 const char *func,
142 const char *object_field,
143 const char *object,
144 const char *extra_field,
145 const char *extra,
146 const char *format, ...) _printf_(10,11);
147
148 int log_object_internalv(
149 int level,
150 int error,
151 const char *file,
152 int line,
153 const char *func,
154 const char *object_field,
155 const char *object,
156 const char *extra_field,
157 const char *extra,
158 const char *format,
159 va_list ap) _printf_(10,0);
160
161 int log_struct_internal(
162 int level,
163 int error,
164 const char *file,
165 int line,
166 const char *func,
167 const char *format, ...) _printf_(6,0) _sentinel_;
168
169 int log_oom_internal(
170 LogRealm realm,
171 const char *file,
172 int line,
173 const char *func);
174
175 int log_format_iovec(
176 struct iovec *iovec,
177 unsigned iovec_len,
178 unsigned *n,
179 bool newline_separator,
180 int error,
181 const char *format,
182 va_list ap) _printf_(6, 0);
183
184 /* This modifies the buffer passed! */
185 int log_dump_internal(
186 int level,
187 int error,
188 const char *file,
189 int line,
190 const char *func,
191 char *buffer);
192
193 /* Logging for various assertions */
194 noreturn void log_assert_failed_realm(
195 LogRealm realm,
196 const char *text,
197 const char *file,
198 int line,
199 const char *func);
200 #define log_assert_failed(text, ...) \
201 log_assert_failed_realm(LOG_REALM, (text), __VA_ARGS__)
202
203 noreturn void log_assert_failed_unreachable_realm(
204 LogRealm realm,
205 const char *text,
206 const char *file,
207 int line,
208 const char *func);
209 #define log_assert_failed_unreachable(text, ...) \
210 log_assert_failed_unreachable_realm(LOG_REALM, (text), __VA_ARGS__)
211
212 void log_assert_failed_return_realm(
213 LogRealm realm,
214 const char *text,
215 const char *file,
216 int line,
217 const char *func);
218 #define log_assert_failed_return(text, ...) \
219 log_assert_failed_return_realm(LOG_REALM, (text), __VA_ARGS__)
220
221 #define log_dispatch(level, error, buffer) \
222 log_dispatch_internal(level, error, __FILE__, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
223
224 /* Logging with level */
225 #define log_full_errno_realm(realm, level, error, ...) \
226 ({ \
227 int _level = (level), _e = (error); \
228 (log_get_max_level_realm((realm)) >= LOG_PRI(_level)) \
229 ? log_internal_realm(LOG_REALM_PLUS_LEVEL((realm), _level), _e, \
230 __FILE__, __LINE__, __func__, __VA_ARGS__) \
231 : -abs(_e); \
232 })
233
234 #define log_full_errno(level, error, ...) \
235 log_full_errno_realm(LOG_REALM, (level), (error), __VA_ARGS__)
236
237 #define log_full(level, ...) log_full_errno((level), 0, __VA_ARGS__)
238
239 /* Normal logging */
240 #define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__)
241 #define log_info(...) log_full(LOG_INFO, __VA_ARGS__)
242 #define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__)
243 #define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
244 #define log_error(...) log_full(LOG_ERR, __VA_ARGS__)
245 #define log_emergency(...) log_full(getpid() == 1 ? LOG_EMERG : LOG_ERR, __VA_ARGS__)
246
247 /* Logging triggered by an errno-like error */
248 #define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__)
249 #define log_info_errno(error, ...) log_full_errno(LOG_INFO, error, __VA_ARGS__)
250 #define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__)
251 #define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__)
252 #define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__)
253 #define log_emergency_errno(error, ...) log_full_errno(getpid() == 1 ? LOG_EMERG : LOG_ERR, error, __VA_ARGS__)
254
255 #ifdef LOG_TRACE
256 # define log_trace(...) log_debug(__VA_ARGS__)
257 #else
258 # define log_trace(...) do {} while (0)
259 #endif
260
261 /* Structured logging */
262 #define log_struct_errno(level, error, ...) \
263 log_struct_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
264 error, __FILE__, __LINE__, __func__, __VA_ARGS__)
265 #define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__)
266
267 /* This modifies the buffer passed! */
268 #define log_dump(level, buffer) \
269 log_dump_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
270 0, __FILE__, __LINE__, __func__, buffer)
271
272 #define log_oom() log_oom_internal(LOG_REALM, __FILE__, __LINE__, __func__)
273
274 bool log_on_console(void) _pure_;
275
276 const char *log_target_to_string(LogTarget target) _const_;
277 LogTarget log_target_from_string(const char *s) _pure_;
278
279 /* Helper to prepare various field for structured logging */
280 #define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
281
282 void log_received_signal(int level, const struct signalfd_siginfo *si);
283
284 void log_set_upgrade_syslog_to_journal(bool b);
285 void log_set_always_reopen_console(bool b);
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 #define log_syntax(unit, level, config_file, config_line, error, ...) \
299 ({ \
300 int _level = (level), _e = (error); \
301 (log_get_max_level() >= LOG_PRI(_level)) \
302 ? log_syntax_internal(unit, _level, config_file, config_line, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \
303 : -abs(_e); \
304 })
305
306 #define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \
307 ({ \
308 int _level = (level); \
309 if (log_get_max_level() >= LOG_PRI(_level)) { \
310 _cleanup_free_ char *_p = NULL; \
311 _p = utf8_escape_invalid(rvalue); \
312 log_syntax_internal(unit, _level, config_file, config_line, 0, __FILE__, __LINE__, __func__, \
313 "String is not UTF-8 clean, ignoring assignment: %s", strna(_p)); \
314 } \
315 })