New upstream version 17.11.1
[deb_dpdk.git] / lib / librte_eal / common / include / rte_log.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_LOG_H_
35 #define _RTE_LOG_H_
36
37 /**
38  * @file
39  *
40  * RTE Logs API
41  *
42  * This file provides a log API to RTE applications.
43  */
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #include <stdint.h>
50 #include <stdio.h>
51 #include <stdarg.h>
52
53 #include <rte_common.h>
54 #include <rte_config.h>
55
56 struct rte_log_dynamic_type;
57
58 /** The rte_log structure. */
59 struct rte_logs {
60         uint32_t type;  /**< Bitfield with enabled logs. */
61         uint32_t level; /**< Log level. */
62         FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
63         size_t dynamic_types_len;
64         struct rte_log_dynamic_type *dynamic_types;
65 };
66
67 /** Global log informations */
68 extern struct rte_logs rte_logs;
69
70 /* SDK log type */
71 #define RTE_LOGTYPE_EAL        0 /**< Log related to eal. */
72 #define RTE_LOGTYPE_MALLOC     1 /**< Log related to malloc. */
73 #define RTE_LOGTYPE_RING       2 /**< Log related to ring. */
74 #define RTE_LOGTYPE_MEMPOOL    3 /**< Log related to mempool. */
75 #define RTE_LOGTYPE_TIMER      4 /**< Log related to timers. */
76 #define RTE_LOGTYPE_PMD        5 /**< Log related to poll mode driver. */
77 #define RTE_LOGTYPE_HASH       6 /**< Log related to hash table. */
78 #define RTE_LOGTYPE_LPM        7 /**< Log related to LPM. */
79 #define RTE_LOGTYPE_KNI        8 /**< Log related to KNI. */
80 #define RTE_LOGTYPE_ACL        9 /**< Log related to ACL. */
81 #define RTE_LOGTYPE_POWER     10 /**< Log related to power. */
82 #define RTE_LOGTYPE_METER     11 /**< Log related to QoS meter. */
83 #define RTE_LOGTYPE_SCHED     12 /**< Log related to QoS port scheduler. */
84 #define RTE_LOGTYPE_PORT      13 /**< Log related to port. */
85 #define RTE_LOGTYPE_TABLE     14 /**< Log related to table. */
86 #define RTE_LOGTYPE_PIPELINE  15 /**< Log related to pipeline. */
87 #define RTE_LOGTYPE_MBUF      16 /**< Log related to mbuf. */
88 #define RTE_LOGTYPE_CRYPTODEV 17 /**< Log related to cryptodev. */
89 #define RTE_LOGTYPE_EFD       18 /**< Log related to EFD. */
90 #define RTE_LOGTYPE_EVENTDEV  19 /**< Log related to eventdev. */
91 #define RTE_LOGTYPE_GSO       20 /**< Log related to GSO. */
92
93 /* these log types can be used in an application */
94 #define RTE_LOGTYPE_USER1     24 /**< User-defined log type 1. */
95 #define RTE_LOGTYPE_USER2     25 /**< User-defined log type 2. */
96 #define RTE_LOGTYPE_USER3     26 /**< User-defined log type 3. */
97 #define RTE_LOGTYPE_USER4     27 /**< User-defined log type 4. */
98 #define RTE_LOGTYPE_USER5     28 /**< User-defined log type 5. */
99 #define RTE_LOGTYPE_USER6     29 /**< User-defined log type 6. */
100 #define RTE_LOGTYPE_USER7     30 /**< User-defined log type 7. */
101 #define RTE_LOGTYPE_USER8     31 /**< User-defined log type 8. */
102
103 /** First identifier for extended logs */
104 #define RTE_LOGTYPE_FIRST_EXT_ID 32
105
106 /* Can't use 0, as it gives compiler warnings */
107 #define RTE_LOG_EMERG    1U  /**< System is unusable.               */
108 #define RTE_LOG_ALERT    2U  /**< Action must be taken immediately. */
109 #define RTE_LOG_CRIT     3U  /**< Critical conditions.              */
110 #define RTE_LOG_ERR      4U  /**< Error conditions.                 */
111 #define RTE_LOG_WARNING  5U  /**< Warning conditions.               */
112 #define RTE_LOG_NOTICE   6U  /**< Normal but significant condition. */
113 #define RTE_LOG_INFO     7U  /**< Informational.                    */
114 #define RTE_LOG_DEBUG    8U  /**< Debug-level messages.             */
115
116 /**
117  * Change the stream that will be used by the logging system.
118  *
119  * This can be done at any time. The f argument represents the stream
120  * to be used to send the logs. If f is NULL, the default output is
121  * used (stderr).
122  *
123  * @param f
124  *   Pointer to the stream.
125  * @return
126  *   - 0 on success.
127  *   - Negative on error.
128  */
129 int rte_openlog_stream(FILE *f);
130
131 /**
132  * Set the global log level.
133  *
134  * After this call, logs with a level lower or equal than the level
135  * passed as argument will be displayed.
136  *
137  * @param level
138  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
139  */
140 void rte_log_set_global_level(uint32_t level);
141
142 /**
143  * Get the global log level.
144  *
145  * @return
146  *   The current global log level.
147  */
148 uint32_t rte_log_get_global_level(void);
149
150 /**
151  * Get the log level for a given type.
152  *
153  * @param logtype
154  *   The log type identifier.
155  * @return
156  *   0 on success, a negative value if logtype is invalid.
157  */
158 int rte_log_get_level(uint32_t logtype);
159
160 /**
161  * Set the log level for a given type.
162  *
163  * @param pattern
164  *   The regexp identifying the log type.
165  * @param level
166  *   The level to be set.
167  * @return
168  *   0 on success, a negative value if level is invalid.
169  */
170 int rte_log_set_level_regexp(const char *pattern, uint32_t level);
171
172 /**
173  * Set the log level for a given type.
174  *
175  * @param logtype
176  *   The log type identifier.
177  * @param level
178  *   The level to be set.
179  * @return
180  *   0 on success, a negative value if logtype or level is invalid.
181  */
182 int rte_log_set_level(uint32_t logtype, uint32_t level);
183
184 /**
185  * Get the current loglevel for the message being processed.
186  *
187  * Before calling the user-defined stream for logging, the log
188  * subsystem sets a per-lcore variable containing the loglevel and the
189  * logtype of the message being processed. This information can be
190  * accessed by the user-defined log output function through this
191  * function.
192  *
193  * @return
194  *   The loglevel of the message being processed.
195  */
196 int rte_log_cur_msg_loglevel(void);
197
198 /**
199  * Get the current logtype for the message being processed.
200  *
201  * Before calling the user-defined stream for logging, the log
202  * subsystem sets a per-lcore variable containing the loglevel and the
203  * logtype of the message being processed. This information can be
204  * accessed by the user-defined log output function through this
205  * function.
206  *
207  * @return
208  *   The logtype of the message being processed.
209  */
210 int rte_log_cur_msg_logtype(void);
211
212 /**
213  * Register a dynamic log type
214  *
215  * If a log is already registered with the same type, the returned value
216  * is the same than the previous one.
217  *
218  * @param name
219  *   The string identifying the log type.
220  * @return
221  *   - >0: success, the returned value is the log type identifier.
222  *   - (-ENOMEM): cannot allocate memory.
223  */
224 int rte_log_register(const char *name);
225
226 /**
227  * Dump log information.
228  *
229  * Dump the global level and the registered log types.
230  *
231  * @param f
232  *   The output stream where the dump should be sent.
233  */
234 void rte_log_dump(FILE *f);
235
236 /**
237  * Generates a log message.
238  *
239  * The message will be sent in the stream defined by the previous call
240  * to rte_openlog_stream().
241  *
242  * The level argument determines if the log should be displayed or
243  * not, depending on the global rte_logs variable.
244  *
245  * The preferred alternative is the RTE_LOG() because it adds the
246  * level and type in the logged string.
247  *
248  * @param level
249  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
250  * @param logtype
251  *   The log type, for example, RTE_LOGTYPE_EAL.
252  * @param format
253  *   The format string, as in printf(3), followed by the variable arguments
254  *   required by the format.
255  * @return
256  *   - 0: Success.
257  *   - Negative on error.
258  */
259 int rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
260 #ifdef __GNUC__
261 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
262         __attribute__((cold))
263 #endif
264 #endif
265         __attribute__((format(printf, 3, 4)));
266
267 /**
268  * Generates a log message.
269  *
270  * The message will be sent in the stream defined by the previous call
271  * to rte_openlog_stream().
272  *
273  * The level argument determines if the log should be displayed or
274  * not, depending on the global rte_logs variable. A trailing
275  * newline may be added if needed.
276  *
277  * The preferred alternative is the RTE_LOG() because it adds the
278  * level and type in the logged string.
279  *
280  * @param level
281  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
282  * @param logtype
283  *   The log type, for example, RTE_LOGTYPE_EAL.
284  * @param format
285  *   The format string, as in printf(3), followed by the variable arguments
286  *   required by the format.
287  * @param ap
288  *   The va_list of the variable arguments required by the format.
289  * @return
290  *   - 0: Success.
291  *   - Negative on error.
292  */
293 int rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
294         __attribute__((format(printf,3,0)));
295
296 /**
297  * Generates a log message.
298  *
299  * The RTE_LOG() is a helper that prefixes the string with the log level
300  * and type, and call rte_log().
301  *
302  * @param l
303  *   Log level. A value between EMERG (1) and DEBUG (8). The short name is
304  *   expanded by the macro, so it cannot be an integer value.
305  * @param t
306  *   The log type, for example, EAL. The short name is expanded by the
307  *   macro, so it cannot be an integer value.
308  * @param ...
309  *   The fmt string, as in printf(3), followed by the variable arguments
310  *   required by the format.
311  * @return
312  *   - 0: Success.
313  *   - Negative on error.
314  */
315 #define RTE_LOG(l, t, ...)                                      \
316          rte_log(RTE_LOG_ ## l,                                 \
317                  RTE_LOGTYPE_ ## t, # t ": " __VA_ARGS__)
318
319 /**
320  * Generates a log message for data path.
321  *
322  * Similar to RTE_LOG(), except that it is removed at compilation time
323  * if the RTE_LOG_DP_LEVEL configuration option is lower than the log
324  * level argument.
325  *
326  * @param l
327  *   Log level. A value between EMERG (1) and DEBUG (8). The short name is
328  *   expanded by the macro, so it cannot be an integer value.
329  * @param t
330  *   The log type, for example, EAL. The short name is expanded by the
331  *   macro, so it cannot be an integer value.
332  * @param ...
333  *   The fmt string, as in printf(3), followed by the variable arguments
334  *   required by the format.
335  * @return
336  *   - 0: Success.
337  *   - Negative on error.
338  */
339 #define RTE_LOG_DP(l, t, ...)                                   \
340         (void)((RTE_LOG_ ## l <= RTE_LOG_DP_LEVEL) ?            \
341          rte_log(RTE_LOG_ ## l,                                 \
342                  RTE_LOGTYPE_ ## t, # t ": " __VA_ARGS__) :     \
343          0)
344
345 #ifdef __cplusplus
346 }
347 #endif
348
349 #endif /* _RTE_LOG_H_ */