efd: new Elastic Flow Distributor library
[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 /** The rte_log structure. */
54 struct rte_logs {
55         uint32_t type;  /**< Bitfield with enabled logs. */
56         uint32_t level; /**< Log level. */
57         FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
58 };
59
60 /** Global log informations */
61 extern struct rte_logs rte_logs;
62
63 /* SDK log type */
64 #define RTE_LOGTYPE_EAL     0x00000001 /**< Log related to eal. */
65 #define RTE_LOGTYPE_MALLOC  0x00000002 /**< Log related to malloc. */
66 #define RTE_LOGTYPE_RING    0x00000004 /**< Log related to ring. */
67 #define RTE_LOGTYPE_MEMPOOL 0x00000008 /**< Log related to mempool. */
68 #define RTE_LOGTYPE_TIMER   0x00000010 /**< Log related to timers. */
69 #define RTE_LOGTYPE_PMD     0x00000020 /**< Log related to poll mode driver. */
70 #define RTE_LOGTYPE_HASH    0x00000040 /**< Log related to hash table. */
71 #define RTE_LOGTYPE_LPM     0x00000080 /**< Log related to LPM. */
72 #define RTE_LOGTYPE_KNI     0x00000100 /**< Log related to KNI. */
73 #define RTE_LOGTYPE_ACL     0x00000200 /**< Log related to ACL. */
74 #define RTE_LOGTYPE_POWER   0x00000400 /**< Log related to power. */
75 #define RTE_LOGTYPE_METER   0x00000800 /**< Log related to QoS meter. */
76 #define RTE_LOGTYPE_SCHED   0x00001000 /**< Log related to QoS port scheduler. */
77 #define RTE_LOGTYPE_PORT    0x00002000 /**< Log related to port. */
78 #define RTE_LOGTYPE_TABLE   0x00004000 /**< Log related to table. */
79 #define RTE_LOGTYPE_PIPELINE 0x00008000 /**< Log related to pipeline. */
80 #define RTE_LOGTYPE_MBUF    0x00010000 /**< Log related to mbuf. */
81 #define RTE_LOGTYPE_CRYPTODEV 0x00020000 /**< Log related to cryptodev. */
82 #define RTE_LOGTYPE_EFD     0x00040000 /**< Log related to EFD. */
83
84 /* these log types can be used in an application */
85 #define RTE_LOGTYPE_USER1   0x01000000 /**< User-defined log type 1. */
86 #define RTE_LOGTYPE_USER2   0x02000000 /**< User-defined log type 2. */
87 #define RTE_LOGTYPE_USER3   0x04000000 /**< User-defined log type 3. */
88 #define RTE_LOGTYPE_USER4   0x08000000 /**< User-defined log type 4. */
89 #define RTE_LOGTYPE_USER5   0x10000000 /**< User-defined log type 5. */
90 #define RTE_LOGTYPE_USER6   0x20000000 /**< User-defined log type 6. */
91 #define RTE_LOGTYPE_USER7   0x40000000 /**< User-defined log type 7. */
92 #define RTE_LOGTYPE_USER8   0x80000000 /**< User-defined log type 8. */
93
94 /* Can't use 0, as it gives compiler warnings */
95 #define RTE_LOG_EMERG    1U  /**< System is unusable.               */
96 #define RTE_LOG_ALERT    2U  /**< Action must be taken immediately. */
97 #define RTE_LOG_CRIT     3U  /**< Critical conditions.              */
98 #define RTE_LOG_ERR      4U  /**< Error conditions.                 */
99 #define RTE_LOG_WARNING  5U  /**< Warning conditions.               */
100 #define RTE_LOG_NOTICE   6U  /**< Normal but significant condition. */
101 #define RTE_LOG_INFO     7U  /**< Informational.                    */
102 #define RTE_LOG_DEBUG    8U  /**< Debug-level messages.             */
103
104 /**
105  * Change the stream that will be used by the logging system.
106  *
107  * This can be done at any time. The f argument represents the stream
108  * to be used to send the logs. If f is NULL, the default output is
109  * used (stderr).
110  *
111  * @param f
112  *   Pointer to the stream.
113  * @return
114  *   - 0 on success.
115  *   - Negative on error.
116  */
117 int rte_openlog_stream(FILE *f);
118
119 /**
120  * Set the global log level.
121  *
122  * After this call, logs with a level lower or equal than the level
123  * passed as argument will be displayed.
124  *
125  * @param level
126  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
127  */
128 void rte_set_log_level(uint32_t level);
129
130 /**
131  * Get the global log level.
132  */
133 uint32_t rte_get_log_level(void);
134
135 /**
136  * Enable or disable the log type.
137  *
138  * @param type
139  *   Log type, for example, RTE_LOGTYPE_EAL.
140  * @param enable
141  *   True for enable; false for disable.
142  */
143 void rte_set_log_type(uint32_t type, int enable);
144
145 /**
146  * Get the global log type.
147  */
148 uint32_t rte_get_log_type(void);
149
150 /**
151  * Get the current loglevel for the message being processed.
152  *
153  * Before calling the user-defined stream for logging, the log
154  * subsystem sets a per-lcore variable containing the loglevel and the
155  * logtype of the message being processed. This information can be
156  * accessed by the user-defined log output function through this
157  * function.
158  *
159  * @return
160  *   The loglevel of the message being processed.
161  */
162 int rte_log_cur_msg_loglevel(void);
163
164 /**
165  * Get the current logtype for the message being processed.
166  *
167  * Before calling the user-defined stream for logging, the log
168  * subsystem sets a per-lcore variable containing the loglevel and the
169  * logtype of the message being processed. This information can be
170  * accessed by the user-defined log output function through this
171  * function.
172  *
173  * @return
174  *   The logtype of the message being processed.
175  */
176 int rte_log_cur_msg_logtype(void);
177
178 /**
179  * Generates a log message.
180  *
181  * The message will be sent in the stream defined by the previous call
182  * to rte_openlog_stream().
183  *
184  * The level argument determines if the log should be displayed or
185  * not, depending on the global rte_logs variable.
186  *
187  * The preferred alternative is the RTE_LOG() because it adds the
188  * level and type in the logged string.
189  *
190  * @param level
191  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
192  * @param logtype
193  *   The log type, for example, RTE_LOGTYPE_EAL.
194  * @param format
195  *   The format string, as in printf(3), followed by the variable arguments
196  *   required by the format.
197  * @return
198  *   - 0: Success.
199  *   - Negative on error.
200  */
201 int rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
202 #ifdef __GNUC__
203 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
204         __attribute__((cold))
205 #endif
206 #endif
207         __attribute__((format(printf, 3, 4)));
208
209 /**
210  * Generates a log message.
211  *
212  * The message will be sent in the stream defined by the previous call
213  * to rte_openlog_stream().
214  *
215  * The level argument determines if the log should be displayed or
216  * not, depending on the global rte_logs variable. A trailing
217  * newline may be added if needed.
218  *
219  * The preferred alternative is the RTE_LOG() because it adds the
220  * level and type in the logged string.
221  *
222  * @param level
223  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
224  * @param logtype
225  *   The log type, for example, RTE_LOGTYPE_EAL.
226  * @param format
227  *   The format string, as in printf(3), followed by the variable arguments
228  *   required by the format.
229  * @param ap
230  *   The va_list of the variable arguments required by the format.
231  * @return
232  *   - 0: Success.
233  *   - Negative on error.
234  */
235 int rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
236         __attribute__((format(printf,3,0)));
237
238 /**
239  * Generates a log message.
240  *
241  * The RTE_LOG() is a helper that prefixes the string with the log level
242  * and type, and call rte_log().
243  *
244  * @param l
245  *   Log level. A value between EMERG (1) and DEBUG (8). The short name is
246  *   expanded by the macro, so it cannot be an integer value.
247  * @param t
248  *   The log type, for example, EAL. The short name is expanded by the
249  *   macro, so it cannot be an integer value.
250  * @param ...
251  *   The fmt string, as in printf(3), followed by the variable arguments
252  *   required by the format.
253  * @return
254  *   - 0: Success.
255  *   - Negative on error.
256  */
257 #define RTE_LOG(l, t, ...)                                      \
258          rte_log(RTE_LOG_ ## l,                                 \
259                  RTE_LOGTYPE_ ## t, # t ": " __VA_ARGS__)
260
261 /**
262  * Generates a log message for data path.
263  *
264  * Similar to RTE_LOG(), except that it is removed at compilation time
265  * if the RTE_LOG_DP_LEVEL configuration option is lower than the log
266  * level argument.
267  *
268  * @param l
269  *   Log level. A value between EMERG (1) and DEBUG (8). The short name is
270  *   expanded by the macro, so it cannot be an integer value.
271  * @param t
272  *   The log type, for example, EAL. The short name is expanded by the
273  *   macro, so it cannot be an integer value.
274  * @param ...
275  *   The fmt string, as in printf(3), followed by the variable arguments
276  *   required by the format.
277  * @return
278  *   - 0: Success.
279  *   - Negative on error.
280  */
281 #define RTE_LOG_DP(l, t, ...)                                   \
282         (void)((RTE_LOG_ ## l <= RTE_LOG_DP_LEVEL) ?            \
283          rte_log(RTE_LOG_ ## l,                                 \
284                  RTE_LOGTYPE_ ## t, # t ": " __VA_ARGS__) :     \
285          0)
286
287 #ifdef __cplusplus
288 }
289 #endif
290
291 #endif /* _RTE_LOG_H_ */