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