1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright 2023 Red Hat
4 */
5
6#ifndef VDO_LOGGER_H
7#define VDO_LOGGER_H
8
9#include <linux/kern_levels.h>
10#include <linux/module.h>
11#include <linux/ratelimit.h>
12#include <linux/device-mapper.h>
13
14/* Custom logging utilities for UDS */
15
16enum {
17	VDO_LOG_EMERG = LOGLEVEL_EMERG,
18	VDO_LOG_ALERT = LOGLEVEL_ALERT,
19	VDO_LOG_CRIT = LOGLEVEL_CRIT,
20	VDO_LOG_ERR = LOGLEVEL_ERR,
21	VDO_LOG_WARNING = LOGLEVEL_WARNING,
22	VDO_LOG_NOTICE = LOGLEVEL_NOTICE,
23	VDO_LOG_INFO = LOGLEVEL_INFO,
24	VDO_LOG_DEBUG = LOGLEVEL_DEBUG,
25
26	VDO_LOG_MAX = VDO_LOG_DEBUG,
27	VDO_LOG_DEFAULT = VDO_LOG_INFO,
28};
29
30extern int vdo_log_level;
31
32#define DM_MSG_PREFIX "vdo"
33#define VDO_LOGGING_MODULE_NAME DM_NAME ": " DM_MSG_PREFIX
34
35/* Apply a rate limiter to a log method call. */
36#define vdo_log_ratelimit(log_fn, ...)                                    \
37	do {                                                              \
38		static DEFINE_RATELIMIT_STATE(_rs,                        \
39					      DEFAULT_RATELIMIT_INTERVAL, \
40					      DEFAULT_RATELIMIT_BURST);   \
41		if (__ratelimit(&_rs)) {                                  \
42			log_fn(__VA_ARGS__);                              \
43		}                                                         \
44	} while (0)
45
46int vdo_get_log_level(void);
47
48void vdo_log_embedded_message(int priority, const char *module, const char *prefix,
49			      const char *fmt1, va_list args1, const char *fmt2, ...)
50	__printf(4, 0) __printf(6, 7);
51
52void vdo_log_backtrace(int priority);
53
54/* All log functions will preserve the caller's value of errno. */
55
56#define vdo_log_strerror(priority, errnum, ...) \
57	__vdo_log_strerror(priority, errnum, VDO_LOGGING_MODULE_NAME, __VA_ARGS__)
58
59int __vdo_log_strerror(int priority, int errnum, const char *module,
60		       const char *format, ...)
61	__printf(4, 5);
62
63int vdo_vlog_strerror(int priority, int errnum, const char *module, const char *format,
64		      va_list args)
65	__printf(4, 0);
66
67/* Log an error prefixed with the string associated with the errnum. */
68#define vdo_log_error_strerror(errnum, ...) \
69	vdo_log_strerror(VDO_LOG_ERR, errnum, __VA_ARGS__)
70
71#define vdo_log_debug_strerror(errnum, ...) \
72	vdo_log_strerror(VDO_LOG_DEBUG, errnum, __VA_ARGS__)
73
74#define vdo_log_info_strerror(errnum, ...) \
75	vdo_log_strerror(VDO_LOG_INFO, errnum, __VA_ARGS__)
76
77#define vdo_log_warning_strerror(errnum, ...) \
78	vdo_log_strerror(VDO_LOG_WARNING, errnum, __VA_ARGS__)
79
80#define vdo_log_fatal_strerror(errnum, ...) \
81	vdo_log_strerror(VDO_LOG_CRIT, errnum, __VA_ARGS__)
82
83#define vdo_log_message(priority, ...) \
84	__vdo_log_message(priority, VDO_LOGGING_MODULE_NAME, __VA_ARGS__)
85
86void __vdo_log_message(int priority, const char *module, const char *format, ...)
87	__printf(3, 4);
88
89#define vdo_log_debug(...) vdo_log_message(VDO_LOG_DEBUG, __VA_ARGS__)
90
91#define vdo_log_info(...) vdo_log_message(VDO_LOG_INFO, __VA_ARGS__)
92
93#define vdo_log_warning(...) vdo_log_message(VDO_LOG_WARNING, __VA_ARGS__)
94
95#define vdo_log_error(...) vdo_log_message(VDO_LOG_ERR, __VA_ARGS__)
96
97#define vdo_log_fatal(...) vdo_log_message(VDO_LOG_CRIT, __VA_ARGS__)
98
99void vdo_pause_for_logger(void);
100#endif /* VDO_LOGGER_H */
101