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