first public release
[dpdk.git] / lib / librte_eal / common / include / rte_log.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #ifndef _RTE_LOG_H_
37 #define _RTE_LOG_H_
38
39 /**
40  * @file
41  *
42  * RTE Logs API
43  *
44  * This file provides a log API to RTE applications.
45  */
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
75 /* these log types can be used in an application */
76 #define RTE_LOGTYPE_USER1   0x01000000 /**< User-defined log type 1. */
77 #define RTE_LOGTYPE_USER2   0x02000000 /**< User-defined log type 2. */
78 #define RTE_LOGTYPE_USER3   0x04000000 /**< User-defined log type 3. */
79 #define RTE_LOGTYPE_USER4   0x08000000 /**< User-defined log type 4. */
80 #define RTE_LOGTYPE_USER5   0x10000000 /**< User-defined log type 5. */
81 #define RTE_LOGTYPE_USER6   0x20000000 /**< User-defined log type 6. */
82 #define RTE_LOGTYPE_USER7   0x40000000 /**< User-defined log type 7. */
83 #define RTE_LOGTYPE_USER8   0x80000000 /**< User-defined log type 8. */
84
85 /* Can't use 0, as it gives compiler warnings */
86 #define RTE_LOG_EMERG    1U  /**< System is unusable.               */
87 #define RTE_LOG_ALERT    2U  /**< Action must be taken immediately. */
88 #define RTE_LOG_CRIT     3U  /**< Critical conditions.              */
89 #define RTE_LOG_ERR      4U  /**< Error conditions.                 */
90 #define RTE_LOG_WARNING  5U  /**< Warning conditions.               */
91 #define RTE_LOG_NOTICE   6U  /**< Normal but significant condition. */
92 #define RTE_LOG_INFO     7U  /**< Informational.                    */
93 #define RTE_LOG_DEBUG    8U  /**< Debug-level messages.             */
94
95 /** The default log stream. */
96 extern FILE *eal_default_log_stream;
97
98 /**
99  * Change the stream that will be used by the logging system.
100  *
101  * This can be done at any time. The f argument represents the stream
102  * to be used to send the logs. If f is NULL, the default output is
103  * used, which is the serial line in case of bare metal, or directly
104  * sent to syslog in case of linux application.
105  *
106  * @param f
107  *   Pointer to the stream.
108  * @return
109  *   - 0 on success.
110  *   - Negative on error.
111  */
112 int rte_openlog_stream(FILE *f);
113
114 /**
115  * Set the global log level.
116  *
117  * After this call, all logs that are lower or equal than level and
118  * lower or equal than the RTE_LOG_LEVEL configuration option will be
119  * displayed.
120  *
121  * @param level
122  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
123  */
124 void rte_set_log_level(uint32_t level);
125
126 /**
127  * Enable or disable the log type.
128  *
129  * @param type
130  *   Log type, for example, RTE_LOGTYPE_EAL.
131  * @param enable
132  *   True for enable; false for disable.
133  */
134 void rte_set_log_type(uint32_t type, int enable);
135
136 /**
137  * Get the current loglevel for the message being processed.
138  *
139  * Before calling the user-defined stream for logging, the log
140  * subsystem sets a per-lcore variable containing the loglevel and the
141  * logtype of the message being processed. This information can be
142  * accessed by the user-defined log output function through this
143  * function.
144  *
145  * @return
146  *   The loglevel of the message being processed.
147  */
148 int rte_log_cur_msg_loglevel(void);
149
150 /**
151  * Get the current logtype 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 logtype of the message being processed.
161  */
162 int rte_log_cur_msg_logtype(void);
163
164 /**
165  * Enable or disable the history (enabled by default)
166  *
167  * @param enable
168  *   true to enable, or 0 to disable history.
169  */
170 void rte_log_set_history(int enable);
171
172 /**
173  * Dump the log history to the console.
174  */
175 void rte_log_dump_history(void);
176
177 /**
178  * Add a log message to the history.
179  *
180  * This function can be called from a user-defined log stream. It adds
181  * the given message in the history that can be dumped using
182  * rte_log_dump_history().
183  *
184  * @param buf
185  *   A data buffer containing the message to be saved in the history.
186  * @param size
187  *   The length of the data buffer.
188  * @return
189  *   - 0: Success.
190  *   - (-ENOBUFS) if there is no room to store the message.
191  */
192 int rte_log_add_in_history(const char *buf, size_t size);
193
194 /**
195  * Generates a log message.
196  *
197  * The message will be sent in the stream defined by the previous call
198  * to rte_openlog_stream().
199  *
200  * The level argument determines if the log should be displayed or
201  * not, depending on the global rte_logs variable.
202  *
203  * The preferred alternative is the RTE_LOG() function because debug logs may
204  * be removed at compilation time if optimization is enabled. Moreover,
205  * logs are automatically prefixed by type when using the macro.
206  *
207  * @param level
208  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
209  * @param logtype
210  *   The log type, for example, RTE_LOGTYPE_EAL.
211  * @param format
212  *   The format string, as in printf(3), followed by the variable arguments
213  *   required by the format.
214  * @return
215  *   - 0: Success.
216  *   - Negative on error.
217  */
218 int rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
219         __attribute__((format(printf, 3, 4)));
220
221 /**
222  * Generates a log message.
223  *
224  * The message will be sent in the stream defined by the previous call
225  * to rte_openlog_stream().
226  *
227  * The level argument determines if the log should be displayed or
228  * not, depending on the global rte_logs variable. A trailing
229  * newline may be added if needed.
230  *
231  * The preferred alternative is the RTE_LOG() because debug logs may be
232  * removed at compilation time.
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  * @param ap
242  *   The va_list of the variable arguments required by the format.
243  * @return
244  *   - 0: Success.
245  *   - Negative on error.
246  */
247 int rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap);
248
249 /**
250  * Generates a log message.
251  *
252  * The RTE_LOG() is equivalent to rte_log() with two differences:
253
254  * - RTE_LOG() can be used to remove debug logs at compilation time,
255  *   depending on RTE_LOG_LEVEL configuration option, and compilation
256  *   optimization level. If optimization is enabled, the tests
257  *   involving constants only are pre-computed. If compilation is done
258  *   with -O0, these tests will be done at run time.
259  * - The log level and log type names are smaller, for example:
260  *   RTE_LOG(INFO, EAL, "this is a %s", "log");
261  *
262  * @param l
263  *   Log level. A value between EMERG (1) and DEBUG (8). The short name is
264  *   expanded by the macro, so it cannot be an integer value.
265  * @param t
266  *   The log type, for example, EAL. The short name is expanded by the
267  *   macro, so it cannot be an integer value.
268  * @param fmt
269  *   The fmt string, as in printf(3), followed by the variable arguments
270  *   required by the format.
271  * @param args
272  *   The variable list of arguments according to the format string.
273  * @return
274  *   - 0: Success.
275  *   - Negative on error.
276  */
277 #define RTE_LOG(l, t, fmt, args...) ({                                  \
278         if ((RTE_LOG_##l <= RTE_LOG_LEVEL) &&                           \
279             (RTE_LOG_##l <= rte_logs.level) &&                          \
280             (RTE_LOGTYPE_##t & rte_logs.type)) {                        \
281                 rte_log(RTE_LOG_##l, RTE_LOGTYPE_##t,                   \
282                           #t ": " fmt, ## args);                        \
283         }                                                               \
284 })
285
286 #ifdef __cplusplus
287 }
288 #endif
289
290 #endif /* _RTE_LOG_H_ */