4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
44 #include <rte_per_lcore.h>
46 #include "eal_private.h"
48 /* global log structure */
49 struct rte_logs rte_logs = {
51 .level = RTE_LOG_DEBUG,
55 /* Stream to use for logging if rte_logs.file is NULL */
56 static FILE *default_log_stream;
59 * This global structure stores some informations about the message
60 * that is currently being processed by one lcore
63 uint32_t loglevel; /**< log level - see rte_log.h */
64 uint32_t logtype; /**< log type - see rte_log.h */
67 struct rte_log_dynamic_type {
73 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
77 /* Change the stream that will be used by logging system */
79 rte_openlog_stream(FILE *f)
85 /* Set global log level */
87 rte_log_set_global_level(uint32_t level)
89 rte_logs.level = (uint32_t)level;
92 /* Set global log level */
93 /* replaced by rte_log_set_global_level */
95 rte_set_log_level(uint32_t level)
97 rte_log_set_global_level(level);
100 /* Get global log level */
102 rte_log_get_global_level(void)
104 return rte_logs.level;
107 /* Get global log level */
108 /* replaced by rte_log_get_global_level */
110 rte_get_log_level(void)
112 return rte_log_get_global_level();
115 /* Set global log type */
116 __rte_deprecated void
117 rte_set_log_type(uint32_t type, int enable)
119 if (type < RTE_LOGTYPE_FIRST_EXT_ID) {
121 rte_logs.type |= type;
123 rte_logs.type &= ~type;
127 rte_log_set_level(type, 0);
129 rte_log_set_level(type, RTE_LOG_DEBUG);
132 /* Get global log type */
133 __rte_deprecated uint32_t
134 rte_get_log_type(void)
136 return rte_logs.type;
140 rte_log_set_level(uint32_t type, uint32_t level)
142 if (type >= rte_logs.dynamic_types_len)
144 if (level > RTE_LOG_DEBUG)
147 rte_logs.dynamic_types[type].loglevel = level;
154 rte_log_set_level_regexp(const char *pattern, uint32_t level)
159 if (level > RTE_LOG_DEBUG)
162 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
163 if (rte_logs.dynamic_types[i].name == NULL)
165 if (regexec(&r, pattern, 0, NULL, 0) == 0)
166 rte_logs.dynamic_types[i].loglevel = level;
172 /* get the current loglevel for the message beeing processed */
173 int rte_log_cur_msg_loglevel(void)
175 return RTE_PER_LCORE(log_cur_msg).loglevel;
178 /* get the current logtype for the message beeing processed */
179 int rte_log_cur_msg_logtype(void)
181 return RTE_PER_LCORE(log_cur_msg).logtype;
185 rte_log_lookup(const char *name)
189 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
190 if (rte_logs.dynamic_types[i].name == NULL)
192 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
199 /* register an extended log type, assuming table is large enough, and id
200 * is not yet registered.
203 __rte_log_register(const char *name, int id)
205 char *dup_name = strdup(name);
207 if (dup_name == NULL)
210 rte_logs.dynamic_types[id].name = dup_name;
211 rte_logs.dynamic_types[id].loglevel = RTE_LOG_DEBUG;
216 /* register an extended log type */
218 rte_log_register(const char *name)
220 struct rte_log_dynamic_type *new_dynamic_types;
223 id = rte_log_lookup(name);
227 new_dynamic_types = realloc(rte_logs.dynamic_types,
228 sizeof(struct rte_log_dynamic_type) *
229 (rte_logs.dynamic_types_len + 1));
230 if (new_dynamic_types == NULL)
232 rte_logs.dynamic_types = new_dynamic_types;
234 ret = __rte_log_register(name, rte_logs.dynamic_types_len);
238 rte_logs.dynamic_types_len++;
243 RTE_INIT(rte_log_init);
247 rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
248 sizeof(struct rte_log_dynamic_type));
249 if (rte_logs.dynamic_types == NULL)
252 /* register legacy log types, keep sync'd with RTE_LOGTYPE_* */
253 __rte_log_register("eal", 0);
254 __rte_log_register("malloc", 1);
255 __rte_log_register("ring", 2);
256 __rte_log_register("mempool", 3);
257 __rte_log_register("timer", 4);
258 __rte_log_register("pmd", 5);
259 __rte_log_register("hash", 6);
260 __rte_log_register("lpm", 7);
261 __rte_log_register("kni", 8);
262 __rte_log_register("acl", 9);
263 __rte_log_register("power", 10);
264 __rte_log_register("meter", 11);
265 __rte_log_register("sched", 12);
266 __rte_log_register("port", 13);
267 __rte_log_register("table", 14);
268 __rte_log_register("pipeline", 15);
269 __rte_log_register("mbuf", 16);
270 __rte_log_register("cryptodev", 17);
271 __rte_log_register("user1", 24);
272 __rte_log_register("user2", 25);
273 __rte_log_register("user3", 26);
274 __rte_log_register("user4", 27);
275 __rte_log_register("user5", 28);
276 __rte_log_register("user6", 29);
277 __rte_log_register("user7", 30);
278 __rte_log_register("user8", 31);
280 rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
284 loglevel_to_string(uint32_t level)
287 case RTE_LOG_EMERG: return "emerg";
288 case RTE_LOG_ALERT: return "alert";
289 case RTE_LOG_CRIT: return "critical";
290 case RTE_LOG_ERR: return "error";
291 case RTE_LOG_WARNING: return "warning";
292 case RTE_LOG_NOTICE: return "notice";
293 case RTE_LOG_INFO: return "info";
294 case RTE_LOG_DEBUG: return "debug";
295 default: return "unknown";
299 /* dump global level and registered log types */
301 rte_log_dump(FILE *f)
305 fprintf(f, "global log level is %s\n",
306 loglevel_to_string(rte_log_get_global_level()));
308 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
309 if (rte_logs.dynamic_types[i].name == NULL)
311 fprintf(f, "id %zu: %s, level is %s\n",
312 i, rte_logs.dynamic_types[i].name,
313 loglevel_to_string(rte_logs.dynamic_types[i].loglevel));
318 * Generates a log message The message will be sent in the stream
319 * defined by the previous call to rte_openlog_stream().
322 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
325 FILE *f = rte_logs.file;
327 f = default_log_stream;
330 * Grab the current value of stderr here, rather than
331 * just initializing default_log_stream to stderr. This
332 * ensures that we will always use the current value
333 * of stderr, even if the application closes and
340 if (level > rte_logs.level)
342 if (logtype >= rte_logs.dynamic_types_len)
344 if (level > rte_logs.dynamic_types[logtype].loglevel)
347 /* save loglevel and logtype in a global per-lcore variable */
348 RTE_PER_LCORE(log_cur_msg).loglevel = level;
349 RTE_PER_LCORE(log_cur_msg).logtype = logtype;
351 ret = vfprintf(f, format, ap);
357 * Generates a log message The message will be sent in the stream
358 * defined by the previous call to rte_openlog_stream().
359 * No need to check level here, done by rte_vlog().
362 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
367 va_start(ap, format);
368 ret = rte_vlog(level, logtype, format, ap);
374 * Called by environment-specific initialization functions.
377 eal_log_set_default(FILE *default_log)
379 default_log_stream = default_log;
381 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
383 "Debug dataplane logs available - lower performance\n");