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