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 <string.h>
8 #include <syslog.h>
9 
10 #include "macro.h"
11 
12 /* Some structures we reference but don't want to pull in headers for */
13 struct iovec;
14 struct signalfd_siginfo;
15 
16 typedef enum LogTarget{
17         LOG_TARGET_CONSOLE,
18         LOG_TARGET_CONSOLE_PREFIXED,
19         LOG_TARGET_KMSG,
20         LOG_TARGET_JOURNAL,
21         LOG_TARGET_JOURNAL_OR_KMSG,
22         LOG_TARGET_SYSLOG,
23         LOG_TARGET_SYSLOG_OR_KMSG,
24         LOG_TARGET_AUTO, /* console if stderr is not journal, JOURNAL_OR_KMSG otherwise */
25         LOG_TARGET_NULL,
26         _LOG_TARGET_MAX,
27         _LOG_TARGET_INVALID = -EINVAL,
28 } LogTarget;
29 
30 /* This log level disables logging completely. It can only be passed to log_set_max_level() and cannot be
31  * used a regular log level. */
32 #define LOG_NULL (LOG_EMERG - 1)
33 
34 /* Note to readers: << and >> have lower precedence (are evaluated earlier) than & and | */
35 #define SYNTHETIC_ERRNO(num)                (1 << 30 | (num))
36 #define IS_SYNTHETIC_ERRNO(val)             ((val) >> 30 & 1)
37 #define ERRNO_VALUE(val)                    (abs(val) & ~(1 << 30))
38 
39 /* The callback function to be invoked when syntax warnings are seen
40  * in the unit files. */
41 typedef void (*log_syntax_callback_t)(const char *unit, int level, void *userdata);
42 void set_log_syntax_callback(log_syntax_callback_t cb, void *userdata);
43 
clear_log_syntax_callback(dummy_t * dummy)44 static inline void clear_log_syntax_callback(dummy_t *dummy) {
45           set_log_syntax_callback(/* cb= */ NULL, /* userdata= */ NULL);
46 }
47 
48 const char *log_target_to_string(LogTarget target) _const_;
49 LogTarget log_target_from_string(const char *s) _pure_;
50 void log_set_target(LogTarget target);
51 int log_set_target_from_string(const char *e);
52 LogTarget log_get_target(void) _pure_;
53 
54 void log_set_max_level(int level);
55 int log_set_max_level_from_string(const char *e);
56 int log_get_max_level(void) _pure_;
57 
58 void log_set_facility(int facility);
59 
60 void log_show_color(bool b);
61 bool log_get_show_color(void) _pure_;
62 void log_show_location(bool b);
63 bool log_get_show_location(void) _pure_;
64 void log_show_time(bool b);
65 bool log_get_show_time(void) _pure_;
66 void log_show_tid(bool b);
67 bool log_get_show_tid(void) _pure_;
68 
69 int log_show_color_from_string(const char *e);
70 int log_show_location_from_string(const char *e);
71 int log_show_time_from_string(const char *e);
72 int log_show_tid_from_string(const char *e);
73 
74 /* Functions below that open and close logs or configure logging based on the
75  * environment should not be called from library code — this is always a job
76  * for the application itself. */
77 
78 assert_cc(STRLEN(__FILE__) > STRLEN(RELATIVE_SOURCE_PATH) + 1);
79 #define PROJECT_FILE (&__FILE__[STRLEN(RELATIVE_SOURCE_PATH) + 1])
80 
81 int log_open(void);
82 void log_close(void);
83 void log_forget_fds(void);
84 
85 void log_parse_environment_variables(void);
86 void log_parse_environment(void);
87 
88 int log_dispatch_internal(
89                 int level,
90                 int error,
91                 const char *file,
92                 int line,
93                 const char *func,
94                 const char *object_field,
95                 const char *object,
96                 const char *extra,
97                 const char *extra_field,
98                 char *buffer);
99 
100 int log_internal(
101                 int level,
102                 int error,
103                 const char *file,
104                 int line,
105                 const char *func,
106                 const char *format, ...) _printf_(6,7);
107 
108 int log_internalv(
109                 int level,
110                 int error,
111                 const char *file,
112                 int line,
113                 const char *func,
114                 const char *format,
115                 va_list ap) _printf_(6,0);
116 
117 int log_object_internalv(
118                 int level,
119                 int error,
120                 const char *file,
121                 int line,
122                 const char *func,
123                 const char *object_field,
124                 const char *object,
125                 const char *extra_field,
126                 const char *extra,
127                 const char *format,
128                 va_list ap) _printf_(10,0);
129 
130 int log_object_internal(
131                 int level,
132                 int error,
133                 const char *file,
134                 int line,
135                 const char *func,
136                 const char *object_field,
137                 const char *object,
138                 const char *extra_field,
139                 const char *extra,
140                 const char *format, ...) _printf_(10,11);
141 
142 int log_struct_internal(
143                 int level,
144                 int error,
145                 const char *file,
146                 int line,
147                 const char *func,
148                 const char *format, ...) _printf_(6,0) _sentinel_;
149 
150 int log_oom_internal(
151                 int level,
152                 const char *file,
153                 int line,
154                 const char *func);
155 
156 int log_format_iovec(
157                 struct iovec *iovec,
158                 size_t iovec_len,
159                 size_t *n,
160                 bool newline_separator,
161                 int error,
162                 const char *format,
163                 va_list ap) _printf_(6, 0);
164 
165 int log_struct_iovec_internal(
166                 int level,
167                 int error,
168                 const char *file,
169                 int line,
170                 const char *func,
171                 const struct iovec *input_iovec,
172                 size_t n_input_iovec);
173 
174 /* This modifies the buffer passed! */
175 int log_dump_internal(
176                 int level,
177                 int error,
178                 const char *file,
179                 int line,
180                 const char *func,
181                 char *buffer);
182 
183 /* Logging for various assertions */
184 _noreturn_ void log_assert_failed(
185                 const char *text,
186                 const char *file,
187                 int line,
188                 const char *func);
189 
190 _noreturn_ void log_assert_failed_unreachable(
191                 const char *file,
192                 int line,
193                 const char *func);
194 
195 void log_assert_failed_return(
196                 const char *text,
197                 const char *file,
198                 int line,
199                 const char *func);
200 
201 #define log_dispatch(level, error, buffer)                              \
202         log_dispatch_internal(level, error, PROJECT_FILE, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
203 
204 /* Logging with level */
205 #define log_full_errno_zerook(level, error, ...)                        \
206         ({                                                              \
207                 int _level = (level), _e = (error);                     \
208                 _e = (log_get_max_level() >= LOG_PRI(_level))           \
209                         ? log_internal(_level, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
210                         : -ERRNO_VALUE(_e);                             \
211                 _e < 0 ? _e : -ESTRPIPE;                                \
212         })
213 
214 #if BUILD_MODE_DEVELOPER && !defined(TEST_CODE)
215 #  define ASSERT_NON_ZERO(x) assert((x) != 0)
216 #else
217 #  define ASSERT_NON_ZERO(x)
218 #endif
219 
220 #define log_full_errno(level, error, ...)                               \
221         ({                                                              \
222                 int _error = (error);                                   \
223                 ASSERT_NON_ZERO(_error);                                \
224                 log_full_errno_zerook(level, _error, __VA_ARGS__);      \
225         })
226 
227 #define log_full(level, fmt, ...)                                      \
228         ({                                                             \
229                 if (BUILD_MODE_DEVELOPER)                              \
230                         assert(!strstr(fmt, "%m"));                    \
231                 (void) log_full_errno_zerook(level, 0, fmt, ##__VA_ARGS__); \
232         })
233 
234 int log_emergency_level(void);
235 
236 /* Normal logging */
237 #define log_debug(...)     log_full(LOG_DEBUG,   __VA_ARGS__)
238 #define log_info(...)      log_full(LOG_INFO,    __VA_ARGS__)
239 #define log_notice(...)    log_full(LOG_NOTICE,  __VA_ARGS__)
240 #define log_warning(...)   log_full(LOG_WARNING, __VA_ARGS__)
241 #define log_error(...)     log_full(LOG_ERR,     __VA_ARGS__)
242 #define log_emergency(...) log_full(log_emergency_level(), __VA_ARGS__)
243 
244 /* Logging triggered by an errno-like error */
245 #define log_debug_errno(error, ...)     log_full_errno(LOG_DEBUG,   error, __VA_ARGS__)
246 #define log_info_errno(error, ...)      log_full_errno(LOG_INFO,    error, __VA_ARGS__)
247 #define log_notice_errno(error, ...)    log_full_errno(LOG_NOTICE,  error, __VA_ARGS__)
248 #define log_warning_errno(error, ...)   log_full_errno(LOG_WARNING, error, __VA_ARGS__)
249 #define log_error_errno(error, ...)     log_full_errno(LOG_ERR,     error, __VA_ARGS__)
250 #define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__)
251 
252 /* This logs at the specified level the first time it is called, and then
253  * logs at debug. If the specified level is debug, this logs only the first
254  * time it is called. */
255 #define log_once(level, ...)                                             \
256         ({                                                               \
257                 if (ONCE)                                                \
258                         log_full(level, __VA_ARGS__);                    \
259                 else if (LOG_PRI(level) != LOG_DEBUG)                    \
260                         log_debug(__VA_ARGS__);                          \
261         })
262 
263 #define log_once_errno(level, error, ...)                                \
264         ({                                                               \
265                 int _err = (error);                                      \
266                 if (ONCE)                                                \
267                         _err = log_full_errno(level, _err, __VA_ARGS__); \
268                 else if (LOG_PRI(level) != LOG_DEBUG)                    \
269                         _err = log_debug_errno(_err, __VA_ARGS__);       \
270                 else                                                     \
271                         _err = -ERRNO_VALUE(_err);                       \
272                 _err;                                                    \
273         })
274 
275 #if LOG_TRACE
276 #  define log_trace(...)          log_debug(__VA_ARGS__)
277 #  define log_trace_errno(...)    log_debug_errno(__VA_ARGS__)
278 #else
279 #  define log_trace(...)          do {} while (0)
280 #  define log_trace_errno(e, ...) (-ERRNO_VALUE(e))
281 #endif
282 
283 /* Structured logging */
284 #define log_struct_errno(level, error, ...)                             \
285         log_struct_internal(level, error, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__, NULL)
286 #define log_struct(level, ...) log_struct_errno(level, 0, __VA_ARGS__)
287 
288 #define log_struct_iovec_errno(level, error, iovec, n_iovec)            \
289         log_struct_iovec_internal(level, error, PROJECT_FILE, __LINE__, __func__, iovec, n_iovec)
290 #define log_struct_iovec(level, iovec, n_iovec) log_struct_iovec_errno(level, 0, iovec, n_iovec)
291 
292 /* This modifies the buffer passed! */
293 #define log_dump(level, buffer)                                         \
294         log_dump_internal(level, 0, PROJECT_FILE, __LINE__, __func__, buffer)
295 
296 #define log_oom() log_oom_internal(LOG_ERR, PROJECT_FILE, __LINE__, __func__)
297 #define log_oom_debug() log_oom_internal(LOG_DEBUG, PROJECT_FILE, __LINE__, __func__)
298 
299 bool log_on_console(void) _pure_;
300 
301 /* Helper to wrap the main message in structured logging. The macro doesn't do much,
302  * except to provide visual grouping of the format string and its arguments. */
303 #if LOG_MESSAGE_VERIFICATION || defined(__COVERITY__)
304 /* Do a fake formatting of the message string to let the scanner verify the arguments against the format
305  * message. The variable will never be set to true, but we don't tell the compiler that :) */
306 extern bool _log_message_dummy;
307 #  define LOG_MESSAGE(fmt, ...) "MESSAGE=%.0d" fmt, (_log_message_dummy && printf(fmt, ##__VA_ARGS__)), ##__VA_ARGS__
308 #else
309 #  define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
310 #endif
311 
312 void log_received_signal(int level, const struct signalfd_siginfo *si);
313 
314 /* If turned on, any requests for a log target involving "syslog" will be implicitly upgraded to the equivalent journal target */
315 void log_set_upgrade_syslog_to_journal(bool b);
316 
317 /* If turned on, and log_open() is called, we'll not use STDERR_FILENO for logging ever, but rather open /dev/console */
318 void log_set_always_reopen_console(bool b);
319 
320 /* If turned on, we'll open the log stream implicitly if needed on each individual log call. This is normally not
321  * desired as we want to reuse our logging streams. It is useful however  */
322 void log_set_open_when_needed(bool b);
323 
324 /* 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
325  * stderr, the console or kmsg */
326 void log_set_prohibit_ipc(bool b);
327 
328 int log_dup_console(void);
329 
330 int log_syntax_internal(
331                 const char *unit,
332                 int level,
333                 const char *config_file,
334                 unsigned config_line,
335                 int error,
336                 const char *file,
337                 int line,
338                 const char *func,
339                 const char *format, ...) _printf_(9, 10);
340 
341 int log_syntax_invalid_utf8_internal(
342                 const char *unit,
343                 int level,
344                 const char *config_file,
345                 unsigned config_line,
346                 const char *file,
347                 int line,
348                 const char *func,
349                 const char *rvalue);
350 
351 #define log_syntax(unit, level, config_file, config_line, error, ...)   \
352         ({                                                              \
353                 int _level = (level), _e = (error);                     \
354                 (log_get_max_level() >= LOG_PRI(_level))                \
355                         ? log_syntax_internal(unit, _level, config_file, config_line, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
356                         : -ERRNO_VALUE(_e);                             \
357         })
358 
359 #define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \
360         ({                                                              \
361                 int _level = (level);                                   \
362                 (log_get_max_level() >= LOG_PRI(_level))                \
363                         ? log_syntax_invalid_utf8_internal(unit, _level, config_file, config_line, PROJECT_FILE, __LINE__, __func__, rvalue) \
364                         : -EINVAL;                                      \
365         })
366 
367 #define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG)
368 
369 void log_setup(void);
370