lib: use SPDX tag for Intel copyright files
[dpdk.git] / lib / librte_eal / common / include / rte_log.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #ifndef _RTE_LOG_H_
6 #define _RTE_LOG_H_
7
8 /**
9  * @file
10  *
11  * RTE Logs API
12  *
13  * This file provides a log API to RTE applications.
14  */
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23
24 #include <rte_common.h>
25
26 struct rte_log_dynamic_type;
27
28 /** The rte_log structure. */
29 struct rte_logs {
30         uint32_t type;  /**< Bitfield with enabled logs. */
31         uint32_t level; /**< Log level. */
32         FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
33         size_t dynamic_types_len;
34         struct rte_log_dynamic_type *dynamic_types;
35 };
36
37 /** Global log informations */
38 extern struct rte_logs rte_logs;
39
40 /* SDK log type */
41 #define RTE_LOGTYPE_EAL        0 /**< Log related to eal. */
42 #define RTE_LOGTYPE_MALLOC     1 /**< Log related to malloc. */
43 #define RTE_LOGTYPE_RING       2 /**< Log related to ring. */
44 #define RTE_LOGTYPE_MEMPOOL    3 /**< Log related to mempool. */
45 #define RTE_LOGTYPE_TIMER      4 /**< Log related to timers. */
46 #define RTE_LOGTYPE_PMD        5 /**< Log related to poll mode driver. */
47 #define RTE_LOGTYPE_HASH       6 /**< Log related to hash table. */
48 #define RTE_LOGTYPE_LPM        7 /**< Log related to LPM. */
49 #define RTE_LOGTYPE_KNI        8 /**< Log related to KNI. */
50 #define RTE_LOGTYPE_ACL        9 /**< Log related to ACL. */
51 #define RTE_LOGTYPE_POWER     10 /**< Log related to power. */
52 #define RTE_LOGTYPE_METER     11 /**< Log related to QoS meter. */
53 #define RTE_LOGTYPE_SCHED     12 /**< Log related to QoS port scheduler. */
54 #define RTE_LOGTYPE_PORT      13 /**< Log related to port. */
55 #define RTE_LOGTYPE_TABLE     14 /**< Log related to table. */
56 #define RTE_LOGTYPE_PIPELINE  15 /**< Log related to pipeline. */
57 #define RTE_LOGTYPE_MBUF      16 /**< Log related to mbuf. */
58 #define RTE_LOGTYPE_CRYPTODEV 17 /**< Log related to cryptodev. */
59 #define RTE_LOGTYPE_EFD       18 /**< Log related to EFD. */
60 #define RTE_LOGTYPE_EVENTDEV  19 /**< Log related to eventdev. */
61 #define RTE_LOGTYPE_GSO       20 /**< Log related to GSO. */
62
63 /* these log types can be used in an application */
64 #define RTE_LOGTYPE_USER1     24 /**< User-defined log type 1. */
65 #define RTE_LOGTYPE_USER2     25 /**< User-defined log type 2. */
66 #define RTE_LOGTYPE_USER3     26 /**< User-defined log type 3. */
67 #define RTE_LOGTYPE_USER4     27 /**< User-defined log type 4. */
68 #define RTE_LOGTYPE_USER5     28 /**< User-defined log type 5. */
69 #define RTE_LOGTYPE_USER6     29 /**< User-defined log type 6. */
70 #define RTE_LOGTYPE_USER7     30 /**< User-defined log type 7. */
71 #define RTE_LOGTYPE_USER8     31 /**< User-defined log type 8. */
72
73 /** First identifier for extended logs */
74 #define RTE_LOGTYPE_FIRST_EXT_ID 32
75
76 /* Can't use 0, as it gives compiler warnings */
77 #define RTE_LOG_EMERG    1U  /**< System is unusable.               */
78 #define RTE_LOG_ALERT    2U  /**< Action must be taken immediately. */
79 #define RTE_LOG_CRIT     3U  /**< Critical conditions.              */
80 #define RTE_LOG_ERR      4U  /**< Error conditions.                 */
81 #define RTE_LOG_WARNING  5U  /**< Warning conditions.               */
82 #define RTE_LOG_NOTICE   6U  /**< Normal but significant condition. */
83 #define RTE_LOG_INFO     7U  /**< Informational.                    */
84 #define RTE_LOG_DEBUG    8U  /**< Debug-level messages.             */
85
86 /**
87  * Change the stream that will be used by the logging system.
88  *
89  * This can be done at any time. The f argument represents the stream
90  * to be used to send the logs. If f is NULL, the default output is
91  * used (stderr).
92  *
93  * @param f
94  *   Pointer to the stream.
95  * @return
96  *   - 0 on success.
97  *   - Negative on error.
98  */
99 int rte_openlog_stream(FILE *f);
100
101 /**
102  * Set the global log level.
103  *
104  * After this call, logs with a level lower or equal than the level
105  * passed as argument will be displayed.
106  *
107  * @param level
108  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
109  */
110 void rte_log_set_global_level(uint32_t level);
111
112 /**
113  * Get the global log level.
114  *
115  * @return
116  *   The current global log level.
117  */
118 uint32_t rte_log_get_global_level(void);
119
120 /**
121  * Get the log level for a given type.
122  *
123  * @param logtype
124  *   The log type identifier.
125  * @return
126  *   0 on success, a negative value if logtype is invalid.
127  */
128 int rte_log_get_level(uint32_t logtype);
129
130 /**
131  * Set the log level for a given type.
132  *
133  * @param pattern
134  *   The regexp identifying the log type.
135  * @param level
136  *   The level to be set.
137  * @return
138  *   0 on success, a negative value if level is invalid.
139  */
140 int rte_log_set_level_regexp(const char *pattern, uint32_t level);
141
142 /**
143  * Set the log level for a given type.
144  *
145  * @param logtype
146  *   The log type identifier.
147  * @param level
148  *   The level to be set.
149  * @return
150  *   0 on success, a negative value if logtype or level is invalid.
151  */
152 int rte_log_set_level(uint32_t logtype, uint32_t level);
153
154 /**
155  * Get the current loglevel for the message being processed.
156  *
157  * Before calling the user-defined stream for logging, the log
158  * subsystem sets a per-lcore variable containing the loglevel and the
159  * logtype of the message being processed. This information can be
160  * accessed by the user-defined log output function through this
161  * function.
162  *
163  * @return
164  *   The loglevel of the message being processed.
165  */
166 int rte_log_cur_msg_loglevel(void);
167
168 /**
169  * Get the current logtype for the message being processed.
170  *
171  * Before calling the user-defined stream for logging, the log
172  * subsystem sets a per-lcore variable containing the loglevel and the
173  * logtype of the message being processed. This information can be
174  * accessed by the user-defined log output function through this
175  * function.
176  *
177  * @return
178  *   The logtype of the message being processed.
179  */
180 int rte_log_cur_msg_logtype(void);
181
182 /**
183  * Register a dynamic log type
184  *
185  * If a log is already registered with the same type, the returned value
186  * is the same than the previous one.
187  *
188  * @param name
189  *   The string identifying the log type.
190  * @return
191  *   - >0: success, the returned value is the log type identifier.
192  *   - (-ENOMEM): cannot allocate memory.
193  */
194 int rte_log_register(const char *name);
195
196 /**
197  * Dump log information.
198  *
199  * Dump the global level and the registered log types.
200  *
201  * @param f
202  *   The output stream where the dump should be sent.
203  */
204 void rte_log_dump(FILE *f);
205
206 /**
207  * Generates a log message.
208  *
209  * The message will be sent in the stream defined by the previous call
210  * to rte_openlog_stream().
211  *
212  * The level argument determines if the log should be displayed or
213  * not, depending on the global rte_logs variable.
214  *
215  * The preferred alternative is the RTE_LOG() because it adds the
216  * level and type in the logged string.
217  *
218  * @param level
219  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
220  * @param logtype
221  *   The log type, for example, RTE_LOGTYPE_EAL.
222  * @param format
223  *   The format string, as in printf(3), followed by the variable arguments
224  *   required by the format.
225  * @return
226  *   - 0: Success.
227  *   - Negative on error.
228  */
229 int rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
230 #ifdef __GNUC__
231 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
232         __attribute__((cold))
233 #endif
234 #endif
235         __attribute__((format(printf, 3, 4)));
236
237 /**
238  * Generates a log message.
239  *
240  * The message will be sent in the stream defined by the previous call
241  * to rte_openlog_stream().
242  *
243  * The level argument determines if the log should be displayed or
244  * not, depending on the global rte_logs variable. A trailing
245  * newline may be added if needed.
246  *
247  * The preferred alternative is the RTE_LOG() because it adds the
248  * level and type in the logged string.
249  *
250  * @param level
251  *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
252  * @param logtype
253  *   The log type, for example, RTE_LOGTYPE_EAL.
254  * @param format
255  *   The format string, as in printf(3), followed by the variable arguments
256  *   required by the format.
257  * @param ap
258  *   The va_list of the variable arguments required by the format.
259  * @return
260  *   - 0: Success.
261  *   - Negative on error.
262  */
263 int rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
264         __attribute__((format(printf,3,0)));
265
266 /**
267  * Generates a log message.
268  *
269  * The RTE_LOG() is a helper that prefixes the string with the log level
270  * and type, and call rte_log().
271  *
272  * @param l
273  *   Log level. A value between EMERG (1) and DEBUG (8). The short name is
274  *   expanded by the macro, so it cannot be an integer value.
275  * @param t
276  *   The log type, for example, EAL. The short name is expanded by the
277  *   macro, so it cannot be an integer value.
278  * @param ...
279  *   The fmt string, as in printf(3), followed by the variable arguments
280  *   required by the format.
281  * @return
282  *   - 0: Success.
283  *   - Negative on error.
284  */
285 #define RTE_LOG(l, t, ...)                                      \
286          rte_log(RTE_LOG_ ## l,                                 \
287                  RTE_LOGTYPE_ ## t, # t ": " __VA_ARGS__)
288
289 /**
290  * Generates a log message for data path.
291  *
292  * Similar to RTE_LOG(), except that it is removed at compilation time
293  * if the RTE_LOG_DP_LEVEL configuration option is lower than the log
294  * level argument.
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_DP(l, t, ...)                                   \
310         (void)((RTE_LOG_ ## l <= RTE_LOG_DP_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_ */