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