Imported Upstream version 16.07-rc1
[deb_dpdk.git] / lib / librte_eal / common / include / rte_log.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include "rte_common.h" /* for __rte_deprecated macro */
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <stdarg.h>
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;     /**< Pointer to current FILE* for logs. */
60 };
61
62 /** Global log informations */
63 extern struct rte_logs rte_logs;
64
65 /* SDK log type */
66 #define RTE_LOGTYPE_EAL     0x00000001 /**< Log related to eal. */
67 #define RTE_LOGTYPE_MALLOC  0x00000002 /**< Log related to malloc. */
68 #define RTE_LOGTYPE_RING    0x00000004 /**< Log related to ring. */
69 #define RTE_LOGTYPE_MEMPOOL 0x00000008 /**< Log related to mempool. */
70 #define RTE_LOGTYPE_TIMER   0x00000010 /**< Log related to timers. */
71 #define RTE_LOGTYPE_PMD     0x00000020 /**< Log related to poll mode driver. */
72 #define RTE_LOGTYPE_HASH    0x00000040 /**< Log related to hash table. */
73 #define RTE_LOGTYPE_LPM     0x00000080 /**< Log related to LPM. */
74 #define RTE_LOGTYPE_KNI     0x00000100 /**< Log related to KNI. */
75 #define RTE_LOGTYPE_ACL     0x00000200 /**< Log related to ACL. */
76 #define RTE_LOGTYPE_POWER   0x00000400 /**< Log related to power. */
77 #define RTE_LOGTYPE_METER   0x00000800 /**< Log related to QoS meter. */
78 #define RTE_LOGTYPE_SCHED   0x00001000 /**< Log related to QoS port scheduler. */
79 #define RTE_LOGTYPE_PORT    0x00002000 /**< Log related to port. */
80 #define RTE_LOGTYPE_TABLE   0x00004000 /**< Log related to table. */
81 #define RTE_LOGTYPE_PIPELINE 0x00008000 /**< Log related to pipeline. */
82 #define RTE_LOGTYPE_MBUF    0x00010000 /**< Log related to mbuf. */
83 #define RTE_LOGTYPE_CRYPTODEV 0x00020000 /**< Log related to cryptodev. */
84
85 /* these log types can be used in an application */
86 #define RTE_LOGTYPE_USER1   0x01000000 /**< User-defined log type 1. */
87 #define RTE_LOGTYPE_USER2   0x02000000 /**< User-defined log type 2. */
88 #define RTE_LOGTYPE_USER3   0x04000000 /**< User-defined log type 3. */
89 #define RTE_LOGTYPE_USER4   0x08000000 /**< User-defined log type 4. */
90 #define RTE_LOGTYPE_USER5   0x10000000 /**< User-defined log type 5. */
91 #define RTE_LOGTYPE_USER6   0x20000000 /**< User-defined log type 6. */
92 #define RTE_LOGTYPE_USER7   0x40000000 /**< User-defined log type 7. */
93 #define RTE_LOGTYPE_USER8   0x80000000 /**< User-defined log type 8. */
94
95 /* Can't use 0, as it gives compiler warnings */
96 #define RTE_LOG_EMERG    1U  /**< System is unusable.               */
97 #define RTE_LOG_ALERT    2U  /**< Action must be taken immediately. */
98 #define RTE_LOG_CRIT     3U  /**< Critical conditions.              */
99 #define RTE_LOG_ERR      4U  /**< Error conditions.                 */
100 #define RTE_LOG_WARNING  5U  /**< Warning conditions.               */
101 #define RTE_LOG_NOTICE   6U  /**< Normal but significant condition. */
102 #define RTE_LOG_INFO     7U  /**< Informational.                    */
103 #define RTE_LOG_DEBUG    8U  /**< Debug-level messages.             */
104
105 /** The default log stream. */
106 extern FILE *eal_default_log_stream;
107
108 /**
109  * Change the stream that will be used by the logging system.
110  *
111  * This can be done at any time. The f argument represents the stream
112  * to be used to send the logs. If f is NULL, the default output is
113  * used (stderr).
114  *
115  * @param f
116  *   Pointer to the stream.
117  * @return
118  *   - 0 on success.
119  *   - Negative on error.
120  */
121 int rte_openlog_stream(FILE *f);
122
123 /**
124  * Set the global log level.
125  *
126  * After this call, all logs that are lower or equal than level and
127  * lower or equal than the RTE_LOG_LEVEL configuration option will be
128  * displayed.
129  *
130  * @param level
131  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
132  */
133 void rte_set_log_level(uint32_t level);
134
135 /**
136  * Get the global log level.
137  */
138 uint32_t rte_get_log_level(void);
139
140 /**
141  * Enable or disable the log type.
142  *
143  * @param type
144  *   Log type, for example, RTE_LOGTYPE_EAL.
145  * @param enable
146  *   True for enable; false for disable.
147  */
148 void rte_set_log_type(uint32_t type, int enable);
149
150 /**
151  * Get the global log type.
152  */
153 uint32_t rte_get_log_type(void);
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  * @deprecated
185  * Enable or disable the history (enabled by default)
186  *
187  * @param enable
188  *   true to enable, or 0 to disable history.
189  */
190 __rte_deprecated
191 void rte_log_set_history(int enable);
192
193 /**
194  * @deprecated
195  * Dump the log history to a file
196  *
197  * @param f
198  *   A pointer to a file for output
199  */
200 __rte_deprecated
201 void rte_log_dump_history(FILE *f);
202
203 /**
204  * @deprecated
205  * Add a log message to the history.
206  *
207  * This function can be called from a user-defined log stream. It adds
208  * the given message in the history that can be dumped using
209  * rte_log_dump_history().
210  *
211  * @param buf
212  *   A data buffer containing the message to be saved in the history.
213  * @param size
214  *   The length of the data buffer.
215  * @return
216  *   - 0: Success.
217  *   - (-ENOBUFS) if there is no room to store the message.
218  */
219 __rte_deprecated
220 int rte_log_add_in_history(const char *buf, size_t size);
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() function because debug logs may
232  * be removed at compilation time if optimization is enabled. Moreover,
233  * logs are automatically prefixed by type when using the macro.
234  *
235  * @param level
236  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
237  * @param logtype
238  *   The log type, for example, RTE_LOGTYPE_EAL.
239  * @param format
240  *   The format string, as in printf(3), followed by the variable arguments
241  *   required by the format.
242  * @return
243  *   - 0: Success.
244  *   - Negative on error.
245  */
246 int rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
247 #ifdef __GNUC__
248 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
249         __attribute__((cold))
250 #endif
251 #endif
252         __attribute__((format(printf, 3, 4)));
253
254 /**
255  * Generates a log message.
256  *
257  * The message will be sent in the stream defined by the previous call
258  * to rte_openlog_stream().
259  *
260  * The level argument determines if the log should be displayed or
261  * not, depending on the global rte_logs variable. A trailing
262  * newline may be added if needed.
263  *
264  * The preferred alternative is the RTE_LOG() because debug logs may be
265  * removed at compilation time.
266  *
267  * @param level
268  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
269  * @param logtype
270  *   The log type, for example, RTE_LOGTYPE_EAL.
271  * @param format
272  *   The format string, as in printf(3), followed by the variable arguments
273  *   required by the format.
274  * @param ap
275  *   The va_list of the variable arguments required by the format.
276  * @return
277  *   - 0: Success.
278  *   - Negative on error.
279  */
280 int rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
281         __attribute__((format(printf,3,0)));
282
283 /**
284  * Generates a log message.
285  *
286  * The RTE_LOG() is equivalent to rte_log() with two differences:
287
288  * - RTE_LOG() can be used to remove debug logs at compilation time,
289  *   depending on RTE_LOG_LEVEL configuration option, and compilation
290  *   optimization level. If optimization is enabled, the tests
291  *   involving constants only are pre-computed. If compilation is done
292  *   with -O0, these tests will be done at run time.
293  * - The log level and log type names are smaller, for example:
294  *   RTE_LOG(INFO, EAL, "this is a %s", "log");
295  *
296  * @param l
297  *   Log level. A value between EMERG (1) and DEBUG (8). The short name is
298  *   expanded by the macro, so it cannot be an integer value.
299  * @param t
300  *   The log type, for example, EAL. The short name is expanded by the
301  *   macro, so it cannot be an integer value.
302  * @param ...
303  *   The fmt string, as in printf(3), followed by the variable arguments
304  *   required by the format.
305  * @return
306  *   - 0: Success.
307  *   - Negative on error.
308  */
309 #define RTE_LOG(l, t, ...)                                      \
310         (void)((RTE_LOG_ ## l <= RTE_LOG_LEVEL) ?               \
311          rte_log(RTE_LOG_ ## l,                                 \
312                  RTE_LOGTYPE_ ## t, # t ": " __VA_ARGS__) :     \
313          0)
314
315 #ifdef __cplusplus
316 }
317 #endif
318
319 #endif /* _RTE_LOG_H_ */