1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #include <sys/queue.h>
16 #include <rte_per_lcore.h>
19 #include "eal_private.h"
21 struct rte_log_dynamic_type {
26 /** The rte_log structure. */
27 static struct rte_logs {
28 uint32_t type; /**< Bitfield with enabled logs. */
29 uint32_t level; /**< Log level. */
30 FILE *file; /**< Output file set by rte_openlog_stream, or NULL. */
31 size_t dynamic_types_len;
32 struct rte_log_dynamic_type *dynamic_types;
35 .level = RTE_LOG_DEBUG,
38 struct rte_eal_opt_loglevel {
39 /** Next list entry */
40 TAILQ_ENTRY(rte_eal_opt_loglevel) next;
41 /** Compiled regular expression obtained from the option */
43 /** Globbing pattern option */
45 /** Log level value obtained from the option */
49 TAILQ_HEAD(rte_eal_opt_loglevel_list, rte_eal_opt_loglevel);
51 /** List of valid EAL log level options */
52 static struct rte_eal_opt_loglevel_list opt_loglevel_list =
53 TAILQ_HEAD_INITIALIZER(opt_loglevel_list);
55 /* Stream to use for logging if rte_logs.file is NULL */
56 static FILE *default_log_stream;
59 * This global structure stores some information 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 */
68 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
72 /* Change the stream that will be used by logging system */
74 rte_openlog_stream(FILE *f)
81 rte_log_get_stream(void)
83 FILE *f = rte_logs.file;
87 * Grab the current value of stderr here, rather than
88 * just initializing default_log_stream to stderr. This
89 * ensures that we will always use the current value
90 * of stderr, even if the application closes and
93 return default_log_stream ? : stderr;
98 /* Set global log level */
100 rte_log_set_global_level(uint32_t level)
102 rte_logs.level = (uint32_t)level;
105 /* Get global log level */
107 rte_log_get_global_level(void)
109 return rte_logs.level;
113 rte_log_get_level(uint32_t type)
115 if (type >= rte_logs.dynamic_types_len)
118 return rte_logs.dynamic_types[type].loglevel;
122 rte_log_can_log(uint32_t logtype, uint32_t level)
126 if (level > rte_log_get_global_level())
129 log_level = rte_log_get_level(logtype);
133 if (level > (uint32_t)log_level)
140 logtype_set_level(uint32_t type, uint32_t level)
142 uint32_t current = rte_logs.dynamic_types[type].loglevel;
144 if (current != level) {
145 rte_logs.dynamic_types[type].loglevel = level;
146 RTE_LOG(DEBUG, EAL, "%s log level changed from %s to %s\n",
147 rte_logs.dynamic_types[type].name == NULL ?
148 "" : rte_logs.dynamic_types[type].name,
149 eal_log_level2str(current),
150 eal_log_level2str(level));
155 rte_log_set_level(uint32_t type, uint32_t level)
157 if (type >= rte_logs.dynamic_types_len)
159 if (level > RTE_LOG_MAX)
162 logtype_set_level(type, level);
167 /* set log level by regular expression */
169 rte_log_set_level_regexp(const char *regex, uint32_t level)
174 if (level > RTE_LOG_MAX)
177 if (regcomp(&r, regex, 0) != 0)
180 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
181 if (rte_logs.dynamic_types[i].name == NULL)
183 if (regexec(&r, rte_logs.dynamic_types[i].name, 0,
185 logtype_set_level(i, level);
194 * Save the type string and the loglevel for later dynamic
195 * logtypes which may register later.
198 log_save_level(uint32_t priority, const char *regex, const char *pattern)
200 struct rte_eal_opt_loglevel *opt_ll = NULL;
202 opt_ll = malloc(sizeof(*opt_ll));
206 opt_ll->level = priority;
209 opt_ll->pattern = NULL;
210 if (regcomp(&opt_ll->re_match, regex, 0) != 0)
212 } else if (pattern) {
213 opt_ll->pattern = strdup(pattern);
214 if (opt_ll->pattern == NULL)
219 TAILQ_INSERT_HEAD(&opt_loglevel_list, opt_ll, next);
227 eal_log_save_regexp(const char *regex, uint32_t level)
229 return log_save_level(level, regex, NULL);
232 /* set log level based on globbing pattern */
234 rte_log_set_level_pattern(const char *pattern, uint32_t level)
238 if (level > RTE_LOG_MAX)
241 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
242 if (rte_logs.dynamic_types[i].name == NULL)
245 if (fnmatch(pattern, rte_logs.dynamic_types[i].name, 0) == 0)
246 logtype_set_level(i, level);
253 eal_log_save_pattern(const char *pattern, uint32_t level)
255 return log_save_level(level, NULL, pattern);
258 /* get the current loglevel for the message being processed */
259 int rte_log_cur_msg_loglevel(void)
261 return RTE_PER_LCORE(log_cur_msg).loglevel;
264 /* get the current logtype for the message being processed */
265 int rte_log_cur_msg_logtype(void)
267 return RTE_PER_LCORE(log_cur_msg).logtype;
271 log_lookup(const char *name)
275 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
276 if (rte_logs.dynamic_types[i].name == NULL)
278 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
286 log_register(const char *name, uint32_t level)
288 struct rte_log_dynamic_type *new_dynamic_types;
291 id = log_lookup(name);
295 new_dynamic_types = realloc(rte_logs.dynamic_types,
296 sizeof(struct rte_log_dynamic_type) *
297 (rte_logs.dynamic_types_len + 1));
298 if (new_dynamic_types == NULL)
300 rte_logs.dynamic_types = new_dynamic_types;
302 id = rte_logs.dynamic_types_len;
303 memset(&rte_logs.dynamic_types[id], 0,
304 sizeof(rte_logs.dynamic_types[id]));
305 rte_logs.dynamic_types[id].name = strdup(name);
306 if (rte_logs.dynamic_types[id].name == NULL)
308 logtype_set_level(id, level);
310 rte_logs.dynamic_types_len++;
315 /* register an extended log type */
317 rte_log_register(const char *name)
319 return log_register(name, RTE_LOG_INFO);
322 /* Register an extended log type and try to pick its level from EAL options */
324 rte_log_register_type_and_pick_level(const char *name, uint32_t level_def)
326 struct rte_eal_opt_loglevel *opt_ll;
327 uint32_t level = level_def;
329 TAILQ_FOREACH(opt_ll, &opt_loglevel_list, next) {
330 if (opt_ll->level > RTE_LOG_MAX)
333 if (opt_ll->pattern) {
334 if (fnmatch(opt_ll->pattern, name, 0) == 0)
335 level = opt_ll->level;
337 if (regexec(&opt_ll->re_match, name, 0, NULL, 0) == 0)
338 level = opt_ll->level;
342 return log_register(name, level);
350 static const struct logtype logtype_strings[] = {
351 {RTE_LOGTYPE_EAL, "lib.eal"},
352 {RTE_LOGTYPE_MALLOC, "lib.malloc"},
353 {RTE_LOGTYPE_RING, "lib.ring"},
354 {RTE_LOGTYPE_MEMPOOL, "lib.mempool"},
355 {RTE_LOGTYPE_TIMER, "lib.timer"},
356 {RTE_LOGTYPE_PMD, "pmd"},
357 {RTE_LOGTYPE_HASH, "lib.hash"},
358 {RTE_LOGTYPE_LPM, "lib.lpm"},
359 {RTE_LOGTYPE_KNI, "lib.kni"},
360 {RTE_LOGTYPE_ACL, "lib.acl"},
361 {RTE_LOGTYPE_POWER, "lib.power"},
362 {RTE_LOGTYPE_METER, "lib.meter"},
363 {RTE_LOGTYPE_SCHED, "lib.sched"},
364 {RTE_LOGTYPE_PORT, "lib.port"},
365 {RTE_LOGTYPE_TABLE, "lib.table"},
366 {RTE_LOGTYPE_PIPELINE, "lib.pipeline"},
367 {RTE_LOGTYPE_MBUF, "lib.mbuf"},
368 {RTE_LOGTYPE_CRYPTODEV, "lib.cryptodev"},
369 {RTE_LOGTYPE_EFD, "lib.efd"},
370 {RTE_LOGTYPE_EVENTDEV, "lib.eventdev"},
371 {RTE_LOGTYPE_GSO, "lib.gso"},
372 {RTE_LOGTYPE_USER1, "user1"},
373 {RTE_LOGTYPE_USER2, "user2"},
374 {RTE_LOGTYPE_USER3, "user3"},
375 {RTE_LOGTYPE_USER4, "user4"},
376 {RTE_LOGTYPE_USER5, "user5"},
377 {RTE_LOGTYPE_USER6, "user6"},
378 {RTE_LOGTYPE_USER7, "user7"},
379 {RTE_LOGTYPE_USER8, "user8"}
382 /* Logging should be first initializer (before drivers and bus) */
383 RTE_INIT_PRIO(log_init, LOG)
387 rte_log_set_global_level(RTE_LOG_DEBUG);
389 rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
390 sizeof(struct rte_log_dynamic_type));
391 if (rte_logs.dynamic_types == NULL)
394 /* register legacy log types */
395 for (i = 0; i < RTE_DIM(logtype_strings); i++) {
396 rte_logs.dynamic_types[logtype_strings[i].log_id].name =
397 strdup(logtype_strings[i].logtype);
398 logtype_set_level(logtype_strings[i].log_id, RTE_LOG_INFO);
401 rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
405 eal_log_level2str(uint32_t level)
408 case 0: return "disabled";
409 case RTE_LOG_EMERG: return "emergency";
410 case RTE_LOG_ALERT: return "alert";
411 case RTE_LOG_CRIT: return "critical";
412 case RTE_LOG_ERR: return "error";
413 case RTE_LOG_WARNING: return "warning";
414 case RTE_LOG_NOTICE: return "notice";
415 case RTE_LOG_INFO: return "info";
416 case RTE_LOG_DEBUG: return "debug";
417 default: return "unknown";
422 log_type_compare(const void *a, const void *b)
424 const struct rte_log_dynamic_type *type_a = a;
425 const struct rte_log_dynamic_type *type_b = b;
427 if (type_a->name == NULL && type_b->name == NULL)
429 if (type_a->name == NULL)
431 if (type_b->name == NULL)
433 return strcmp(type_a->name, type_b->name);
436 /* Dump name of each logtype, one per line. */
438 rte_log_list_types(FILE *out, const char *prefix)
440 struct rte_log_dynamic_type *sorted_types;
441 const size_t type_size = sizeof(rte_logs.dynamic_types[0]);
442 const size_t type_count = rte_logs.dynamic_types_len;
443 const size_t total_size = type_size * type_count;
446 sorted_types = malloc(total_size);
447 if (sorted_types == NULL) {
448 /* no sorting - unlikely */
449 sorted_types = rte_logs.dynamic_types;
451 memcpy(sorted_types, rte_logs.dynamic_types, total_size);
452 qsort(sorted_types, type_count, type_size, log_type_compare);
455 for (type = 0; type < type_count; ++type) {
456 if (sorted_types[type].name == NULL)
458 fprintf(out, "%s%s\n", prefix, sorted_types[type].name);
461 if (sorted_types != rte_logs.dynamic_types)
465 /* dump global level and registered log types */
467 rte_log_dump(FILE *f)
471 fprintf(f, "global log level is %s\n",
472 eal_log_level2str(rte_log_get_global_level()));
474 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
475 if (rte_logs.dynamic_types[i].name == NULL)
477 fprintf(f, "id %zu: %s, level is %s\n",
478 i, rte_logs.dynamic_types[i].name,
479 eal_log_level2str(rte_logs.dynamic_types[i].loglevel));
484 * Generates a log message The message will be sent in the stream
485 * defined by the previous call to rte_openlog_stream().
488 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
490 FILE *f = rte_log_get_stream();
493 if (logtype >= rte_logs.dynamic_types_len)
495 if (!rte_log_can_log(logtype, level))
498 /* save loglevel and logtype in a global per-lcore variable */
499 RTE_PER_LCORE(log_cur_msg).loglevel = level;
500 RTE_PER_LCORE(log_cur_msg).logtype = logtype;
502 ret = vfprintf(f, format, ap);
508 * Generates a log message The message will be sent in the stream
509 * defined by the previous call to rte_openlog_stream().
510 * No need to check level here, done by rte_vlog().
513 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
518 va_start(ap, format);
519 ret = rte_vlog(level, logtype, format, ap);
525 * Called by environment-specific initialization functions.
528 eal_log_set_default(FILE *default_log)
530 default_log_stream = default_log;
532 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
534 "Debug dataplane logs available - lower performance\n");
539 * Called by eal_cleanup
542 rte_eal_log_cleanup(void)
544 if (default_log_stream) {
545 fclose(default_log_stream);
546 default_log_stream = NULL;