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;
145 /* get the current loglevel for the message being processed */
146 int rte_log_cur_msg_loglevel(void)
148 return RTE_PER_LCORE(log_cur_msg).loglevel;
151 /* get the current logtype for the message being processed */
152 int rte_log_cur_msg_logtype(void)
154 return RTE_PER_LCORE(log_cur_msg).logtype;
158 rte_log_lookup(const char *name)
162 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
163 if (rte_logs.dynamic_types[i].name == NULL)
165 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
172 /* register an extended log type, assuming table is large enough, and id
173 * is not yet registered.
176 __rte_log_register(const char *name, int id)
178 char *dup_name = strdup(name);
180 if (dup_name == NULL)
183 rte_logs.dynamic_types[id].name = dup_name;
184 rte_logs.dynamic_types[id].loglevel = RTE_LOG_DEBUG;
189 /* register an extended log type */
191 rte_log_register(const char *name)
193 struct rte_log_dynamic_type *new_dynamic_types;
196 id = rte_log_lookup(name);
200 new_dynamic_types = realloc(rte_logs.dynamic_types,
201 sizeof(struct rte_log_dynamic_type) *
202 (rte_logs.dynamic_types_len + 1));
203 if (new_dynamic_types == NULL)
205 rte_logs.dynamic_types = new_dynamic_types;
207 ret = __rte_log_register(name, rte_logs.dynamic_types_len);
211 rte_logs.dynamic_types_len++;
221 static const struct logtype logtype_strings[] = {
222 {RTE_LOGTYPE_EAL, "eal"},
223 {RTE_LOGTYPE_MALLOC, "malloc"},
224 {RTE_LOGTYPE_RING, "ring"},
225 {RTE_LOGTYPE_MEMPOOL, "mempool"},
226 {RTE_LOGTYPE_TIMER, "timer"},
227 {RTE_LOGTYPE_PMD, "pmd"},
228 {RTE_LOGTYPE_HASH, "hash"},
229 {RTE_LOGTYPE_LPM, "lpm"},
230 {RTE_LOGTYPE_KNI, "kni"},
231 {RTE_LOGTYPE_ACL, "acl"},
232 {RTE_LOGTYPE_POWER, "power"},
233 {RTE_LOGTYPE_METER, "meter"},
234 {RTE_LOGTYPE_SCHED, "sched"},
235 {RTE_LOGTYPE_PORT, "port"},
236 {RTE_LOGTYPE_TABLE, "table"},
237 {RTE_LOGTYPE_PIPELINE, "pipeline"},
238 {RTE_LOGTYPE_MBUF, "mbuf"},
239 {RTE_LOGTYPE_CRYPTODEV, "cryptodev"},
240 {RTE_LOGTYPE_EFD, "efd"},
241 {RTE_LOGTYPE_EVENTDEV, "eventdev"},
242 {RTE_LOGTYPE_USER1, "user1"},
243 {RTE_LOGTYPE_USER2, "user2"},
244 {RTE_LOGTYPE_USER3, "user3"},
245 {RTE_LOGTYPE_USER4, "user4"},
246 {RTE_LOGTYPE_USER5, "user5"},
247 {RTE_LOGTYPE_USER6, "user6"},
248 {RTE_LOGTYPE_USER7, "user7"},
249 {RTE_LOGTYPE_USER8, "user8"}
252 /* Logging should be first initializer (before drivers and bus) */
253 RTE_INIT_PRIO(rte_log_init, 101);
259 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
260 rte_log_set_global_level(RTE_LOG_INFO);
262 rte_log_set_global_level(RTE_LOG_LEVEL);
265 rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
266 sizeof(struct rte_log_dynamic_type));
267 if (rte_logs.dynamic_types == NULL)
270 /* register legacy log types */
271 for (i = 0; i < RTE_DIM(logtype_strings); i++)
272 __rte_log_register(logtype_strings[i].logtype,
273 logtype_strings[i].log_id);
275 rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
279 loglevel_to_string(uint32_t level)
282 case 0: return "disabled";
283 case RTE_LOG_EMERG: return "emerg";
284 case RTE_LOG_ALERT: return "alert";
285 case RTE_LOG_CRIT: return "critical";
286 case RTE_LOG_ERR: return "error";
287 case RTE_LOG_WARNING: return "warning";
288 case RTE_LOG_NOTICE: return "notice";
289 case RTE_LOG_INFO: return "info";
290 case RTE_LOG_DEBUG: return "debug";
291 default: return "unknown";
295 /* dump global level and registered log types */
297 rte_log_dump(FILE *f)
301 fprintf(f, "global log level is %s\n",
302 loglevel_to_string(rte_log_get_global_level()));
304 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
305 if (rte_logs.dynamic_types[i].name == NULL)
307 fprintf(f, "id %zu: %s, level is %s\n",
308 i, rte_logs.dynamic_types[i].name,
309 loglevel_to_string(rte_logs.dynamic_types[i].loglevel));
314 * Generates a log message The message will be sent in the stream
315 * defined by the previous call to rte_openlog_stream().
318 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
321 FILE *f = rte_logs.file;
323 f = default_log_stream;
326 * Grab the current value of stderr here, rather than
327 * just initializing default_log_stream to stderr. This
328 * ensures that we will always use the current value
329 * of stderr, even if the application closes and
336 if (level > rte_logs.level)
338 if (logtype >= rte_logs.dynamic_types_len)
340 if (level > rte_logs.dynamic_types[logtype].loglevel)
343 /* save loglevel and logtype in a global per-lcore variable */
344 RTE_PER_LCORE(log_cur_msg).loglevel = level;
345 RTE_PER_LCORE(log_cur_msg).logtype = logtype;
347 ret = vfprintf(f, format, ap);
353 * Generates a log message The message will be sent in the stream
354 * defined by the previous call to rte_openlog_stream().
355 * No need to check level here, done by rte_vlog().
358 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
363 va_start(ap, format);
364 ret = rte_vlog(level, logtype, format, ap);
370 * Called by environment-specific initialization functions.
373 eal_log_set_default(FILE *default_log)
375 default_log_stream = default_log;
377 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
379 "Debug dataplane logs available - lower performance\n");