a1c608ab41e92349370ba8f0a674221ba7010946
[dpdk.git] / lib / eal / include / rte_log.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #ifndef _RTE_LOG_H_
6 #define _RTE_LOG_H_
7
8 /**
9  * @file
10  *
11  * RTE Logs API
12  *
13  * This file provides a log API to RTE applications.
14  */
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <stdbool.h>
24 #include <sys/queue.h>
25
26 #include <rte_common.h>
27 #include <rte_config.h>
28 #include <rte_compat.h>
29
30 /* SDK log type */
31 #define RTE_LOGTYPE_EAL        0 /**< Log related to eal. */
32 #define RTE_LOGTYPE_MALLOC     1 /**< Log related to malloc. */
33 #define RTE_LOGTYPE_RING       2 /**< Log related to ring. */
34 #define RTE_LOGTYPE_MEMPOOL    3 /**< Log related to mempool. */
35 #define RTE_LOGTYPE_TIMER      4 /**< Log related to timers. */
36 #define RTE_LOGTYPE_PMD        5 /**< Log related to poll mode driver. */
37 #define RTE_LOGTYPE_HASH       6 /**< Log related to hash table. */
38 #define RTE_LOGTYPE_LPM        7 /**< Log related to LPM. */
39 #define RTE_LOGTYPE_KNI        8 /**< Log related to KNI. */
40 #define RTE_LOGTYPE_ACL        9 /**< Log related to ACL. */
41 #define RTE_LOGTYPE_POWER     10 /**< Log related to power. */
42 #define RTE_LOGTYPE_METER     11 /**< Log related to QoS meter. */
43 #define RTE_LOGTYPE_SCHED     12 /**< Log related to QoS port scheduler. */
44 #define RTE_LOGTYPE_PORT      13 /**< Log related to port. */
45 #define RTE_LOGTYPE_TABLE     14 /**< Log related to table. */
46 #define RTE_LOGTYPE_PIPELINE  15 /**< Log related to pipeline. */
47 #define RTE_LOGTYPE_MBUF      16 /**< Log related to mbuf. */
48 #define RTE_LOGTYPE_CRYPTODEV 17 /**< Log related to cryptodev. */
49 #define RTE_LOGTYPE_EFD       18 /**< Log related to EFD. */
50 #define RTE_LOGTYPE_EVENTDEV  19 /**< Log related to eventdev. */
51 #define RTE_LOGTYPE_GSO       20 /**< Log related to GSO. */
52
53 /* these log types can be used in an application */
54 #define RTE_LOGTYPE_USER1     24 /**< User-defined log type 1. */
55 #define RTE_LOGTYPE_USER2     25 /**< User-defined log type 2. */
56 #define RTE_LOGTYPE_USER3     26 /**< User-defined log type 3. */
57 #define RTE_LOGTYPE_USER4     27 /**< User-defined log type 4. */
58 #define RTE_LOGTYPE_USER5     28 /**< User-defined log type 5. */
59 #define RTE_LOGTYPE_USER6     29 /**< User-defined log type 6. */
60 #define RTE_LOGTYPE_USER7     30 /**< User-defined log type 7. */
61 #define RTE_LOGTYPE_USER8     31 /**< User-defined log type 8. */
62
63 /** First identifier for extended logs */
64 #define RTE_LOGTYPE_FIRST_EXT_ID 32
65
66 /* Can't use 0, as it gives compiler warnings */
67 #define RTE_LOG_EMERG    1U  /**< System is unusable.               */
68 #define RTE_LOG_ALERT    2U  /**< Action must be taken immediately. */
69 #define RTE_LOG_CRIT     3U  /**< Critical conditions.              */
70 #define RTE_LOG_ERR      4U  /**< Error conditions.                 */
71 #define RTE_LOG_WARNING  5U  /**< Warning conditions.               */
72 #define RTE_LOG_NOTICE   6U  /**< Normal but significant condition. */
73 #define RTE_LOG_INFO     7U  /**< Informational.                    */
74 #define RTE_LOG_DEBUG    8U  /**< Debug-level messages.             */
75 #define RTE_LOG_MAX RTE_LOG_DEBUG /**< Most detailed log level.     */
76
77 /**
78  * Change the stream that will be used by the logging system.
79  *
80  * This can be done at any time. The f argument represents the stream
81  * to be used to send the logs. If f is NULL, the default output is
82  * used (stderr).
83  *
84  * @param f
85  *   Pointer to the stream.
86  * @return
87  *   - 0 on success.
88  *   - Negative on error.
89  */
90 int rte_openlog_stream(FILE *f);
91
92 /**
93  * Retrieve the stream used by the logging system (see rte_openlog_stream()
94  * to change it).
95  *
96  * @return
97  *   Pointer to the stream.
98  */
99 FILE *rte_log_get_stream(void);
100
101 /**
102  * Set the global log level.
103  *
104  * After this call, logs with a level lower or equal than the level
105  * passed as argument will be displayed.
106  *
107  * @param level
108  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
109  */
110 void rte_log_set_global_level(uint32_t level);
111
112 /**
113  * Get the global log level.
114  *
115  * @return
116  *   The current global log level.
117  */
118 uint32_t rte_log_get_global_level(void);
119
120 /**
121  * Get the log level for a given type.
122  *
123  * @param logtype
124  *   The log type identifier.
125  * @return
126  *   0 on success, a negative value if logtype is invalid.
127  */
128 int rte_log_get_level(uint32_t logtype);
129
130 /**
131  * For a given `logtype`, check if a log with `loglevel` can be printed.
132  *
133  * @param logtype
134  *   The log type identifier
135  * @param loglevel
136  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
137  * @return
138  * Returns 'true' if log can be printed and 'false' if it can't.
139  */
140 __rte_experimental
141 bool rte_log_can_log(uint32_t logtype, uint32_t loglevel);
142
143 /**
144  * Set the log level for a given type based on globbing pattern.
145  *
146  * @param pattern
147  *   The globbing pattern identifying the log type.
148  * @param level
149  *   The level to be set.
150  * @return
151  *   0 on success, a negative value if level is invalid.
152  */
153 int rte_log_set_level_pattern(const char *pattern, uint32_t level);
154
155 /**
156  * Set the log level for a given type based on regular expression.
157  *
158  * @param regex
159  *   The regular expression identifying the log type.
160  * @param level
161  *   The level to be set.
162  * @return
163  *   0 on success, a negative value if level is invalid.
164  */
165 int rte_log_set_level_regexp(const char *regex, uint32_t level);
166
167 /**
168  * Set the log level for a given type.
169  *
170  * @param logtype
171  *   The log type identifier.
172  * @param level
173  *   The level to be set.
174  * @return
175  *   0 on success, a negative value if logtype or level is invalid.
176  */
177 int rte_log_set_level(uint32_t logtype, uint32_t level);
178
179 /**
180  * Get the current loglevel for the message being processed.
181  *
182  * Before calling the user-defined stream for logging, the log
183  * subsystem sets a per-lcore variable containing the loglevel and the
184  * logtype of the message being processed. This information can be
185  * accessed by the user-defined log output function through this
186  * function.
187  *
188  * @return
189  *   The loglevel of the message being processed.
190  */
191 int rte_log_cur_msg_loglevel(void);
192
193 /**
194  * Get the current logtype for the message being processed.
195  *
196  * Before calling the user-defined stream for logging, the log
197  * subsystem sets a per-lcore variable containing the loglevel and the
198  * logtype of the message being processed. This information can be
199  * accessed by the user-defined log output function through this
200  * function.
201  *
202  * @return
203  *   The logtype of the message being processed.
204  */
205 int rte_log_cur_msg_logtype(void);
206
207 /**
208  * Register a dynamic log type
209  *
210  * If a log is already registered with the same type, the returned value
211  * is the same than the previous one.
212  *
213  * @param name
214  *   The string identifying the log type.
215  * @return
216  *   - >0: success, the returned value is the log type identifier.
217  *   - (-ENOMEM): cannot allocate memory.
218  */
219 int rte_log_register(const char *name);
220
221 /**
222  * @warning
223  * @b EXPERIMENTAL: this API may change without prior notice
224  *
225  * Register a dynamic log type and try to pick its level from EAL options
226  *
227  * rte_log_register() is called inside. If successful, the function tries
228  * to search for matching regexp in the list of EAL log level options and
229  * pick the level from the last matching entry. If nothing can be applied
230  * from the list, the level will be set to the user-defined default value.
231  *
232  * @param name
233  *    Name for the log type to be registered
234  * @param level_def
235  *    Fallback level to be set if the global list has no matching options
236  * @return
237  *    - >=0: the newly registered log type
238  *    - <0: rte_log_register() error value
239  */
240 __rte_experimental
241 int rte_log_register_type_and_pick_level(const char *name, uint32_t level_def);
242
243 /**
244  * @warning
245  * @b EXPERIMENTAL: this API may change without prior notice
246  *
247  * Dump name of each logtype, one per line.
248  *
249  * @param out
250  *   Stream where the list is sent.
251  * @param prefix
252  *   String preceding each logtype in the output.
253  */
254 __rte_experimental
255 void rte_log_list_types(FILE *out, const char *prefix);
256
257 /**
258  * Dump log information.
259  *
260  * Dump the global level and the registered log types.
261  *
262  * @param f
263  *   The output stream where the dump should be sent.
264  */
265 void rte_log_dump(FILE *f);
266
267 /**
268  * Generates a log message.
269  *
270  * The message will be sent in the stream defined by the previous call
271  * to rte_openlog_stream().
272  *
273  * The level argument determines if the log should be displayed or
274  * not, depending on the loglevel settings.
275  *
276  * The preferred alternative is the RTE_LOG() because it adds the
277  * level and type in the logged string.
278  *
279  * @param level
280  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
281  * @param logtype
282  *   The log type, for example, RTE_LOGTYPE_EAL.
283  * @param format
284  *   The format string, as in printf(3), followed by the variable arguments
285  *   required by the format.
286  * @return
287  *   - 0: Success.
288  *   - Negative on error.
289  */
290 int rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
291 #ifdef __GNUC__
292 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
293         __rte_cold
294 #endif
295 #endif
296         __rte_format_printf(3, 4);
297
298 /**
299  * Generates a log message.
300  *
301  * The message will be sent in the stream defined by the previous call
302  * to rte_openlog_stream().
303  *
304  * The level argument determines if the log should be displayed or
305  * not, depending on the loglevel settings. A trailing
306  * newline may be added if needed.
307  *
308  * The preferred alternative is the RTE_LOG() because it adds the
309  * level and type in the logged string.
310  *
311  * @param level
312  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
313  * @param logtype
314  *   The log type, for example, RTE_LOGTYPE_EAL.
315  * @param format
316  *   The format string, as in printf(3), followed by the variable arguments
317  *   required by the format.
318  * @param ap
319  *   The va_list of the variable arguments required by the format.
320  * @return
321  *   - 0: Success.
322  *   - Negative on error.
323  */
324 int rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
325         __rte_format_printf(3, 0);
326
327 /**
328  * Generates a log message.
329  *
330  * The RTE_LOG() is a helper that prefixes the string with the log level
331  * and type, and call rte_log().
332  *
333  * @param l
334  *   Log level. A value between EMERG (1) and DEBUG (8). The short name is
335  *   expanded by the macro, so it cannot be an integer value.
336  * @param t
337  *   The log type, for example, EAL. The short name is expanded by the
338  *   macro, so it cannot be an integer value.
339  * @param ...
340  *   The fmt string, as in printf(3), followed by the variable arguments
341  *   required by the format.
342  * @return
343  *   - 0: Success.
344  *   - Negative on error.
345  */
346 #define RTE_LOG(l, t, ...)                                      \
347          rte_log(RTE_LOG_ ## l,                                 \
348                  RTE_LOGTYPE_ ## t, # t ": " __VA_ARGS__)
349
350 /**
351  * Generates a log message for data path.
352  *
353  * Similar to RTE_LOG(), except that it is removed at compilation time
354  * if the RTE_LOG_DP_LEVEL configuration option is lower than the log
355  * level argument.
356  *
357  * @param l
358  *   Log level. A value between EMERG (1) and DEBUG (8). The short name is
359  *   expanded by the macro, so it cannot be an integer value.
360  * @param t
361  *   The log type, for example, EAL. The short name is expanded by the
362  *   macro, so it cannot be an integer value.
363  * @param ...
364  *   The fmt string, as in printf(3), followed by the variable arguments
365  *   required by the format.
366  * @return
367  *   - 0: Success.
368  *   - Negative on error.
369  */
370 #define RTE_LOG_DP(l, t, ...)                                   \
371         (void)((RTE_LOG_ ## l <= RTE_LOG_DP_LEVEL) ?            \
372          rte_log(RTE_LOG_ ## l,                                 \
373                  RTE_LOGTYPE_ ## t, # t ": " __VA_ARGS__) :     \
374          0)
375
376 /**
377  * @warning
378  * @b EXPERIMENTAL: this API may change without prior notice
379  *
380  * Register a dynamic log type in constructor context with its name and level.
381  *
382  * It is a wrapper macro for declaring the logtype, register the log and
383  * sets it's level in the constructor context.
384  *
385  * @param type
386  *   The log type identifier
387  * @param name
388  *    Name for the log type to be registered
389  * @param level
390  *   Log level. A value between EMERG (1) and DEBUG (8).
391  */
392 #define RTE_LOG_REGISTER(type, name, level)                             \
393 int type;                                                               \
394 RTE_INIT(__##type)                                                      \
395 {                                                                       \
396         type = rte_log_register_type_and_pick_level(RTE_STR(name),      \
397                                                     RTE_LOG_##level);   \
398         if (type < 0)                                                   \
399                 type = RTE_LOGTYPE_EAL;                                 \
400 }
401
402 #ifdef __cplusplus
403 }
404 #endif
405
406 #endif /* _RTE_LOG_H_ */