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