xref: /linux/drivers/md/dm-vdo/logger.h (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
103d1089eSMatthew Sakai /* SPDX-License-Identifier: GPL-2.0-only */
203d1089eSMatthew Sakai /*
303d1089eSMatthew Sakai  * Copyright 2023 Red Hat
403d1089eSMatthew Sakai  */
503d1089eSMatthew Sakai 
6*3584240bSMike Snitzer #ifndef VDO_LOGGER_H
7*3584240bSMike Snitzer #define VDO_LOGGER_H
803d1089eSMatthew Sakai 
925315e96SMike Snitzer #include <linux/kern_levels.h>
1003d1089eSMatthew Sakai #include <linux/module.h>
1103d1089eSMatthew Sakai #include <linux/ratelimit.h>
121e00d576SMike Snitzer #include <linux/device-mapper.h>
1303d1089eSMatthew Sakai 
1403d1089eSMatthew Sakai /* Custom logging utilities for UDS */
1503d1089eSMatthew Sakai 
1625315e96SMike Snitzer enum {
17*3584240bSMike Snitzer 	VDO_LOG_EMERG = LOGLEVEL_EMERG,
18*3584240bSMike Snitzer 	VDO_LOG_ALERT = LOGLEVEL_ALERT,
19*3584240bSMike Snitzer 	VDO_LOG_CRIT = LOGLEVEL_CRIT,
20*3584240bSMike Snitzer 	VDO_LOG_ERR = LOGLEVEL_ERR,
21*3584240bSMike Snitzer 	VDO_LOG_WARNING = LOGLEVEL_WARNING,
22*3584240bSMike Snitzer 	VDO_LOG_NOTICE = LOGLEVEL_NOTICE,
23*3584240bSMike Snitzer 	VDO_LOG_INFO = LOGLEVEL_INFO,
24*3584240bSMike Snitzer 	VDO_LOG_DEBUG = LOGLEVEL_DEBUG,
2525315e96SMike Snitzer 
26*3584240bSMike Snitzer 	VDO_LOG_MAX = VDO_LOG_DEBUG,
27*3584240bSMike Snitzer 	VDO_LOG_DEFAULT = VDO_LOG_INFO,
2825315e96SMike Snitzer };
2925315e96SMike Snitzer 
3025315e96SMike Snitzer extern int vdo_log_level;
3103d1089eSMatthew Sakai 
321e00d576SMike Snitzer #define DM_MSG_PREFIX "vdo"
33*3584240bSMike Snitzer #define VDO_LOGGING_MODULE_NAME DM_NAME ": " DM_MSG_PREFIX
3403d1089eSMatthew Sakai 
3503d1089eSMatthew Sakai /* Apply a rate limiter to a log method call. */
36*3584240bSMike Snitzer #define vdo_log_ratelimit(log_fn, ...)                                    \
3703d1089eSMatthew Sakai 	do {                                                              \
3803d1089eSMatthew Sakai 		static DEFINE_RATELIMIT_STATE(_rs,                        \
3903d1089eSMatthew Sakai 					      DEFAULT_RATELIMIT_INTERVAL, \
4003d1089eSMatthew Sakai 					      DEFAULT_RATELIMIT_BURST);   \
4103d1089eSMatthew Sakai 		if (__ratelimit(&_rs)) {                                  \
4203d1089eSMatthew Sakai 			log_fn(__VA_ARGS__);                              \
4303d1089eSMatthew Sakai 		}                                                         \
4403d1089eSMatthew Sakai 	} while (0)
4503d1089eSMatthew Sakai 
46*3584240bSMike Snitzer int vdo_get_log_level(void);
4703d1089eSMatthew Sakai 
48*3584240bSMike Snitzer void vdo_log_embedded_message(int priority, const char *module, const char *prefix,
4903d1089eSMatthew Sakai 			      const char *fmt1, va_list args1, const char *fmt2, ...)
5003d1089eSMatthew Sakai 	__printf(4, 0) __printf(6, 7);
5103d1089eSMatthew Sakai 
52*3584240bSMike Snitzer void vdo_log_backtrace(int priority);
5303d1089eSMatthew Sakai 
5403d1089eSMatthew Sakai /* All log functions will preserve the caller's value of errno. */
5503d1089eSMatthew Sakai 
56*3584240bSMike Snitzer #define vdo_log_strerror(priority, errnum, ...) \
57*3584240bSMike Snitzer 	__vdo_log_strerror(priority, errnum, VDO_LOGGING_MODULE_NAME, __VA_ARGS__)
5803d1089eSMatthew Sakai 
59*3584240bSMike Snitzer int __vdo_log_strerror(int priority, int errnum, const char *module,
6003d1089eSMatthew Sakai 		       const char *format, ...)
6103d1089eSMatthew Sakai 	__printf(4, 5);
6203d1089eSMatthew Sakai 
63*3584240bSMike Snitzer int vdo_vlog_strerror(int priority, int errnum, const char *module, const char *format,
6403d1089eSMatthew Sakai 		      va_list args)
6503d1089eSMatthew Sakai 	__printf(4, 0);
6603d1089eSMatthew Sakai 
6703d1089eSMatthew Sakai /* Log an error prefixed with the string associated with the errnum. */
68*3584240bSMike Snitzer #define vdo_log_error_strerror(errnum, ...) \
69*3584240bSMike Snitzer 	vdo_log_strerror(VDO_LOG_ERR, errnum, __VA_ARGS__)
7003d1089eSMatthew Sakai 
71*3584240bSMike Snitzer #define vdo_log_debug_strerror(errnum, ...) \
72*3584240bSMike Snitzer 	vdo_log_strerror(VDO_LOG_DEBUG, errnum, __VA_ARGS__)
7303d1089eSMatthew Sakai 
74*3584240bSMike Snitzer #define vdo_log_info_strerror(errnum, ...) \
75*3584240bSMike Snitzer 	vdo_log_strerror(VDO_LOG_INFO, errnum, __VA_ARGS__)
7603d1089eSMatthew Sakai 
77*3584240bSMike Snitzer #define vdo_log_warning_strerror(errnum, ...) \
78*3584240bSMike Snitzer 	vdo_log_strerror(VDO_LOG_WARNING, errnum, __VA_ARGS__)
7903d1089eSMatthew Sakai 
80*3584240bSMike Snitzer #define vdo_log_fatal_strerror(errnum, ...) \
81*3584240bSMike Snitzer 	vdo_log_strerror(VDO_LOG_CRIT, errnum, __VA_ARGS__)
8203d1089eSMatthew Sakai 
83*3584240bSMike Snitzer #define vdo_log_message(priority, ...) \
84*3584240bSMike Snitzer 	__vdo_log_message(priority, VDO_LOGGING_MODULE_NAME, __VA_ARGS__)
8503d1089eSMatthew Sakai 
86*3584240bSMike Snitzer void __vdo_log_message(int priority, const char *module, const char *format, ...)
8703d1089eSMatthew Sakai 	__printf(3, 4);
8803d1089eSMatthew Sakai 
89*3584240bSMike Snitzer #define vdo_log_debug(...) vdo_log_message(VDO_LOG_DEBUG, __VA_ARGS__)
9003d1089eSMatthew Sakai 
91*3584240bSMike Snitzer #define vdo_log_info(...) vdo_log_message(VDO_LOG_INFO, __VA_ARGS__)
9203d1089eSMatthew Sakai 
93*3584240bSMike Snitzer #define vdo_log_warning(...) vdo_log_message(VDO_LOG_WARNING, __VA_ARGS__)
9403d1089eSMatthew Sakai 
95*3584240bSMike Snitzer #define vdo_log_error(...) vdo_log_message(VDO_LOG_ERR, __VA_ARGS__)
9603d1089eSMatthew Sakai 
97*3584240bSMike Snitzer #define vdo_log_fatal(...) vdo_log_message(VDO_LOG_CRIT, __VA_ARGS__)
9803d1089eSMatthew Sakai 
99*3584240bSMike Snitzer void vdo_pause_for_logger(void);
100*3584240bSMike Snitzer #endif /* VDO_LOGGER_H */
101