xref: /linux/include/linux/dev_printk.h (revision f86fd32d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * dev_printk.h - printk messages helpers for devices
4  *
5  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
6  * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
7  * Copyright (c) 2008-2009 Novell Inc.
8  *
9  */
10 
11 #ifndef _DEVICE_PRINTK_H_
12 #define _DEVICE_PRINTK_H_
13 
14 #include <linux/compiler.h>
15 #include <linux/types.h>
16 #include <linux/ratelimit.h>
17 
18 #ifndef dev_fmt
19 #define dev_fmt(fmt) fmt
20 #endif
21 
22 struct device;
23 
24 #ifdef CONFIG_PRINTK
25 
26 __printf(3, 0) __cold
27 int dev_vprintk_emit(int level, const struct device *dev,
28 		     const char *fmt, va_list args);
29 __printf(3, 4) __cold
30 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
31 
32 __printf(3, 4) __cold
33 void dev_printk(const char *level, const struct device *dev,
34 		const char *fmt, ...);
35 __printf(2, 3) __cold
36 void _dev_emerg(const struct device *dev, const char *fmt, ...);
37 __printf(2, 3) __cold
38 void _dev_alert(const struct device *dev, const char *fmt, ...);
39 __printf(2, 3) __cold
40 void _dev_crit(const struct device *dev, const char *fmt, ...);
41 __printf(2, 3) __cold
42 void _dev_err(const struct device *dev, const char *fmt, ...);
43 __printf(2, 3) __cold
44 void _dev_warn(const struct device *dev, const char *fmt, ...);
45 __printf(2, 3) __cold
46 void _dev_notice(const struct device *dev, const char *fmt, ...);
47 __printf(2, 3) __cold
48 void _dev_info(const struct device *dev, const char *fmt, ...);
49 
50 #else
51 
52 static inline __printf(3, 0)
53 int dev_vprintk_emit(int level, const struct device *dev,
54 		     const char *fmt, va_list args)
55 { return 0; }
56 static inline __printf(3, 4)
57 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
58 { return 0; }
59 
60 static inline void __dev_printk(const char *level, const struct device *dev,
61 				struct va_format *vaf)
62 {}
63 static inline __printf(3, 4)
64 void dev_printk(const char *level, const struct device *dev,
65 		 const char *fmt, ...)
66 {}
67 
68 static inline __printf(2, 3)
69 void _dev_emerg(const struct device *dev, const char *fmt, ...)
70 {}
71 static inline __printf(2, 3)
72 void _dev_crit(const struct device *dev, const char *fmt, ...)
73 {}
74 static inline __printf(2, 3)
75 void _dev_alert(const struct device *dev, const char *fmt, ...)
76 {}
77 static inline __printf(2, 3)
78 void _dev_err(const struct device *dev, const char *fmt, ...)
79 {}
80 static inline __printf(2, 3)
81 void _dev_warn(const struct device *dev, const char *fmt, ...)
82 {}
83 static inline __printf(2, 3)
84 void _dev_notice(const struct device *dev, const char *fmt, ...)
85 {}
86 static inline __printf(2, 3)
87 void _dev_info(const struct device *dev, const char *fmt, ...)
88 {}
89 
90 #endif
91 
92 /*
93  * #defines for all the dev_<level> macros to prefix with whatever
94  * possible use of #define dev_fmt(fmt) ...
95  */
96 
97 #define dev_emerg(dev, fmt, ...)					\
98 	_dev_emerg(dev, dev_fmt(fmt), ##__VA_ARGS__)
99 #define dev_crit(dev, fmt, ...)						\
100 	_dev_crit(dev, dev_fmt(fmt), ##__VA_ARGS__)
101 #define dev_alert(dev, fmt, ...)					\
102 	_dev_alert(dev, dev_fmt(fmt), ##__VA_ARGS__)
103 #define dev_err(dev, fmt, ...)						\
104 	_dev_err(dev, dev_fmt(fmt), ##__VA_ARGS__)
105 #define dev_warn(dev, fmt, ...)						\
106 	_dev_warn(dev, dev_fmt(fmt), ##__VA_ARGS__)
107 #define dev_notice(dev, fmt, ...)					\
108 	_dev_notice(dev, dev_fmt(fmt), ##__VA_ARGS__)
109 #define dev_info(dev, fmt, ...)						\
110 	_dev_info(dev, dev_fmt(fmt), ##__VA_ARGS__)
111 
112 #if defined(CONFIG_DYNAMIC_DEBUG)
113 #define dev_dbg(dev, fmt, ...)						\
114 	dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
115 #elif defined(DEBUG)
116 #define dev_dbg(dev, fmt, ...)						\
117 	dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
118 #else
119 #define dev_dbg(dev, fmt, ...)						\
120 ({									\
121 	if (0)								\
122 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
123 })
124 #endif
125 
126 #ifdef CONFIG_PRINTK
127 #define dev_level_once(dev_level, dev, fmt, ...)			\
128 do {									\
129 	static bool __print_once __read_mostly;				\
130 									\
131 	if (!__print_once) {						\
132 		__print_once = true;					\
133 		dev_level(dev, fmt, ##__VA_ARGS__);			\
134 	}								\
135 } while (0)
136 #else
137 #define dev_level_once(dev_level, dev, fmt, ...)			\
138 do {									\
139 	if (0)								\
140 		dev_level(dev, fmt, ##__VA_ARGS__);			\
141 } while (0)
142 #endif
143 
144 #define dev_emerg_once(dev, fmt, ...)					\
145 	dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
146 #define dev_alert_once(dev, fmt, ...)					\
147 	dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
148 #define dev_crit_once(dev, fmt, ...)					\
149 	dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
150 #define dev_err_once(dev, fmt, ...)					\
151 	dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
152 #define dev_warn_once(dev, fmt, ...)					\
153 	dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
154 #define dev_notice_once(dev, fmt, ...)					\
155 	dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
156 #define dev_info_once(dev, fmt, ...)					\
157 	dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
158 #define dev_dbg_once(dev, fmt, ...)					\
159 	dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__)
160 
161 #define dev_level_ratelimited(dev_level, dev, fmt, ...)			\
162 do {									\
163 	static DEFINE_RATELIMIT_STATE(_rs,				\
164 				      DEFAULT_RATELIMIT_INTERVAL,	\
165 				      DEFAULT_RATELIMIT_BURST);		\
166 	if (__ratelimit(&_rs))						\
167 		dev_level(dev, fmt, ##__VA_ARGS__);			\
168 } while (0)
169 
170 #define dev_emerg_ratelimited(dev, fmt, ...)				\
171 	dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
172 #define dev_alert_ratelimited(dev, fmt, ...)				\
173 	dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
174 #define dev_crit_ratelimited(dev, fmt, ...)				\
175 	dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
176 #define dev_err_ratelimited(dev, fmt, ...)				\
177 	dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
178 #define dev_warn_ratelimited(dev, fmt, ...)				\
179 	dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
180 #define dev_notice_ratelimited(dev, fmt, ...)				\
181 	dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
182 #define dev_info_ratelimited(dev, fmt, ...)				\
183 	dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
184 #if defined(CONFIG_DYNAMIC_DEBUG)
185 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
186 #define dev_dbg_ratelimited(dev, fmt, ...)				\
187 do {									\
188 	static DEFINE_RATELIMIT_STATE(_rs,				\
189 				      DEFAULT_RATELIMIT_INTERVAL,	\
190 				      DEFAULT_RATELIMIT_BURST);		\
191 	DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);			\
192 	if (DYNAMIC_DEBUG_BRANCH(descriptor) &&				\
193 	    __ratelimit(&_rs))						\
194 		__dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt),	\
195 				  ##__VA_ARGS__);			\
196 } while (0)
197 #elif defined(DEBUG)
198 #define dev_dbg_ratelimited(dev, fmt, ...)				\
199 do {									\
200 	static DEFINE_RATELIMIT_STATE(_rs,				\
201 				      DEFAULT_RATELIMIT_INTERVAL,	\
202 				      DEFAULT_RATELIMIT_BURST);		\
203 	if (__ratelimit(&_rs))						\
204 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
205 } while (0)
206 #else
207 #define dev_dbg_ratelimited(dev, fmt, ...)				\
208 do {									\
209 	if (0)								\
210 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
211 } while (0)
212 #endif
213 
214 #ifdef VERBOSE_DEBUG
215 #define dev_vdbg	dev_dbg
216 #else
217 #define dev_vdbg(dev, fmt, ...)						\
218 ({									\
219 	if (0)								\
220 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
221 })
222 #endif
223 
224 /*
225  * dev_WARN*() acts like dev_printk(), but with the key difference of
226  * using WARN/WARN_ONCE to include file/line information and a backtrace.
227  */
228 #define dev_WARN(dev, format, arg...) \
229 	WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg);
230 
231 #define dev_WARN_ONCE(dev, condition, format, arg...) \
232 	WARN_ONCE(condition, "%s %s: " format, \
233 			dev_driver_string(dev), dev_name(dev), ## arg)
234 
235 #endif /* _DEVICE_PRINTK_H_ */
236