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