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