1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
16 #include <rte_per_lcore.h>
20 struct rte_log_dynamic_type {
25 /** The rte_log structure. */
26 static struct rte_logs {
27 uint32_t type; /**< Bitfield with enabled logs. */
28 uint32_t level; /**< Log level. */
29 FILE *file; /**< Output file set by rte_openlog_stream, or NULL. */
30 size_t dynamic_types_len;
31 struct rte_log_dynamic_type *dynamic_types;
34 .level = RTE_LOG_DEBUG,
37 struct rte_eal_opt_loglevel {
38 /** Next list entry */
39 TAILQ_ENTRY(rte_eal_opt_loglevel) next;
40 /** Compiled regular expression obtained from the option */
42 /** Globbing pattern option */
44 /** Log level value obtained from the option */
48 TAILQ_HEAD(rte_eal_opt_loglevel_list, rte_eal_opt_loglevel);
50 /** List of valid EAL log level options */
51 static struct rte_eal_opt_loglevel_list opt_loglevel_list =
52 TAILQ_HEAD_INITIALIZER(opt_loglevel_list);
54 /* Stream to use for logging if rte_logs.file is NULL */
55 static FILE *default_log_stream;
58 * This global structure stores some information about the message
59 * that is currently being processed by one lcore
62 uint32_t loglevel; /**< log level - see rte_log.h */
63 uint32_t logtype; /**< log type - see rte_log.h */
67 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
71 /* Change the stream that will be used by logging system */
73 rte_openlog_stream(FILE *f)
80 rte_log_get_stream(void)
82 FILE *f = rte_logs.file;
86 * Grab the current value of stderr here, rather than
87 * just initializing default_log_stream to stderr. This
88 * ensures that we will always use the current value
89 * of stderr, even if the application closes and
92 return default_log_stream ? : stderr;
97 /* Set global log level */
99 rte_log_set_global_level(uint32_t level)
101 rte_logs.level = (uint32_t)level;
104 /* Get global log level */
106 rte_log_get_global_level(void)
108 return rte_logs.level;
112 rte_log_get_level(uint32_t type)
114 if (type >= rte_logs.dynamic_types_len)
117 return rte_logs.dynamic_types[type].loglevel;
121 rte_log_can_log(uint32_t logtype, uint32_t level)
125 if (level > rte_log_get_global_level())
128 log_level = rte_log_get_level(logtype);
132 if (level > (uint32_t)log_level)
139 rte_log_set_level(uint32_t type, uint32_t level)
141 if (type >= rte_logs.dynamic_types_len)
143 if (level > RTE_LOG_MAX)
146 rte_logs.dynamic_types[type].loglevel = level;
151 /* set log level by regular expression */
153 rte_log_set_level_regexp(const char *regex, uint32_t level)
158 if (level > RTE_LOG_MAX)
161 if (regcomp(&r, regex, 0) != 0)
164 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
165 if (rte_logs.dynamic_types[i].name == NULL)
167 if (regexec(&r, rte_logs.dynamic_types[i].name, 0,
169 rte_logs.dynamic_types[i].loglevel = level;
178 * Save the type string and the loglevel for later dynamic
179 * logtypes which may register later.
182 log_save_level(uint32_t priority, const char *regex, const char *pattern)
184 struct rte_eal_opt_loglevel *opt_ll = NULL;
186 opt_ll = malloc(sizeof(*opt_ll));
190 opt_ll->level = priority;
193 opt_ll->pattern = NULL;
194 if (regcomp(&opt_ll->re_match, regex, 0) != 0)
196 } else if (pattern) {
197 opt_ll->pattern = strdup(pattern);
198 if (opt_ll->pattern == NULL)
203 TAILQ_INSERT_HEAD(&opt_loglevel_list, opt_ll, next);
211 eal_log_save_regexp(const char *regex, uint32_t level)
213 return log_save_level(level, regex, NULL);
216 /* set log level based on globbing pattern */
218 rte_log_set_level_pattern(const char *pattern, uint32_t level)
222 if (level > RTE_LOG_MAX)
225 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
226 if (rte_logs.dynamic_types[i].name == NULL)
229 if (fnmatch(pattern, rte_logs.dynamic_types[i].name, 0) == 0)
230 rte_logs.dynamic_types[i].loglevel = level;
237 eal_log_save_pattern(const char *pattern, uint32_t level)
239 return log_save_level(level, NULL, pattern);
242 /* get the current loglevel for the message being processed */
243 int rte_log_cur_msg_loglevel(void)
245 return RTE_PER_LCORE(log_cur_msg).loglevel;
248 /* get the current logtype for the message being processed */
249 int rte_log_cur_msg_logtype(void)
251 return RTE_PER_LCORE(log_cur_msg).logtype;
255 log_lookup(const char *name)
259 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
260 if (rte_logs.dynamic_types[i].name == NULL)
262 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
269 /* register an extended log type, assuming table is large enough, and id
270 * is not yet registered.
273 log_register(const char *name, int id)
275 char *dup_name = strdup(name);
277 if (dup_name == NULL)
280 rte_logs.dynamic_types[id].name = dup_name;
281 rte_logs.dynamic_types[id].loglevel = RTE_LOG_INFO;
286 /* register an extended log type */
288 rte_log_register(const char *name)
290 struct rte_log_dynamic_type *new_dynamic_types;
293 id = log_lookup(name);
297 new_dynamic_types = realloc(rte_logs.dynamic_types,
298 sizeof(struct rte_log_dynamic_type) *
299 (rte_logs.dynamic_types_len + 1));
300 if (new_dynamic_types == NULL)
302 rte_logs.dynamic_types = new_dynamic_types;
304 ret = log_register(name, rte_logs.dynamic_types_len);
308 rte_logs.dynamic_types_len++;
313 /* Register an extended log type and try to pick its level from EAL options */
315 rte_log_register_type_and_pick_level(const char *name, uint32_t level_def)
317 struct rte_eal_opt_loglevel *opt_ll;
318 uint32_t level = level_def;
321 type = rte_log_register(name);
325 TAILQ_FOREACH(opt_ll, &opt_loglevel_list, next) {
326 if (opt_ll->level > RTE_LOG_MAX)
329 if (opt_ll->pattern) {
330 if (fnmatch(opt_ll->pattern, name, 0) == 0)
331 level = opt_ll->level;
333 if (regexec(&opt_ll->re_match, name, 0, NULL, 0) == 0)
334 level = opt_ll->level;
338 rte_logs.dynamic_types[type].loglevel = level;
348 static const struct logtype logtype_strings[] = {
349 {RTE_LOGTYPE_EAL, "lib.eal"},
350 {RTE_LOGTYPE_MALLOC, "lib.malloc"},
351 {RTE_LOGTYPE_RING, "lib.ring"},
352 {RTE_LOGTYPE_MEMPOOL, "lib.mempool"},
353 {RTE_LOGTYPE_TIMER, "lib.timer"},
354 {RTE_LOGTYPE_PMD, "pmd"},
355 {RTE_LOGTYPE_HASH, "lib.hash"},
356 {RTE_LOGTYPE_LPM, "lib.lpm"},
357 {RTE_LOGTYPE_KNI, "lib.kni"},
358 {RTE_LOGTYPE_ACL, "lib.acl"},
359 {RTE_LOGTYPE_POWER, "lib.power"},
360 {RTE_LOGTYPE_METER, "lib.meter"},
361 {RTE_LOGTYPE_SCHED, "lib.sched"},
362 {RTE_LOGTYPE_PORT, "lib.port"},
363 {RTE_LOGTYPE_TABLE, "lib.table"},
364 {RTE_LOGTYPE_PIPELINE, "lib.pipeline"},
365 {RTE_LOGTYPE_MBUF, "lib.mbuf"},
366 {RTE_LOGTYPE_CRYPTODEV, "lib.cryptodev"},
367 {RTE_LOGTYPE_EFD, "lib.efd"},
368 {RTE_LOGTYPE_EVENTDEV, "lib.eventdev"},
369 {RTE_LOGTYPE_GSO, "lib.gso"},
370 {RTE_LOGTYPE_USER1, "user1"},
371 {RTE_LOGTYPE_USER2, "user2"},
372 {RTE_LOGTYPE_USER3, "user3"},
373 {RTE_LOGTYPE_USER4, "user4"},
374 {RTE_LOGTYPE_USER5, "user5"},
375 {RTE_LOGTYPE_USER6, "user6"},
376 {RTE_LOGTYPE_USER7, "user7"},
377 {RTE_LOGTYPE_USER8, "user8"}
380 /* Logging should be first initializer (before drivers and bus) */
381 RTE_INIT_PRIO(log_init, LOG)
385 rte_log_set_global_level(RTE_LOG_DEBUG);
387 rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
388 sizeof(struct rte_log_dynamic_type));
389 if (rte_logs.dynamic_types == NULL)
392 /* register legacy log types */
393 for (i = 0; i < RTE_DIM(logtype_strings); i++)
394 log_register(logtype_strings[i].logtype,
395 logtype_strings[i].log_id);
397 rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
401 eal_log_level2str(uint32_t level)
404 case 0: return "disabled";
405 case RTE_LOG_EMERG: return "emergency";
406 case RTE_LOG_ALERT: return "alert";
407 case RTE_LOG_CRIT: return "critical";
408 case RTE_LOG_ERR: return "error";
409 case RTE_LOG_WARNING: return "warning";
410 case RTE_LOG_NOTICE: return "notice";
411 case RTE_LOG_INFO: return "info";
412 case RTE_LOG_DEBUG: return "debug";
413 default: return "unknown";
418 log_type_compare(const void *a, const void *b)
420 const struct rte_log_dynamic_type *type_a = a;
421 const struct rte_log_dynamic_type *type_b = b;
423 if (type_a->name == NULL && type_b->name == NULL)
425 if (type_a->name == NULL)
427 if (type_b->name == NULL)
429 return strcmp(type_a->name, type_b->name);
432 /* Dump name of each logtype, one per line. */
434 rte_log_list_types(FILE *out, const char *prefix)
436 struct rte_log_dynamic_type *sorted_types;
437 const size_t type_size = sizeof(rte_logs.dynamic_types[0]);
438 const size_t type_count = rte_logs.dynamic_types_len;
439 const size_t total_size = type_size * type_count;
442 sorted_types = malloc(total_size);
443 if (sorted_types == NULL) {
444 /* no sorting - unlikely */
445 sorted_types = rte_logs.dynamic_types;
447 memcpy(sorted_types, rte_logs.dynamic_types, total_size);
448 qsort(sorted_types, type_count, type_size, log_type_compare);
451 for (type = 0; type < type_count; ++type) {
452 if (sorted_types[type].name == NULL)
454 fprintf(out, "%s%s\n", prefix, sorted_types[type].name);
457 if (sorted_types != rte_logs.dynamic_types)
461 /* dump global level and registered log types */
463 rte_log_dump(FILE *f)
467 fprintf(f, "global log level is %s\n",
468 eal_log_level2str(rte_log_get_global_level()));
470 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
471 if (rte_logs.dynamic_types[i].name == NULL)
473 fprintf(f, "id %zu: %s, level is %s\n",
474 i, rte_logs.dynamic_types[i].name,
475 eal_log_level2str(rte_logs.dynamic_types[i].loglevel));
480 * Generates a log message The message will be sent in the stream
481 * defined by the previous call to rte_openlog_stream().
484 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
486 FILE *f = rte_log_get_stream();
489 if (logtype >= rte_logs.dynamic_types_len)
491 if (!rte_log_can_log(logtype, level))
494 /* save loglevel and logtype in a global per-lcore variable */
495 RTE_PER_LCORE(log_cur_msg).loglevel = level;
496 RTE_PER_LCORE(log_cur_msg).logtype = logtype;
498 ret = vfprintf(f, format, ap);
504 * Generates a log message The message will be sent in the stream
505 * defined by the previous call to rte_openlog_stream().
506 * No need to check level here, done by rte_vlog().
509 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
514 va_start(ap, format);
515 ret = rte_vlog(level, logtype, format, ap);
521 * Called by environment-specific initialization functions.
524 eal_log_set_default(FILE *default_log)
526 default_log_stream = default_log;
528 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
530 "Debug dataplane logs available - lower performance\n");