1*03d1089eSMatthew Sakai /* SPDX-License-Identifier: GPL-2.0-only */ 2*03d1089eSMatthew Sakai /* 3*03d1089eSMatthew Sakai * Copyright 2023 Red Hat 4*03d1089eSMatthew Sakai */ 5*03d1089eSMatthew Sakai 6*03d1089eSMatthew Sakai #ifndef UDS_LOGGER_H 7*03d1089eSMatthew Sakai #define UDS_LOGGER_H 8*03d1089eSMatthew Sakai 9*03d1089eSMatthew Sakai #include <linux/module.h> 10*03d1089eSMatthew Sakai #include <linux/ratelimit.h> 11*03d1089eSMatthew Sakai 12*03d1089eSMatthew Sakai /* Custom logging utilities for UDS */ 13*03d1089eSMatthew Sakai 14*03d1089eSMatthew Sakai #define UDS_LOG_EMERG 0 15*03d1089eSMatthew Sakai #define UDS_LOG_ALERT 1 16*03d1089eSMatthew Sakai #define UDS_LOG_CRIT 2 17*03d1089eSMatthew Sakai #define UDS_LOG_ERR 3 18*03d1089eSMatthew Sakai #define UDS_LOG_WARNING 4 19*03d1089eSMatthew Sakai #define UDS_LOG_NOTICE 5 20*03d1089eSMatthew Sakai #define UDS_LOG_INFO 6 21*03d1089eSMatthew Sakai #define UDS_LOG_DEBUG 7 22*03d1089eSMatthew Sakai 23*03d1089eSMatthew Sakai #if defined(MODULE) 24*03d1089eSMatthew Sakai #define UDS_LOGGING_MODULE_NAME THIS_MODULE->name 25*03d1089eSMatthew Sakai #else /* compiled into the kernel */ 26*03d1089eSMatthew Sakai #define UDS_LOGGING_MODULE_NAME "vdo" 27*03d1089eSMatthew Sakai #endif 28*03d1089eSMatthew Sakai 29*03d1089eSMatthew Sakai /* Apply a rate limiter to a log method call. */ 30*03d1089eSMatthew Sakai #define uds_log_ratelimit(log_fn, ...) \ 31*03d1089eSMatthew Sakai do { \ 32*03d1089eSMatthew Sakai static DEFINE_RATELIMIT_STATE(_rs, \ 33*03d1089eSMatthew Sakai DEFAULT_RATELIMIT_INTERVAL, \ 34*03d1089eSMatthew Sakai DEFAULT_RATELIMIT_BURST); \ 35*03d1089eSMatthew Sakai if (__ratelimit(&_rs)) { \ 36*03d1089eSMatthew Sakai log_fn(__VA_ARGS__); \ 37*03d1089eSMatthew Sakai } \ 38*03d1089eSMatthew Sakai } while (0) 39*03d1089eSMatthew Sakai 40*03d1089eSMatthew Sakai int uds_get_log_level(void); 41*03d1089eSMatthew Sakai 42*03d1089eSMatthew Sakai void uds_set_log_level(int new_log_level); 43*03d1089eSMatthew Sakai 44*03d1089eSMatthew Sakai int uds_log_string_to_priority(const char *string); 45*03d1089eSMatthew Sakai 46*03d1089eSMatthew Sakai const char *uds_log_priority_to_string(int priority); 47*03d1089eSMatthew Sakai 48*03d1089eSMatthew Sakai void uds_log_embedded_message(int priority, const char *module, const char *prefix, 49*03d1089eSMatthew Sakai const char *fmt1, va_list args1, const char *fmt2, ...) 50*03d1089eSMatthew Sakai __printf(4, 0) __printf(6, 7); 51*03d1089eSMatthew Sakai 52*03d1089eSMatthew Sakai void uds_log_backtrace(int priority); 53*03d1089eSMatthew Sakai 54*03d1089eSMatthew Sakai /* All log functions will preserve the caller's value of errno. */ 55*03d1089eSMatthew Sakai 56*03d1089eSMatthew Sakai #define uds_log_strerror(priority, errnum, ...) \ 57*03d1089eSMatthew Sakai __uds_log_strerror(priority, errnum, UDS_LOGGING_MODULE_NAME, __VA_ARGS__) 58*03d1089eSMatthew Sakai 59*03d1089eSMatthew Sakai int __uds_log_strerror(int priority, int errnum, const char *module, 60*03d1089eSMatthew Sakai const char *format, ...) 61*03d1089eSMatthew Sakai __printf(4, 5); 62*03d1089eSMatthew Sakai 63*03d1089eSMatthew Sakai int uds_vlog_strerror(int priority, int errnum, const char *module, const char *format, 64*03d1089eSMatthew Sakai va_list args) 65*03d1089eSMatthew Sakai __printf(4, 0); 66*03d1089eSMatthew Sakai 67*03d1089eSMatthew Sakai /* Log an error prefixed with the string associated with the errnum. */ 68*03d1089eSMatthew Sakai #define uds_log_error_strerror(errnum, ...) \ 69*03d1089eSMatthew Sakai uds_log_strerror(UDS_LOG_ERR, errnum, __VA_ARGS__) 70*03d1089eSMatthew Sakai 71*03d1089eSMatthew Sakai #define uds_log_debug_strerror(errnum, ...) \ 72*03d1089eSMatthew Sakai uds_log_strerror(UDS_LOG_DEBUG, errnum, __VA_ARGS__) 73*03d1089eSMatthew Sakai 74*03d1089eSMatthew Sakai #define uds_log_info_strerror(errnum, ...) \ 75*03d1089eSMatthew Sakai uds_log_strerror(UDS_LOG_INFO, errnum, __VA_ARGS__) 76*03d1089eSMatthew Sakai 77*03d1089eSMatthew Sakai #define uds_log_notice_strerror(errnum, ...) \ 78*03d1089eSMatthew Sakai uds_log_strerror(UDS_LOG_NOTICE, errnum, __VA_ARGS__) 79*03d1089eSMatthew Sakai 80*03d1089eSMatthew Sakai #define uds_log_warning_strerror(errnum, ...) \ 81*03d1089eSMatthew Sakai uds_log_strerror(UDS_LOG_WARNING, errnum, __VA_ARGS__) 82*03d1089eSMatthew Sakai 83*03d1089eSMatthew Sakai #define uds_log_fatal_strerror(errnum, ...) \ 84*03d1089eSMatthew Sakai uds_log_strerror(UDS_LOG_CRIT, errnum, __VA_ARGS__) 85*03d1089eSMatthew Sakai 86*03d1089eSMatthew Sakai #define uds_log_message(priority, ...) \ 87*03d1089eSMatthew Sakai __uds_log_message(priority, UDS_LOGGING_MODULE_NAME, __VA_ARGS__) 88*03d1089eSMatthew Sakai 89*03d1089eSMatthew Sakai void __uds_log_message(int priority, const char *module, const char *format, ...) 90*03d1089eSMatthew Sakai __printf(3, 4); 91*03d1089eSMatthew Sakai 92*03d1089eSMatthew Sakai #define uds_log_debug(...) uds_log_message(UDS_LOG_DEBUG, __VA_ARGS__) 93*03d1089eSMatthew Sakai 94*03d1089eSMatthew Sakai #define uds_log_info(...) uds_log_message(UDS_LOG_INFO, __VA_ARGS__) 95*03d1089eSMatthew Sakai 96*03d1089eSMatthew Sakai #define uds_log_notice(...) uds_log_message(UDS_LOG_NOTICE, __VA_ARGS__) 97*03d1089eSMatthew Sakai 98*03d1089eSMatthew Sakai #define uds_log_warning(...) uds_log_message(UDS_LOG_WARNING, __VA_ARGS__) 99*03d1089eSMatthew Sakai 100*03d1089eSMatthew Sakai #define uds_log_error(...) uds_log_message(UDS_LOG_ERR, __VA_ARGS__) 101*03d1089eSMatthew Sakai 102*03d1089eSMatthew Sakai #define uds_log_fatal(...) uds_log_message(UDS_LOG_CRIT, __VA_ARGS__) 103*03d1089eSMatthew Sakai 104*03d1089eSMatthew Sakai void uds_pause_for_logger(void); 105*03d1089eSMatthew Sakai #endif /* UDS_LOGGER_H */ 106