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