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 /* Get global log level */
94 rte_log_get_global_level(void)
96 return rte_logs.level;
100 rte_log_get_level(uint32_t type)
102 if (type >= rte_logs.dynamic_types_len)
105 return rte_logs.dynamic_types[type].loglevel;
109 rte_log_set_level(uint32_t type, uint32_t level)
111 if (type >= rte_logs.dynamic_types_len)
113 if (level > RTE_LOG_DEBUG)
116 rte_logs.dynamic_types[type].loglevel = level;
123 rte_log_set_level_regexp(const char *pattern, uint32_t level)
128 if (level > RTE_LOG_DEBUG)
131 if (regcomp(&r, pattern, 0) != 0)
134 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
135 if (rte_logs.dynamic_types[i].name == NULL)
137 if (regexec(&r, rte_logs.dynamic_types[i].name, 0,
139 rte_logs.dynamic_types[i].loglevel = level;
147 /* get the current loglevel for the message being processed */
148 int rte_log_cur_msg_loglevel(void)
150 return RTE_PER_LCORE(log_cur_msg).loglevel;
153 /* get the current logtype for the message being processed */
154 int rte_log_cur_msg_logtype(void)
156 return RTE_PER_LCORE(log_cur_msg).logtype;
160 rte_log_lookup(const char *name)
164 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
165 if (rte_logs.dynamic_types[i].name == NULL)
167 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
174 /* register an extended log type, assuming table is large enough, and id
175 * is not yet registered.
178 __rte_log_register(const char *name, int id)
180 char *dup_name = strdup(name);
182 if (dup_name == NULL)
185 rte_logs.dynamic_types[id].name = dup_name;
186 rte_logs.dynamic_types[id].loglevel = RTE_LOG_DEBUG;
191 /* register an extended log type */
193 rte_log_register(const char *name)
195 struct rte_log_dynamic_type *new_dynamic_types;
198 id = rte_log_lookup(name);
202 new_dynamic_types = realloc(rte_logs.dynamic_types,
203 sizeof(struct rte_log_dynamic_type) *
204 (rte_logs.dynamic_types_len + 1));
205 if (new_dynamic_types == NULL)
207 rte_logs.dynamic_types = new_dynamic_types;
209 ret = __rte_log_register(name, rte_logs.dynamic_types_len);
213 rte_logs.dynamic_types_len++;
223 static const struct logtype logtype_strings[] = {
224 {RTE_LOGTYPE_EAL, "eal"},
225 {RTE_LOGTYPE_MALLOC, "malloc"},
226 {RTE_LOGTYPE_RING, "ring"},
227 {RTE_LOGTYPE_MEMPOOL, "mempool"},
228 {RTE_LOGTYPE_TIMER, "timer"},
229 {RTE_LOGTYPE_PMD, "pmd"},
230 {RTE_LOGTYPE_HASH, "hash"},
231 {RTE_LOGTYPE_LPM, "lpm"},
232 {RTE_LOGTYPE_KNI, "kni"},
233 {RTE_LOGTYPE_ACL, "acl"},
234 {RTE_LOGTYPE_POWER, "power"},
235 {RTE_LOGTYPE_METER, "meter"},
236 {RTE_LOGTYPE_SCHED, "sched"},
237 {RTE_LOGTYPE_PORT, "port"},
238 {RTE_LOGTYPE_TABLE, "table"},
239 {RTE_LOGTYPE_PIPELINE, "pipeline"},
240 {RTE_LOGTYPE_MBUF, "mbuf"},
241 {RTE_LOGTYPE_CRYPTODEV, "cryptodev"},
242 {RTE_LOGTYPE_EFD, "efd"},
243 {RTE_LOGTYPE_EVENTDEV, "eventdev"},
244 {RTE_LOGTYPE_USER1, "user1"},
245 {RTE_LOGTYPE_USER2, "user2"},
246 {RTE_LOGTYPE_USER3, "user3"},
247 {RTE_LOGTYPE_USER4, "user4"},
248 {RTE_LOGTYPE_USER5, "user5"},
249 {RTE_LOGTYPE_USER6, "user6"},
250 {RTE_LOGTYPE_USER7, "user7"},
251 {RTE_LOGTYPE_USER8, "user8"}
254 /* Logging should be first initializer (before drivers and bus) */
255 RTE_INIT_PRIO(rte_log_init, 101);
261 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
262 rte_log_set_global_level(RTE_LOG_INFO);
264 rte_log_set_global_level(RTE_LOG_LEVEL);
267 rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
268 sizeof(struct rte_log_dynamic_type));
269 if (rte_logs.dynamic_types == NULL)
272 /* register legacy log types */
273 for (i = 0; i < RTE_DIM(logtype_strings); i++)
274 __rte_log_register(logtype_strings[i].logtype,
275 logtype_strings[i].log_id);
277 rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
281 loglevel_to_string(uint32_t level)
284 case 0: return "disabled";
285 case RTE_LOG_EMERG: return "emerg";
286 case RTE_LOG_ALERT: return "alert";
287 case RTE_LOG_CRIT: return "critical";
288 case RTE_LOG_ERR: return "error";
289 case RTE_LOG_WARNING: return "warning";
290 case RTE_LOG_NOTICE: return "notice";
291 case RTE_LOG_INFO: return "info";
292 case RTE_LOG_DEBUG: return "debug";
293 default: return "unknown";
297 /* dump global level and registered log types */
299 rte_log_dump(FILE *f)
303 fprintf(f, "global log level is %s\n",
304 loglevel_to_string(rte_log_get_global_level()));
306 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
307 if (rte_logs.dynamic_types[i].name == NULL)
309 fprintf(f, "id %zu: %s, level is %s\n",
310 i, rte_logs.dynamic_types[i].name,
311 loglevel_to_string(rte_logs.dynamic_types[i].loglevel));
316 * Generates a log message The message will be sent in the stream
317 * defined by the previous call to rte_openlog_stream().
320 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
323 FILE *f = rte_logs.file;
325 f = default_log_stream;
328 * Grab the current value of stderr here, rather than
329 * just initializing default_log_stream to stderr. This
330 * ensures that we will always use the current value
331 * of stderr, even if the application closes and
338 if (level > rte_logs.level)
340 if (logtype >= rte_logs.dynamic_types_len)
342 if (level > rte_logs.dynamic_types[logtype].loglevel)
345 /* save loglevel and logtype in a global per-lcore variable */
346 RTE_PER_LCORE(log_cur_msg).loglevel = level;
347 RTE_PER_LCORE(log_cur_msg).logtype = logtype;
349 ret = vfprintf(f, format, ap);
355 * Generates a log message The message will be sent in the stream
356 * defined by the previous call to rte_openlog_stream().
357 * No need to check level here, done by rte_vlog().
360 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
365 va_start(ap, format);
366 ret = rte_vlog(level, logtype, format, ap);
372 * Called by environment-specific initialization functions.
375 eal_log_set_default(FILE *default_log)
377 default_log_stream = default_log;
379 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
381 "Debug dataplane logs available - lower performance\n");