chiark / gitweb /
basic/log: split max log level into multiple "realms"
[elogind.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 #if 0 /// UNNEEDED by elogind
92 void log_forget_fds(void);
93 #endif // 0
94
95 void log_close_syslog(void);
96 void log_close_journal(void);
97 void log_close_kmsg(void);
98 void log_close_console(void);
99
100 void log_parse_environment_realm(LogRealm realm);
101 #define log_parse_environment() \
102         log_parse_environment_realm(LOG_REALM)
103
104 int log_dispatch_internal(
105                 int level,
106                 int error,
107                 const char *file,
108                 int line,
109                 const char *func,
110                 const char *object_field,
111                 const char *object,
112                 const char *extra,
113                 const char *extra_field,
114                 char *buffer);
115
116 int log_internal_realm(
117                 int level,
118                 int error,
119                 const char *file,
120                 int line,
121                 const char *func,
122                 const char *format, ...) _printf_(6,7);
123 #define log_internal(level, ...) \
124         log_internal_realm(LOG_REALM_PLUS_LEVEL(LOG_REALM, (level)), __VA_ARGS__)
125
126 int log_internalv_realm(
127                 int level,
128                 int error,
129                 const char *file,
130                 int line,
131                 const char *func,
132                 const char *format,
133                 va_list ap) _printf_(6,0);
134 #define log_internalv(level, ...) \
135         log_internalv_realm(LOG_REALM_PLUS_LEVEL(LOG_REALM, (level)), __VA_ARGS__)
136
137 /* Realm is fixed to LOG_REALM_SYSTEMD for those */
138 int log_object_internal(
139                 int level,
140                 int error,
141                 const char *file,
142                 int line,
143                 const char *func,
144                 const char *object_field,
145                 const char *object,
146                 const char *extra_field,
147                 const char *extra,
148                 const char *format, ...) _printf_(10,11);
149
150 int log_object_internalv(
151                 int level,
152                 int error,
153                 const char *file,
154                 int line,
155                 const char *func,
156                 const char *object_field,
157                 const char *object,
158                 const char *extra_field,
159                 const char *extra,
160                 const char *format,
161                 va_list ap) _printf_(10,0);
162
163 int log_struct_internal(
164                 int level,
165                 int error,
166                 const char *file,
167                 int line,
168                 const char *func,
169                 const char *format, ...) _printf_(6,0) _sentinel_;
170
171 int log_oom_internal(
172                 LogRealm realm,
173                 const char *file,
174                 int line,
175                 const char *func);
176
177 int log_format_iovec(
178                 struct iovec *iovec,
179                 unsigned iovec_len,
180                 unsigned *n,
181                 bool newline_separator,
182                 int error,
183                 const char *format,
184                 va_list ap) _printf_(6, 0);
185
186 /* This modifies the buffer passed! */
187 int log_dump_internal(
188                 int level,
189                 int error,
190                 const char *file,
191                 int line,
192                 const char *func,
193                 char *buffer);
194
195 /* Logging for various assertions */
196 noreturn void log_assert_failed_realm(
197                 LogRealm realm,
198                 const char *text,
199                 const char *file,
200                 int line,
201                 const char *func);
202 #define log_assert_failed(text, ...) \
203         log_assert_failed_realm(LOG_REALM, (text), __VA_ARGS__)
204
205 noreturn void log_assert_failed_unreachable_realm(
206                 LogRealm realm,
207                 const char *text,
208                 const char *file,
209                 int line,
210                 const char *func);
211 #define log_assert_failed_unreachable(text, ...) \
212         log_assert_failed_unreachable_realm(LOG_REALM, (text), __VA_ARGS__)
213
214 void log_assert_failed_return_realm(
215                 LogRealm realm,
216                 const char *text,
217                 const char *file,
218                 int line,
219                 const char *func);
220 #define log_assert_failed_return(text, ...) \
221         log_assert_failed_return_realm(LOG_REALM, (text), __VA_ARGS__)
222
223 #define log_dispatch(level, error, buffer)                              \
224         log_dispatch_internal(level, error, __FILE__, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
225
226 /* Logging with level */
227 #define log_full_errno_realm(realm, level, error, ...)                  \
228         ({                                                              \
229                 int _level = (level), _e = (error);                     \
230                 (log_get_max_level_realm((realm)) >= LOG_PRI(_level))   \
231                         ? log_internal_realm(LOG_REALM_PLUS_LEVEL((realm), _level), _e, \
232                                              __FILE__, __LINE__, __func__, __VA_ARGS__) \
233                         : -abs(_e);                                     \
234         })
235
236 #define log_full_errno(level, error, ...)                               \
237         log_full_errno_realm(LOG_REALM, (level), (error), __VA_ARGS__)
238
239 #define log_full(level, ...) log_full_errno((level), 0, __VA_ARGS__)
240
241 /* Normal logging */
242 #define log_debug(...)     log_full(LOG_DEBUG,   __VA_ARGS__)
243 #define log_info(...)      log_full(LOG_INFO,    __VA_ARGS__)
244 #define log_notice(...)    log_full(LOG_NOTICE,  __VA_ARGS__)
245 #define log_warning(...)   log_full(LOG_WARNING, __VA_ARGS__)
246 #define log_error(...)     log_full(LOG_ERR,     __VA_ARGS__)
247 #define log_emergency(...) log_full(getpid() == 1 ? LOG_EMERG : LOG_ERR, __VA_ARGS__)
248
249 /* Logging triggered by an errno-like error */
250 #define log_debug_errno(error, ...)     log_full_errno(LOG_DEBUG,   error, __VA_ARGS__)
251 #define log_info_errno(error, ...)      log_full_errno(LOG_INFO,    error, __VA_ARGS__)
252 #define log_notice_errno(error, ...)    log_full_errno(LOG_NOTICE,  error, __VA_ARGS__)
253 #define log_warning_errno(error, ...)   log_full_errno(LOG_WARNING, error, __VA_ARGS__)
254 #define log_error_errno(error, ...)     log_full_errno(LOG_ERR,     error, __VA_ARGS__)
255 #define log_emergency_errno(error, ...) log_full_errno(getpid() == 1 ? LOG_EMERG : LOG_ERR, error, __VA_ARGS__)
256
257 #ifdef LOG_TRACE
258 #  define log_trace(...) log_debug(__VA_ARGS__)
259 #else
260 #  define log_trace(...) do {} while (0)
261 #endif
262
263 #ifdef ENABLE_DEBUG_ELOGIND
264 #  define log_debug_elogind(...) log_debug(__VA_ARGS__);usleep(25*USEC_PER_MSEC)
265 #else
266 #  define log_debug_elogind(...) do {} while (0)
267 #endif // ENABLE_DEBUG_ELOGIND
268 /* Structured logging */
269 #define log_struct_errno(level, error, ...) \
270         log_struct_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
271                             error, __FILE__, __LINE__, __func__, __VA_ARGS__)
272 #define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__)
273
274 /* This modifies the buffer passed! */
275 #define log_dump(level, buffer) \
276         log_dump_internal(LOG_REALM_PLUS_LEVEL(LOG_REALM, level), \
277                           0, __FILE__, __LINE__, __func__, buffer)
278
279 #define log_oom() log_oom_internal(LOG_REALM, __FILE__, __LINE__, __func__)
280
281 bool log_on_console(void) _pure_;
282
283 const char *log_target_to_string(LogTarget target) _const_;
284 LogTarget log_target_from_string(const char *s) _pure_;
285
286 /* Helper to prepare various field for structured logging */
287 #define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
288
289 #if 0 /// UNNEEDED by elogind
290 void log_received_signal(int level, const struct signalfd_siginfo *si);
291
292 void log_set_upgrade_syslog_to_journal(bool b);
293 void log_set_always_reopen_console(bool b);
294 #endif // 0
295
296 int log_syntax_internal(
297                 const char *unit,
298                 int level,
299                 const char *config_file,
300                 unsigned config_line,
301                 int error,
302                 const char *file,
303                 int line,
304                 const char *func,
305                 const char *format, ...) _printf_(9, 10);
306
307 #define log_syntax(unit, level, config_file, config_line, error, ...)   \
308         ({                                                              \
309                 int _level = (level), _e = (error);                     \
310                 (log_get_max_level() >= LOG_PRI(_level))                \
311                         ? log_syntax_internal(unit, _level, config_file, config_line, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \
312                         : -abs(_e);                                     \
313         })
314
315 #define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \
316         ({                                                              \
317                 int _level = (level);                                   \
318                 if (log_get_max_level() >= LOG_PRI(_level)) {           \
319                         _cleanup_free_ char *_p = NULL;                 \
320                         _p = utf8_escape_invalid(rvalue);               \
321                         log_syntax_internal(unit, _level, config_file, config_line, 0, __FILE__, __LINE__, __func__, \
322                                             "String is not UTF-8 clean, ignoring assignment: %s", strna(_p)); \
323                 }                                                       \
324         })