1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
16 #include <rte_per_lcore.h>
18 #include "eal_private.h"
20 /* global log structure */
21 struct rte_logs rte_logs = {
23 .level = RTE_LOG_DEBUG,
27 struct rte_eal_opt_loglevel {
28 /** Next list entry */
29 TAILQ_ENTRY(rte_eal_opt_loglevel) next;
30 /** Compiled regular expression obtained from the option */
32 /** Glob match string option */
34 /** Log level value obtained from the option */
38 TAILQ_HEAD(rte_eal_opt_loglevel_list, rte_eal_opt_loglevel);
40 /** List of valid EAL log level options */
41 static struct rte_eal_opt_loglevel_list opt_loglevel_list =
42 TAILQ_HEAD_INITIALIZER(opt_loglevel_list);
44 /* Stream to use for logging if rte_logs.file is NULL */
45 static FILE *default_log_stream;
48 * This global structure stores some informations about the message
49 * that is currently being processed by one lcore
52 uint32_t loglevel; /**< log level - see rte_log.h */
53 uint32_t logtype; /**< log type - see rte_log.h */
56 struct rte_log_dynamic_type {
62 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
66 /* Change the stream that will be used by logging system */
68 rte_openlog_stream(FILE *f)
74 /* Set global log level */
76 rte_log_set_global_level(uint32_t level)
78 rte_logs.level = (uint32_t)level;
81 /* Get global log level */
83 rte_log_get_global_level(void)
85 return rte_logs.level;
89 rte_log_get_level(uint32_t type)
91 if (type >= rte_logs.dynamic_types_len)
94 return rte_logs.dynamic_types[type].loglevel;
98 rte_log_set_level(uint32_t type, uint32_t level)
100 if (type >= rte_logs.dynamic_types_len)
102 if (level > RTE_LOG_DEBUG)
105 rte_logs.dynamic_types[type].loglevel = level;
110 /* set log level by regular expression */
112 rte_log_set_level_regexp(const char *regex, uint32_t level)
117 if (level > RTE_LOG_DEBUG)
120 if (regcomp(&r, regex, 0) != 0)
123 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
124 if (rte_logs.dynamic_types[i].name == NULL)
126 if (regexec(&r, rte_logs.dynamic_types[i].name, 0,
128 rte_logs.dynamic_types[i].loglevel = level;
137 * Save the type string and the loglevel for later dynamic
138 * logtypes which may register later.
140 static int rte_log_save_level(int priority,
141 const char *regex, const char *pattern)
143 struct rte_eal_opt_loglevel *opt_ll = NULL;
145 opt_ll = malloc(sizeof(*opt_ll));
149 opt_ll->level = priority;
152 opt_ll->pattern = NULL;
153 if (regcomp(&opt_ll->re_match, regex, 0) != 0)
155 } else if (pattern) {
156 opt_ll->pattern = strdup(pattern);
157 if (opt_ll->pattern == NULL)
162 TAILQ_INSERT_HEAD(&opt_loglevel_list, opt_ll, next);
169 int rte_log_save_regexp(const char *regex, int tmp)
171 return rte_log_save_level(tmp, regex, NULL);
174 /* set log level based on glob (file match) pattern */
176 rte_log_set_level_pattern(const char *pattern, uint32_t level)
180 if (level > RTE_LOG_DEBUG)
183 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
184 if (rte_logs.dynamic_types[i].name == NULL)
187 if (fnmatch(pattern, rte_logs.dynamic_types[i].name, 0) == 0)
188 rte_logs.dynamic_types[i].loglevel = level;
194 int rte_log_save_pattern(const char *pattern, int priority)
196 return rte_log_save_level(priority, NULL, pattern);
199 /* get the current loglevel for the message being processed */
200 int rte_log_cur_msg_loglevel(void)
202 return RTE_PER_LCORE(log_cur_msg).loglevel;
205 /* get the current logtype for the message being processed */
206 int rte_log_cur_msg_logtype(void)
208 return RTE_PER_LCORE(log_cur_msg).logtype;
212 rte_log_lookup(const char *name)
216 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
217 if (rte_logs.dynamic_types[i].name == NULL)
219 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
226 /* register an extended log type, assuming table is large enough, and id
227 * is not yet registered.
230 __rte_log_register(const char *name, int id)
232 char *dup_name = strdup(name);
234 if (dup_name == NULL)
237 rte_logs.dynamic_types[id].name = dup_name;
238 rte_logs.dynamic_types[id].loglevel = RTE_LOG_INFO;
243 /* register an extended log type */
245 rte_log_register(const char *name)
247 struct rte_log_dynamic_type *new_dynamic_types;
250 id = rte_log_lookup(name);
254 new_dynamic_types = realloc(rte_logs.dynamic_types,
255 sizeof(struct rte_log_dynamic_type) *
256 (rte_logs.dynamic_types_len + 1));
257 if (new_dynamic_types == NULL)
259 rte_logs.dynamic_types = new_dynamic_types;
261 ret = __rte_log_register(name, rte_logs.dynamic_types_len);
265 rte_logs.dynamic_types_len++;
270 /* Register an extended log type and try to pick its level from EAL options */
272 rte_log_register_type_and_pick_level(const char *name, uint32_t level_def)
274 struct rte_eal_opt_loglevel *opt_ll;
275 uint32_t level = level_def;
278 type = rte_log_register(name);
282 TAILQ_FOREACH(opt_ll, &opt_loglevel_list, next) {
283 if (opt_ll->level > RTE_LOG_DEBUG)
286 if (opt_ll->pattern) {
287 if (fnmatch(opt_ll->pattern, name, 0))
288 level = opt_ll->level;
290 if (regexec(&opt_ll->re_match, name, 0, NULL, 0) == 0)
291 level = opt_ll->level;
295 rte_logs.dynamic_types[type].loglevel = level;
305 static const struct logtype logtype_strings[] = {
306 {RTE_LOGTYPE_EAL, "lib.eal"},
307 {RTE_LOGTYPE_MALLOC, "lib.malloc"},
308 {RTE_LOGTYPE_RING, "lib.ring"},
309 {RTE_LOGTYPE_MEMPOOL, "lib.mempool"},
310 {RTE_LOGTYPE_TIMER, "lib.timer"},
311 {RTE_LOGTYPE_PMD, "pmd"},
312 {RTE_LOGTYPE_HASH, "lib.hash"},
313 {RTE_LOGTYPE_LPM, "lib.lpm"},
314 {RTE_LOGTYPE_KNI, "lib.kni"},
315 {RTE_LOGTYPE_ACL, "lib.acl"},
316 {RTE_LOGTYPE_POWER, "lib.power"},
317 {RTE_LOGTYPE_METER, "lib.meter"},
318 {RTE_LOGTYPE_SCHED, "lib.sched"},
319 {RTE_LOGTYPE_PORT, "lib.port"},
320 {RTE_LOGTYPE_TABLE, "lib.table"},
321 {RTE_LOGTYPE_PIPELINE, "lib.pipeline"},
322 {RTE_LOGTYPE_MBUF, "lib.mbuf"},
323 {RTE_LOGTYPE_CRYPTODEV, "lib.cryptodev"},
324 {RTE_LOGTYPE_EFD, "lib.efd"},
325 {RTE_LOGTYPE_EVENTDEV, "lib.eventdev"},
326 {RTE_LOGTYPE_GSO, "lib.gso"},
327 {RTE_LOGTYPE_USER1, "user1"},
328 {RTE_LOGTYPE_USER2, "user2"},
329 {RTE_LOGTYPE_USER3, "user3"},
330 {RTE_LOGTYPE_USER4, "user4"},
331 {RTE_LOGTYPE_USER5, "user5"},
332 {RTE_LOGTYPE_USER6, "user6"},
333 {RTE_LOGTYPE_USER7, "user7"},
334 {RTE_LOGTYPE_USER8, "user8"}
337 /* Logging should be first initializer (before drivers and bus) */
338 RTE_INIT_PRIO(rte_log_init, LOG)
342 rte_log_set_global_level(RTE_LOG_DEBUG);
344 rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
345 sizeof(struct rte_log_dynamic_type));
346 if (rte_logs.dynamic_types == NULL)
349 /* register legacy log types */
350 for (i = 0; i < RTE_DIM(logtype_strings); i++)
351 __rte_log_register(logtype_strings[i].logtype,
352 logtype_strings[i].log_id);
354 rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
358 loglevel_to_string(uint32_t level)
361 case 0: return "disabled";
362 case RTE_LOG_EMERG: return "emerg";
363 case RTE_LOG_ALERT: return "alert";
364 case RTE_LOG_CRIT: return "critical";
365 case RTE_LOG_ERR: return "error";
366 case RTE_LOG_WARNING: return "warning";
367 case RTE_LOG_NOTICE: return "notice";
368 case RTE_LOG_INFO: return "info";
369 case RTE_LOG_DEBUG: return "debug";
370 default: return "unknown";
374 /* dump global level and registered log types */
376 rte_log_dump(FILE *f)
380 fprintf(f, "global log level is %s\n",
381 loglevel_to_string(rte_log_get_global_level()));
383 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
384 if (rte_logs.dynamic_types[i].name == NULL)
386 fprintf(f, "id %zu: %s, level is %s\n",
387 i, rte_logs.dynamic_types[i].name,
388 loglevel_to_string(rte_logs.dynamic_types[i].loglevel));
393 * Generates a log message The message will be sent in the stream
394 * defined by the previous call to rte_openlog_stream().
397 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
400 FILE *f = rte_logs.file;
402 f = default_log_stream;
405 * Grab the current value of stderr here, rather than
406 * just initializing default_log_stream to stderr. This
407 * ensures that we will always use the current value
408 * of stderr, even if the application closes and
415 if (level > rte_logs.level)
417 if (logtype >= rte_logs.dynamic_types_len)
419 if (level > rte_logs.dynamic_types[logtype].loglevel)
422 /* save loglevel and logtype in a global per-lcore variable */
423 RTE_PER_LCORE(log_cur_msg).loglevel = level;
424 RTE_PER_LCORE(log_cur_msg).logtype = logtype;
426 ret = vfprintf(f, format, ap);
432 * Generates a log message The message will be sent in the stream
433 * defined by the previous call to rte_openlog_stream().
434 * No need to check level here, done by rte_vlog().
437 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
442 va_start(ap, format);
443 ret = rte_vlog(level, logtype, format, ap);
449 * Called by environment-specific initialization functions.
452 eal_log_set_default(FILE *default_log)
454 default_log_stream = default_log;
456 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
458 "Debug dataplane logs available - lower performance\n");