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