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