]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/printk.h
bc1619f7cbf765e184d4d80a2cd83a876c4b68e1
[bcachefs-tools-debian] / include / linux / printk.h
1 #ifndef __TOOLS_LINUX_PRINTK_H
2 #define __TOOLS_LINUX_PRINTK_H
3
4 #ifndef pr_fmt
5 #define pr_fmt(fmt) fmt
6 #endif
7
8 #include <stdarg.h>
9 #include <stdio.h>
10
11 #define KERN_EMERG      ""
12 #define KERN_ALERT      ""
13 #define KERN_CRIT       ""
14 #define KERN_ERR        ""
15 #define KERN_WARNING    ""
16 #define KERN_NOTICE     ""
17 #define KERN_INFO       ""
18 #define KERN_DEBUG      ""
19 #define KERN_DEFAULT    ""
20 #define KERN_CONT       ""
21
22 static inline int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
23 {
24         int i;
25
26         i = vsnprintf(buf, size, fmt, args);
27
28         if (likely(i < size))
29                 return i;
30         if (size != 0)
31                 return size - 1;
32         return 0;
33 }
34
35 static inline int scnprintf(char * buf, size_t size, const char * fmt, ...)
36 {
37         va_list args;
38         int i;
39
40         va_start(args, fmt);
41         i = vscnprintf(buf, size, fmt, args);
42         va_end(args);
43
44         return i;
45 }
46
47 #define printk(...)     printf(__VA_ARGS__)
48 #define vprintk(...)    vprintf(__VA_ARGS__)
49
50 #define no_printk(fmt, ...)                             \
51 ({                                                      \
52         do {                                            \
53                 if (0)                                  \
54                         printk(fmt, ##__VA_ARGS__);     \
55         } while (0);                                    \
56         0;                                              \
57 })
58
59 #define pr_emerg(fmt, ...) \
60         printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
61 #define pr_alert(fmt, ...) \
62         printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
63 #define pr_crit(fmt, ...) \
64         printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
65 #define pr_err(fmt, ...) \
66         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
67 #define pr_warning(fmt, ...) \
68         printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
69 #define pr_warn pr_warning
70 #define pr_notice(fmt, ...) \
71         printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
72 #define pr_info(fmt, ...) \
73         printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
74 /*
75  * Like KERN_CONT, pr_cont() should only be used when continuing
76  * a line with no newline ('\n') enclosed. Otherwise it defaults
77  * back to KERN_DEFAULT.
78  */
79 #define pr_cont(fmt, ...) \
80         printk(KERN_CONT fmt, ##__VA_ARGS__)
81
82 /* pr_devel() should produce zero code unless DEBUG is defined */
83 #ifdef DEBUG
84 #define pr_devel(fmt, ...) \
85         printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
86 #else
87 #define pr_devel(fmt, ...) \
88         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
89 #endif
90
91
92 /* If you are writing a driver, please use dev_dbg instead */
93 #if defined(CONFIG_DYNAMIC_DEBUG)
94 #include <linux/dynamic_debug.h>
95
96 /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
97 #define pr_debug(fmt, ...) \
98         dynamic_pr_debug(fmt, ##__VA_ARGS__)
99 #elif defined(DEBUG)
100 #define pr_debug(fmt, ...) \
101         printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
102 #else
103 #define pr_debug(fmt, ...) \
104         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
105 #endif
106
107 /*
108  * Print a one-time message (analogous to WARN_ONCE() et al):
109  */
110
111 #define printk_once(fmt, ...)                                   \
112 ({                                                              \
113         static bool __print_once __read_mostly;                 \
114         bool __ret_print_once = !__print_once;                  \
115                                                                 \
116         if (!__print_once) {                                    \
117                 __print_once = true;                            \
118                 printk(fmt, ##__VA_ARGS__);                     \
119         }                                                       \
120         unlikely(__ret_print_once);                             \
121 })
122 #define printk_deferred_once(fmt, ...)                          \
123 ({                                                              \
124         static bool __print_once __read_mostly;                 \
125         bool __ret_print_once = !__print_once;                  \
126                                                                 \
127         if (!__print_once) {                                    \
128                 __print_once = true;                            \
129                 printk_deferred(fmt, ##__VA_ARGS__);            \
130         }                                                       \
131         unlikely(__ret_print_once);                             \
132 })
133
134 #define pr_emerg_once(fmt, ...)                                 \
135         printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
136 #define pr_alert_once(fmt, ...)                                 \
137         printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
138 #define pr_crit_once(fmt, ...)                                  \
139         printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
140 #define pr_err_once(fmt, ...)                                   \
141         printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
142 #define pr_warn_once(fmt, ...)                                  \
143         printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
144 #define pr_notice_once(fmt, ...)                                \
145         printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
146 #define pr_info_once(fmt, ...)                                  \
147         printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
148 #define pr_cont_once(fmt, ...)                                  \
149         printk_once(KERN_CONT pr_fmt(fmt), ##__VA_ARGS__)
150
151 #if defined(DEBUG)
152 #define pr_devel_once(fmt, ...)                                 \
153         printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
154 #else
155 #define pr_devel_once(fmt, ...)                                 \
156         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
157 #endif
158
159 /* If you are writing a driver, please use dev_dbg instead */
160 #if defined(DEBUG)
161 #define pr_debug_once(fmt, ...)                                 \
162         printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
163 #else
164 #define pr_debug_once(fmt, ...)                                 \
165         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
166 #endif
167
168 /*
169  * ratelimited messages with local ratelimit_state,
170  * no local ratelimit_state used in the !PRINTK case
171  */
172 #ifdef CONFIG_PRINTK
173 #define printk_ratelimited(fmt, ...)                                    \
174 ({                                                                      \
175         static DEFINE_RATELIMIT_STATE(_rs,                              \
176                                       DEFAULT_RATELIMIT_INTERVAL,       \
177                                       DEFAULT_RATELIMIT_BURST);         \
178                                                                         \
179         if (__ratelimit(&_rs))                                          \
180                 printk(fmt, ##__VA_ARGS__);                             \
181 })
182 #else
183 #define printk_ratelimited(fmt, ...)                                    \
184         no_printk(fmt, ##__VA_ARGS__)
185 #endif
186
187 #define pr_emerg_ratelimited(fmt, ...)                                  \
188         printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
189 #define pr_alert_ratelimited(fmt, ...)                                  \
190         printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
191 #define pr_crit_ratelimited(fmt, ...)                                   \
192         printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
193 #define pr_err_ratelimited(fmt, ...)                                    \
194         printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
195 #define pr_warn_ratelimited(fmt, ...)                                   \
196         printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
197 #define pr_notice_ratelimited(fmt, ...)                                 \
198         printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
199 #define pr_info_ratelimited(fmt, ...)                                   \
200         printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
201 /* no pr_cont_ratelimited, don't do that... */
202
203 #if defined(DEBUG)
204 #define pr_devel_ratelimited(fmt, ...)                                  \
205         printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
206 #else
207 #define pr_devel_ratelimited(fmt, ...)                                  \
208         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
209 #endif
210 #endif /* __TOOLS_LINUX_PRINTK_H */