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