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