xref: /dpdk/lib/log/log.c (revision ba9fb2795b9f751836c0949c0c531da7af52a4f2)
109ce4131SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
209ce4131SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
309ce4131SBruce Richardson  */
409ce4131SBruce Richardson 
598513036SStephen Hemminger #include <stdbool.h>
609ce4131SBruce Richardson #include <stdio.h>
709ce4131SBruce Richardson #include <stdint.h>
809ce4131SBruce Richardson #include <stdarg.h>
909ce4131SBruce Richardson #include <stdlib.h>
1009ce4131SBruce Richardson #include <string.h>
1109ce4131SBruce Richardson #include <errno.h>
1209ce4131SBruce Richardson #include <regex.h>
1309ce4131SBruce Richardson #include <fnmatch.h>
1409ce4131SBruce Richardson #include <sys/queue.h>
15259f6f78SStephen Hemminger #include <unistd.h>
1609ce4131SBruce Richardson 
1798513036SStephen Hemminger #include <rte_common.h>
1809ce4131SBruce Richardson #include <rte_log.h>
1909ce4131SBruce Richardson #include <rte_per_lcore.h>
2009ce4131SBruce Richardson 
2162ae1149SStephen Hemminger #ifdef RTE_EXEC_ENV_WINDOWS
2262ae1149SStephen Hemminger #include <rte_os_shim.h>
2362ae1149SStephen Hemminger #endif
2462ae1149SStephen Hemminger 
2509ce4131SBruce Richardson #include "log_internal.h"
2698513036SStephen Hemminger #include "log_private.h"
2709ce4131SBruce Richardson 
2809ce4131SBruce Richardson struct rte_log_dynamic_type {
2909ce4131SBruce Richardson 	const char *name;
3009ce4131SBruce Richardson 	uint32_t loglevel;
3109ce4131SBruce Richardson };
3209ce4131SBruce Richardson 
33630cdfcdSStephen Hemminger /* Note: same as vfprintf() */
34630cdfcdSStephen Hemminger typedef int (*log_print_t)(FILE *f, const char *fmt, va_list ap);
35630cdfcdSStephen Hemminger 
3609ce4131SBruce Richardson /** The rte_log structure. */
3709ce4131SBruce Richardson static struct rte_logs {
3809ce4131SBruce Richardson 	uint32_t type;  /**< Bitfield with enabled logs. */
3909ce4131SBruce Richardson 	uint32_t level; /**< Log level. */
4009ce4131SBruce Richardson 	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
41*ba9fb279SRobin Jarry 	bool is_internal_file;
42630cdfcdSStephen Hemminger 	log_print_t print_func;
4309ce4131SBruce Richardson 	size_t dynamic_types_len;
4409ce4131SBruce Richardson 	struct rte_log_dynamic_type *dynamic_types;
4509ce4131SBruce Richardson } rte_logs = {
4609ce4131SBruce Richardson 	.type = UINT32_MAX,
4709ce4131SBruce Richardson 	.level = RTE_LOG_DEBUG,
48630cdfcdSStephen Hemminger 	.print_func = vfprintf,
4909ce4131SBruce Richardson };
5009ce4131SBruce Richardson 
5109ce4131SBruce Richardson struct rte_eal_opt_loglevel {
5209ce4131SBruce Richardson 	/** Next list entry */
5309ce4131SBruce Richardson 	TAILQ_ENTRY(rte_eal_opt_loglevel) next;
5409ce4131SBruce Richardson 	/** Compiled regular expression obtained from the option */
5509ce4131SBruce Richardson 	regex_t re_match;
5609ce4131SBruce Richardson 	/** Globbing pattern option */
5709ce4131SBruce Richardson 	char *pattern;
5809ce4131SBruce Richardson 	/** Log level value obtained from the option */
5909ce4131SBruce Richardson 	uint32_t level;
6009ce4131SBruce Richardson };
6109ce4131SBruce Richardson 
6209ce4131SBruce Richardson TAILQ_HEAD(rte_eal_opt_loglevel_list, rte_eal_opt_loglevel);
6309ce4131SBruce Richardson 
6409ce4131SBruce Richardson /** List of valid EAL log level options */
6509ce4131SBruce Richardson static struct rte_eal_opt_loglevel_list opt_loglevel_list =
6609ce4131SBruce Richardson 	TAILQ_HEAD_INITIALIZER(opt_loglevel_list);
6709ce4131SBruce Richardson 
6809ce4131SBruce Richardson /**
6909ce4131SBruce Richardson  * This global structure stores some information about the message
7009ce4131SBruce Richardson  * that is currently being processed by one lcore
7109ce4131SBruce Richardson  */
7209ce4131SBruce Richardson struct log_cur_msg {
7309ce4131SBruce Richardson 	uint32_t loglevel; /**< log level - see rte_log.h */
7409ce4131SBruce Richardson 	uint32_t logtype;  /**< log type  - see rte_log.h */
7509ce4131SBruce Richardson };
7609ce4131SBruce Richardson 
7709ce4131SBruce Richardson  /* per core log */
7809ce4131SBruce Richardson static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
7909ce4131SBruce Richardson 
8009ce4131SBruce Richardson /* Change the stream that will be used by logging system */
8109ce4131SBruce Richardson int
8209ce4131SBruce Richardson rte_openlog_stream(FILE *f)
8309ce4131SBruce Richardson {
84*ba9fb279SRobin Jarry 	if (rte_logs.is_internal_file && rte_logs.file != NULL)
85*ba9fb279SRobin Jarry 		fclose(rte_logs.file);
8609ce4131SBruce Richardson 	rte_logs.file = f;
87630cdfcdSStephen Hemminger 	rte_logs.print_func = vfprintf;
88*ba9fb279SRobin Jarry 	rte_logs.is_internal_file = false;
8909ce4131SBruce Richardson 	return 0;
9009ce4131SBruce Richardson }
9109ce4131SBruce Richardson 
9209ce4131SBruce Richardson FILE *
9309ce4131SBruce Richardson rte_log_get_stream(void)
9409ce4131SBruce Richardson {
9509ce4131SBruce Richardson 	FILE *f = rte_logs.file;
9609ce4131SBruce Richardson 
9798513036SStephen Hemminger 	return (f == NULL) ? stderr : f;
9809ce4131SBruce Richardson }
9909ce4131SBruce Richardson 
10009ce4131SBruce Richardson /* Set global log level */
10109ce4131SBruce Richardson void
10209ce4131SBruce Richardson rte_log_set_global_level(uint32_t level)
10309ce4131SBruce Richardson {
10409ce4131SBruce Richardson 	rte_logs.level = (uint32_t)level;
10509ce4131SBruce Richardson }
10609ce4131SBruce Richardson 
10709ce4131SBruce Richardson /* Get global log level */
10809ce4131SBruce Richardson uint32_t
10909ce4131SBruce Richardson rte_log_get_global_level(void)
11009ce4131SBruce Richardson {
11109ce4131SBruce Richardson 	return rte_logs.level;
11209ce4131SBruce Richardson }
11309ce4131SBruce Richardson 
11409ce4131SBruce Richardson int
11509ce4131SBruce Richardson rte_log_get_level(uint32_t type)
11609ce4131SBruce Richardson {
11709ce4131SBruce Richardson 	if (type >= rte_logs.dynamic_types_len)
11809ce4131SBruce Richardson 		return -1;
11909ce4131SBruce Richardson 
12009ce4131SBruce Richardson 	return rte_logs.dynamic_types[type].loglevel;
12109ce4131SBruce Richardson }
12209ce4131SBruce Richardson 
12309ce4131SBruce Richardson bool
12409ce4131SBruce Richardson rte_log_can_log(uint32_t logtype, uint32_t level)
12509ce4131SBruce Richardson {
12609ce4131SBruce Richardson 	int log_level;
12709ce4131SBruce Richardson 
12809ce4131SBruce Richardson 	if (level > rte_log_get_global_level())
12909ce4131SBruce Richardson 		return false;
13009ce4131SBruce Richardson 
13109ce4131SBruce Richardson 	log_level = rte_log_get_level(logtype);
13209ce4131SBruce Richardson 	if (log_level < 0)
13309ce4131SBruce Richardson 		return false;
13409ce4131SBruce Richardson 
13509ce4131SBruce Richardson 	if (level > (uint32_t)log_level)
13609ce4131SBruce Richardson 		return false;
13709ce4131SBruce Richardson 
13809ce4131SBruce Richardson 	return true;
13909ce4131SBruce Richardson }
14009ce4131SBruce Richardson 
14109ce4131SBruce Richardson static void
14209ce4131SBruce Richardson logtype_set_level(uint32_t type, uint32_t level)
14309ce4131SBruce Richardson {
14409ce4131SBruce Richardson 	uint32_t current = rte_logs.dynamic_types[type].loglevel;
14509ce4131SBruce Richardson 
14609ce4131SBruce Richardson 	if (current != level) {
14709ce4131SBruce Richardson 		rte_logs.dynamic_types[type].loglevel = level;
14809ce4131SBruce Richardson 		RTE_LOG(DEBUG, EAL, "%s log level changed from %s to %s\n",
14909ce4131SBruce Richardson 			rte_logs.dynamic_types[type].name == NULL ?
15009ce4131SBruce Richardson 				"" : rte_logs.dynamic_types[type].name,
15109ce4131SBruce Richardson 			eal_log_level2str(current),
15209ce4131SBruce Richardson 			eal_log_level2str(level));
15309ce4131SBruce Richardson 	}
15409ce4131SBruce Richardson }
15509ce4131SBruce Richardson 
15609ce4131SBruce Richardson int
15709ce4131SBruce Richardson rte_log_set_level(uint32_t type, uint32_t level)
15809ce4131SBruce Richardson {
15909ce4131SBruce Richardson 	if (type >= rte_logs.dynamic_types_len)
16009ce4131SBruce Richardson 		return -1;
16109ce4131SBruce Richardson 	if (level > RTE_LOG_MAX)
16209ce4131SBruce Richardson 		return -1;
16309ce4131SBruce Richardson 
16409ce4131SBruce Richardson 	logtype_set_level(type, level);
16509ce4131SBruce Richardson 
16609ce4131SBruce Richardson 	return 0;
16709ce4131SBruce Richardson }
16809ce4131SBruce Richardson 
16909ce4131SBruce Richardson /* set log level by regular expression */
17009ce4131SBruce Richardson int
17109ce4131SBruce Richardson rte_log_set_level_regexp(const char *regex, uint32_t level)
17209ce4131SBruce Richardson {
17309ce4131SBruce Richardson 	regex_t r;
17409ce4131SBruce Richardson 	size_t i;
17509ce4131SBruce Richardson 
17609ce4131SBruce Richardson 	if (level > RTE_LOG_MAX)
17709ce4131SBruce Richardson 		return -1;
17809ce4131SBruce Richardson 
17909ce4131SBruce Richardson 	if (regcomp(&r, regex, 0) != 0)
18009ce4131SBruce Richardson 		return -1;
18109ce4131SBruce Richardson 
18209ce4131SBruce Richardson 	for (i = 0; i < rte_logs.dynamic_types_len; i++) {
18309ce4131SBruce Richardson 		if (rte_logs.dynamic_types[i].name == NULL)
18409ce4131SBruce Richardson 			continue;
18509ce4131SBruce Richardson 		if (regexec(&r, rte_logs.dynamic_types[i].name, 0,
18609ce4131SBruce Richardson 				NULL, 0) == 0)
18709ce4131SBruce Richardson 			logtype_set_level(i, level);
18809ce4131SBruce Richardson 	}
18909ce4131SBruce Richardson 
19009ce4131SBruce Richardson 	regfree(&r);
19109ce4131SBruce Richardson 
19209ce4131SBruce Richardson 	return 0;
19309ce4131SBruce Richardson }
19409ce4131SBruce Richardson 
19509ce4131SBruce Richardson /*
19609ce4131SBruce Richardson  * Save the type string and the loglevel for later dynamic
19709ce4131SBruce Richardson  * logtypes which may register later.
19809ce4131SBruce Richardson  */
19909ce4131SBruce Richardson static int
20009ce4131SBruce Richardson log_save_level(uint32_t priority, const char *regex, const char *pattern)
20109ce4131SBruce Richardson {
20209ce4131SBruce Richardson 	struct rte_eal_opt_loglevel *opt_ll = NULL;
20309ce4131SBruce Richardson 
20409ce4131SBruce Richardson 	opt_ll = malloc(sizeof(*opt_ll));
20509ce4131SBruce Richardson 	if (opt_ll == NULL)
20609ce4131SBruce Richardson 		goto fail;
20709ce4131SBruce Richardson 
20809ce4131SBruce Richardson 	opt_ll->level = priority;
20909ce4131SBruce Richardson 
21009ce4131SBruce Richardson 	if (regex) {
21109ce4131SBruce Richardson 		opt_ll->pattern = NULL;
21209ce4131SBruce Richardson 		if (regcomp(&opt_ll->re_match, regex, 0) != 0)
21309ce4131SBruce Richardson 			goto fail;
21409ce4131SBruce Richardson 	} else if (pattern) {
21509ce4131SBruce Richardson 		opt_ll->pattern = strdup(pattern);
21609ce4131SBruce Richardson 		if (opt_ll->pattern == NULL)
21709ce4131SBruce Richardson 			goto fail;
21809ce4131SBruce Richardson 	} else
21909ce4131SBruce Richardson 		goto fail;
22009ce4131SBruce Richardson 
22109ce4131SBruce Richardson 	TAILQ_INSERT_HEAD(&opt_loglevel_list, opt_ll, next);
22209ce4131SBruce Richardson 	return 0;
22309ce4131SBruce Richardson fail:
22409ce4131SBruce Richardson 	free(opt_ll);
22509ce4131SBruce Richardson 	return -1;
22609ce4131SBruce Richardson }
22709ce4131SBruce Richardson 
22809ce4131SBruce Richardson int
22909ce4131SBruce Richardson eal_log_save_regexp(const char *regex, uint32_t level)
23009ce4131SBruce Richardson {
23109ce4131SBruce Richardson 	return log_save_level(level, regex, NULL);
23209ce4131SBruce Richardson }
23309ce4131SBruce Richardson 
23409ce4131SBruce Richardson /* set log level based on globbing pattern */
23509ce4131SBruce Richardson int
23609ce4131SBruce Richardson rte_log_set_level_pattern(const char *pattern, uint32_t level)
23709ce4131SBruce Richardson {
23809ce4131SBruce Richardson 	size_t i;
23909ce4131SBruce Richardson 
24009ce4131SBruce Richardson 	if (level > RTE_LOG_MAX)
24109ce4131SBruce Richardson 		return -1;
24209ce4131SBruce Richardson 
24309ce4131SBruce Richardson 	for (i = 0; i < rte_logs.dynamic_types_len; i++) {
24409ce4131SBruce Richardson 		if (rte_logs.dynamic_types[i].name == NULL)
24509ce4131SBruce Richardson 			continue;
24609ce4131SBruce Richardson 
24709ce4131SBruce Richardson 		if (fnmatch(pattern, rte_logs.dynamic_types[i].name, 0) == 0)
24809ce4131SBruce Richardson 			logtype_set_level(i, level);
24909ce4131SBruce Richardson 	}
25009ce4131SBruce Richardson 
25109ce4131SBruce Richardson 	return 0;
25209ce4131SBruce Richardson }
25309ce4131SBruce Richardson 
25409ce4131SBruce Richardson int
25509ce4131SBruce Richardson eal_log_save_pattern(const char *pattern, uint32_t level)
25609ce4131SBruce Richardson {
25709ce4131SBruce Richardson 	return log_save_level(level, NULL, pattern);
25809ce4131SBruce Richardson }
25909ce4131SBruce Richardson 
26009ce4131SBruce Richardson /* get the current loglevel for the message being processed */
26109ce4131SBruce Richardson int rte_log_cur_msg_loglevel(void)
26209ce4131SBruce Richardson {
26309ce4131SBruce Richardson 	return RTE_PER_LCORE(log_cur_msg).loglevel;
26409ce4131SBruce Richardson }
26509ce4131SBruce Richardson 
26609ce4131SBruce Richardson /* get the current logtype for the message being processed */
26709ce4131SBruce Richardson int rte_log_cur_msg_logtype(void)
26809ce4131SBruce Richardson {
26909ce4131SBruce Richardson 	return RTE_PER_LCORE(log_cur_msg).logtype;
27009ce4131SBruce Richardson }
27109ce4131SBruce Richardson 
27209ce4131SBruce Richardson static int
27309ce4131SBruce Richardson log_lookup(const char *name)
27409ce4131SBruce Richardson {
27509ce4131SBruce Richardson 	size_t i;
27609ce4131SBruce Richardson 
27709ce4131SBruce Richardson 	for (i = 0; i < rte_logs.dynamic_types_len; i++) {
27809ce4131SBruce Richardson 		if (rte_logs.dynamic_types[i].name == NULL)
27909ce4131SBruce Richardson 			continue;
28009ce4131SBruce Richardson 		if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
28109ce4131SBruce Richardson 			return i;
28209ce4131SBruce Richardson 	}
28309ce4131SBruce Richardson 
28409ce4131SBruce Richardson 	return -1;
28509ce4131SBruce Richardson }
28609ce4131SBruce Richardson 
28709ce4131SBruce Richardson static int
28809ce4131SBruce Richardson log_register(const char *name, uint32_t level)
28909ce4131SBruce Richardson {
29009ce4131SBruce Richardson 	struct rte_log_dynamic_type *new_dynamic_types;
29109ce4131SBruce Richardson 	int id;
29209ce4131SBruce Richardson 
29309ce4131SBruce Richardson 	id = log_lookup(name);
29409ce4131SBruce Richardson 	if (id >= 0)
29509ce4131SBruce Richardson 		return id;
29609ce4131SBruce Richardson 
29709ce4131SBruce Richardson 	new_dynamic_types = realloc(rte_logs.dynamic_types,
29809ce4131SBruce Richardson 		sizeof(struct rte_log_dynamic_type) *
29909ce4131SBruce Richardson 		(rte_logs.dynamic_types_len + 1));
30009ce4131SBruce Richardson 	if (new_dynamic_types == NULL)
30109ce4131SBruce Richardson 		return -ENOMEM;
30209ce4131SBruce Richardson 	rte_logs.dynamic_types = new_dynamic_types;
30309ce4131SBruce Richardson 
30409ce4131SBruce Richardson 	id = rte_logs.dynamic_types_len;
30509ce4131SBruce Richardson 	memset(&rte_logs.dynamic_types[id], 0,
30609ce4131SBruce Richardson 		sizeof(rte_logs.dynamic_types[id]));
30709ce4131SBruce Richardson 	rte_logs.dynamic_types[id].name = strdup(name);
30809ce4131SBruce Richardson 	if (rte_logs.dynamic_types[id].name == NULL)
30909ce4131SBruce Richardson 		return -ENOMEM;
31009ce4131SBruce Richardson 	logtype_set_level(id, level);
31109ce4131SBruce Richardson 
31209ce4131SBruce Richardson 	rte_logs.dynamic_types_len++;
31309ce4131SBruce Richardson 
31409ce4131SBruce Richardson 	return id;
31509ce4131SBruce Richardson }
31609ce4131SBruce Richardson 
31709ce4131SBruce Richardson /* register an extended log type */
31809ce4131SBruce Richardson int
31909ce4131SBruce Richardson rte_log_register(const char *name)
32009ce4131SBruce Richardson {
32109ce4131SBruce Richardson 	return log_register(name, RTE_LOG_INFO);
32209ce4131SBruce Richardson }
32309ce4131SBruce Richardson 
32409ce4131SBruce Richardson /* Register an extended log type and try to pick its level from EAL options */
32509ce4131SBruce Richardson int
32609ce4131SBruce Richardson rte_log_register_type_and_pick_level(const char *name, uint32_t level_def)
32709ce4131SBruce Richardson {
32809ce4131SBruce Richardson 	struct rte_eal_opt_loglevel *opt_ll;
32909ce4131SBruce Richardson 	uint32_t level = level_def;
33009ce4131SBruce Richardson 
33109ce4131SBruce Richardson 	TAILQ_FOREACH(opt_ll, &opt_loglevel_list, next) {
33209ce4131SBruce Richardson 		if (opt_ll->level > RTE_LOG_MAX)
33309ce4131SBruce Richardson 			continue;
33409ce4131SBruce Richardson 
33509ce4131SBruce Richardson 		if (opt_ll->pattern) {
33609ce4131SBruce Richardson 			if (fnmatch(opt_ll->pattern, name, 0) == 0)
33709ce4131SBruce Richardson 				level = opt_ll->level;
33809ce4131SBruce Richardson 		} else {
33909ce4131SBruce Richardson 			if (regexec(&opt_ll->re_match, name, 0, NULL, 0) == 0)
34009ce4131SBruce Richardson 				level = opt_ll->level;
34109ce4131SBruce Richardson 		}
34209ce4131SBruce Richardson 	}
34309ce4131SBruce Richardson 
34409ce4131SBruce Richardson 	return log_register(name, level);
34509ce4131SBruce Richardson }
34609ce4131SBruce Richardson 
34709ce4131SBruce Richardson struct logtype {
34809ce4131SBruce Richardson 	uint32_t log_id;
34909ce4131SBruce Richardson 	const char *logtype;
35009ce4131SBruce Richardson };
35109ce4131SBruce Richardson 
35209ce4131SBruce Richardson static const struct logtype logtype_strings[] = {
35309ce4131SBruce Richardson 	{RTE_LOGTYPE_EAL,        "lib.eal"},
3543224af39SStephen Hemminger 
35509ce4131SBruce Richardson 	{RTE_LOGTYPE_USER1,      "user1"},
35609ce4131SBruce Richardson 	{RTE_LOGTYPE_USER2,      "user2"},
35709ce4131SBruce Richardson 	{RTE_LOGTYPE_USER3,      "user3"},
35809ce4131SBruce Richardson 	{RTE_LOGTYPE_USER4,      "user4"},
35909ce4131SBruce Richardson 	{RTE_LOGTYPE_USER5,      "user5"},
36009ce4131SBruce Richardson 	{RTE_LOGTYPE_USER6,      "user6"},
36109ce4131SBruce Richardson 	{RTE_LOGTYPE_USER7,      "user7"},
36209ce4131SBruce Richardson 	{RTE_LOGTYPE_USER8,      "user8"}
36309ce4131SBruce Richardson };
36409ce4131SBruce Richardson 
36509ce4131SBruce Richardson /* Logging should be first initializer (before drivers and bus) */
36609ce4131SBruce Richardson RTE_INIT_PRIO(log_init, LOG)
36709ce4131SBruce Richardson {
36809ce4131SBruce Richardson 	uint32_t i;
36909ce4131SBruce Richardson 
37009ce4131SBruce Richardson 	rte_log_set_global_level(RTE_LOG_DEBUG);
37109ce4131SBruce Richardson 
37209ce4131SBruce Richardson 	rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
37309ce4131SBruce Richardson 		sizeof(struct rte_log_dynamic_type));
37409ce4131SBruce Richardson 	if (rte_logs.dynamic_types == NULL)
37509ce4131SBruce Richardson 		return;
37609ce4131SBruce Richardson 
37709ce4131SBruce Richardson 	/* register legacy log types */
37809ce4131SBruce Richardson 	for (i = 0; i < RTE_DIM(logtype_strings); i++) {
37909ce4131SBruce Richardson 		rte_logs.dynamic_types[logtype_strings[i].log_id].name =
38009ce4131SBruce Richardson 			strdup(logtype_strings[i].logtype);
38109ce4131SBruce Richardson 		logtype_set_level(logtype_strings[i].log_id, RTE_LOG_INFO);
38209ce4131SBruce Richardson 	}
38309ce4131SBruce Richardson 
38409ce4131SBruce Richardson 	rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
38509ce4131SBruce Richardson }
38609ce4131SBruce Richardson 
38709ce4131SBruce Richardson const char *
38809ce4131SBruce Richardson eal_log_level2str(uint32_t level)
38909ce4131SBruce Richardson {
39009ce4131SBruce Richardson 	switch (level) {
39109ce4131SBruce Richardson 	case 0: return "disabled";
39209ce4131SBruce Richardson 	case RTE_LOG_EMERG: return "emergency";
39309ce4131SBruce Richardson 	case RTE_LOG_ALERT: return "alert";
39409ce4131SBruce Richardson 	case RTE_LOG_CRIT: return "critical";
39509ce4131SBruce Richardson 	case RTE_LOG_ERR: return "error";
39609ce4131SBruce Richardson 	case RTE_LOG_WARNING: return "warning";
39709ce4131SBruce Richardson 	case RTE_LOG_NOTICE: return "notice";
39809ce4131SBruce Richardson 	case RTE_LOG_INFO: return "info";
39909ce4131SBruce Richardson 	case RTE_LOG_DEBUG: return "debug";
40009ce4131SBruce Richardson 	default: return "unknown";
40109ce4131SBruce Richardson 	}
40209ce4131SBruce Richardson }
40309ce4131SBruce Richardson 
40409ce4131SBruce Richardson static int
40509ce4131SBruce Richardson log_type_compare(const void *a, const void *b)
40609ce4131SBruce Richardson {
40709ce4131SBruce Richardson 	const struct rte_log_dynamic_type *type_a = a;
40809ce4131SBruce Richardson 	const struct rte_log_dynamic_type *type_b = b;
40909ce4131SBruce Richardson 
41009ce4131SBruce Richardson 	if (type_a->name == NULL && type_b->name == NULL)
41109ce4131SBruce Richardson 		return 0;
41209ce4131SBruce Richardson 	if (type_a->name == NULL)
41309ce4131SBruce Richardson 		return -1;
41409ce4131SBruce Richardson 	if (type_b->name == NULL)
41509ce4131SBruce Richardson 		return 1;
41609ce4131SBruce Richardson 	return strcmp(type_a->name, type_b->name);
41709ce4131SBruce Richardson }
41809ce4131SBruce Richardson 
41909ce4131SBruce Richardson /* Dump name of each logtype, one per line. */
42009ce4131SBruce Richardson void
42109ce4131SBruce Richardson rte_log_list_types(FILE *out, const char *prefix)
42209ce4131SBruce Richardson {
42309ce4131SBruce Richardson 	struct rte_log_dynamic_type *sorted_types;
42409ce4131SBruce Richardson 	const size_t type_size = sizeof(rte_logs.dynamic_types[0]);
42509ce4131SBruce Richardson 	const size_t type_count = rte_logs.dynamic_types_len;
42609ce4131SBruce Richardson 	const size_t total_size = type_size * type_count;
42709ce4131SBruce Richardson 	size_t type;
42809ce4131SBruce Richardson 
42909ce4131SBruce Richardson 	sorted_types = malloc(total_size);
43009ce4131SBruce Richardson 	if (sorted_types == NULL) {
43109ce4131SBruce Richardson 		/* no sorting - unlikely */
43209ce4131SBruce Richardson 		sorted_types = rte_logs.dynamic_types;
43309ce4131SBruce Richardson 	} else {
43409ce4131SBruce Richardson 		memcpy(sorted_types, rte_logs.dynamic_types, total_size);
43509ce4131SBruce Richardson 		qsort(sorted_types, type_count, type_size, log_type_compare);
43609ce4131SBruce Richardson 	}
43709ce4131SBruce Richardson 
43809ce4131SBruce Richardson 	for (type = 0; type < type_count; ++type) {
43909ce4131SBruce Richardson 		if (sorted_types[type].name == NULL)
44009ce4131SBruce Richardson 			continue;
44109ce4131SBruce Richardson 		fprintf(out, "%s%s\n", prefix, sorted_types[type].name);
44209ce4131SBruce Richardson 	}
44309ce4131SBruce Richardson 
44409ce4131SBruce Richardson 	if (sorted_types != rte_logs.dynamic_types)
44509ce4131SBruce Richardson 		free(sorted_types);
44609ce4131SBruce Richardson }
44709ce4131SBruce Richardson 
44809ce4131SBruce Richardson /* dump global level and registered log types */
44909ce4131SBruce Richardson void
45009ce4131SBruce Richardson rte_log_dump(FILE *f)
45109ce4131SBruce Richardson {
45209ce4131SBruce Richardson 	size_t i;
45309ce4131SBruce Richardson 
45409ce4131SBruce Richardson 	fprintf(f, "global log level is %s\n",
45509ce4131SBruce Richardson 		eal_log_level2str(rte_log_get_global_level()));
45609ce4131SBruce Richardson 
45709ce4131SBruce Richardson 	for (i = 0; i < rte_logs.dynamic_types_len; i++) {
45809ce4131SBruce Richardson 		if (rte_logs.dynamic_types[i].name == NULL)
45909ce4131SBruce Richardson 			continue;
46009ce4131SBruce Richardson 		fprintf(f, "id %zu: %s, level is %s\n",
46109ce4131SBruce Richardson 			i, rte_logs.dynamic_types[i].name,
46209ce4131SBruce Richardson 			eal_log_level2str(rte_logs.dynamic_types[i].loglevel));
46309ce4131SBruce Richardson 	}
46409ce4131SBruce Richardson }
46509ce4131SBruce Richardson 
46609ce4131SBruce Richardson /*
46709ce4131SBruce Richardson  * Generates a log message The message will be sent in the stream
46809ce4131SBruce Richardson  * defined by the previous call to rte_openlog_stream().
46909ce4131SBruce Richardson  */
47009ce4131SBruce Richardson int
47109ce4131SBruce Richardson rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
47209ce4131SBruce Richardson {
47309ce4131SBruce Richardson 	FILE *f = rte_log_get_stream();
47409ce4131SBruce Richardson 	int ret;
47509ce4131SBruce Richardson 
47609ce4131SBruce Richardson 	if (logtype >= rte_logs.dynamic_types_len)
47709ce4131SBruce Richardson 		return -1;
47809ce4131SBruce Richardson 	if (!rte_log_can_log(logtype, level))
47909ce4131SBruce Richardson 		return 0;
48009ce4131SBruce Richardson 
48109ce4131SBruce Richardson 	/* save loglevel and logtype in a global per-lcore variable */
48209ce4131SBruce Richardson 	RTE_PER_LCORE(log_cur_msg).loglevel = level;
48309ce4131SBruce Richardson 	RTE_PER_LCORE(log_cur_msg).logtype = logtype;
48409ce4131SBruce Richardson 
485630cdfcdSStephen Hemminger 	ret = (*rte_logs.print_func)(f, format, ap);
48609ce4131SBruce Richardson 	fflush(f);
48709ce4131SBruce Richardson 	return ret;
48809ce4131SBruce Richardson }
48909ce4131SBruce Richardson 
49009ce4131SBruce Richardson /*
49109ce4131SBruce Richardson  * Generates a log message The message will be sent in the stream
49209ce4131SBruce Richardson  * defined by the previous call to rte_openlog_stream().
49309ce4131SBruce Richardson  * No need to check level here, done by rte_vlog().
49409ce4131SBruce Richardson  */
49509ce4131SBruce Richardson int
49609ce4131SBruce Richardson rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
49709ce4131SBruce Richardson {
49809ce4131SBruce Richardson 	va_list ap;
49909ce4131SBruce Richardson 	int ret;
50009ce4131SBruce Richardson 
50109ce4131SBruce Richardson 	va_start(ap, format);
50209ce4131SBruce Richardson 	ret = rte_vlog(level, logtype, format, ap);
50309ce4131SBruce Richardson 	va_end(ap);
50409ce4131SBruce Richardson 	return ret;
50509ce4131SBruce Richardson }
50609ce4131SBruce Richardson 
50709ce4131SBruce Richardson /*
50898513036SStephen Hemminger  * Called by rte_eal_init
50909ce4131SBruce Richardson  */
51009ce4131SBruce Richardson void
51198513036SStephen Hemminger eal_log_init(const char *id)
51209ce4131SBruce Richardson {
5139da0dc6cSStephen Hemminger 	/* If user has already set a log stream, then use it. */
5149da0dc6cSStephen Hemminger 	if (rte_logs.file == NULL) {
51598513036SStephen Hemminger 		FILE *logf = NULL;
51698513036SStephen Hemminger 
5179da0dc6cSStephen Hemminger 		/* if stderr is associated with systemd environment */
5189da0dc6cSStephen Hemminger 		if (log_journal_enabled())
5199da0dc6cSStephen Hemminger 			logf = log_journal_open(id);
5209da0dc6cSStephen Hemminger 		/* If --syslog option was passed */
5219da0dc6cSStephen Hemminger 		else if (log_syslog_enabled())
52298513036SStephen Hemminger 			logf = log_syslog_open(id);
52398513036SStephen Hemminger 
5249da0dc6cSStephen Hemminger 		/* if either syslog or journal is used, then no special handling */
525259f6f78SStephen Hemminger 		if (logf) {
52698513036SStephen Hemminger 			rte_openlog_stream(logf);
527*ba9fb279SRobin Jarry 			rte_logs.is_internal_file = true;
528259f6f78SStephen Hemminger 		} else {
529259f6f78SStephen Hemminger 			bool is_terminal = isatty(fileno(stderr));
530259f6f78SStephen Hemminger 			bool use_color = log_color_enabled(is_terminal);
531259f6f78SStephen Hemminger 
532259f6f78SStephen Hemminger 			if (log_timestamp_enabled()) {
533259f6f78SStephen Hemminger 				if (use_color)
534259f6f78SStephen Hemminger 					rte_logs.print_func = color_print_with_timestamp;
535259f6f78SStephen Hemminger 				else
53662ae1149SStephen Hemminger 					rte_logs.print_func = log_print_with_timestamp;
537259f6f78SStephen Hemminger 			} else {
538259f6f78SStephen Hemminger 				if (use_color)
539259f6f78SStephen Hemminger 					rte_logs.print_func = color_print;
54062ae1149SStephen Hemminger 				else
54162ae1149SStephen Hemminger 					rte_logs.print_func = vfprintf;
5429da0dc6cSStephen Hemminger 			}
543259f6f78SStephen Hemminger 		}
544259f6f78SStephen Hemminger 	}
54562ae1149SStephen Hemminger 
54609ce4131SBruce Richardson #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
54709ce4131SBruce Richardson 	RTE_LOG(NOTICE, EAL,
54809ce4131SBruce Richardson 		"Debug dataplane logs available - lower performance\n");
54909ce4131SBruce Richardson #endif
55009ce4131SBruce Richardson }
55109ce4131SBruce Richardson 
55209ce4131SBruce Richardson /*
55309ce4131SBruce Richardson  * Called by eal_cleanup
55409ce4131SBruce Richardson  */
55509ce4131SBruce Richardson void
55609ce4131SBruce Richardson rte_eal_log_cleanup(void)
55709ce4131SBruce Richardson {
558*ba9fb279SRobin Jarry 	if (rte_logs.is_internal_file && rte_logs.file != NULL)
559*ba9fb279SRobin Jarry 		fclose(rte_logs.file);
56098513036SStephen Hemminger 	rte_logs.file = NULL;
561*ba9fb279SRobin Jarry 	rte_logs.is_internal_file = false;
56209ce4131SBruce Richardson }
563