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_set_log_level(uint32_t level)
89 rte_logs.level = (uint32_t)level;
92 /* Get global log level */
94 rte_get_log_level(void)
96 return rte_logs.level;
99 /* Set global log type */
101 rte_set_log_type(uint32_t type, int enable)
103 if (type < RTE_LOGTYPE_FIRST_EXT_ID) {
105 rte_logs.type |= type;
107 rte_logs.type &= ~type;
111 rte_log_set_level(type, 0);
113 rte_log_set_level(type, RTE_LOG_DEBUG);
116 /* Get global log type */
118 rte_get_log_type(void)
120 return rte_logs.type;
124 rte_log_set_level(uint32_t type, uint32_t level)
126 if (type >= rte_logs.dynamic_types_len)
128 if (level > RTE_LOG_DEBUG)
131 rte_logs.dynamic_types[type].loglevel = level;
138 rte_log_set_level_regexp(const char *pattern, uint32_t level)
143 if (level > RTE_LOG_DEBUG)
146 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
147 if (rte_logs.dynamic_types[i].name == NULL)
149 if (regexec(&r, pattern, 0, NULL, 0) == 0)
150 rte_logs.dynamic_types[i].loglevel = level;
156 /* get the current loglevel for the message beeing processed */
157 int rte_log_cur_msg_loglevel(void)
159 return RTE_PER_LCORE(log_cur_msg).loglevel;
162 /* get the current logtype for the message beeing processed */
163 int rte_log_cur_msg_logtype(void)
165 return RTE_PER_LCORE(log_cur_msg).logtype;
169 rte_log_lookup(const char *name)
173 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
174 if (rte_logs.dynamic_types[i].name == NULL)
176 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
183 /* register an extended log type, assuming table is large enough, and id
184 * is not yet registered.
187 __rte_log_register(const char *name, int id)
189 char *dup_name = strdup(name);
191 if (dup_name == NULL)
194 rte_logs.dynamic_types[id].name = dup_name;
195 rte_logs.dynamic_types[id].loglevel = RTE_LOG_DEBUG;
200 /* register an extended log type */
202 rte_log_register(const char *name)
204 struct rte_log_dynamic_type *new_dynamic_types;
207 id = rte_log_lookup(name);
211 new_dynamic_types = realloc(rte_logs.dynamic_types,
212 sizeof(struct rte_log_dynamic_type) *
213 (rte_logs.dynamic_types_len + 1));
214 if (new_dynamic_types == NULL)
216 rte_logs.dynamic_types = new_dynamic_types;
218 ret = __rte_log_register(name, rte_logs.dynamic_types_len);
222 rte_logs.dynamic_types_len++;
227 RTE_INIT(rte_log_init);
231 rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
232 sizeof(struct rte_log_dynamic_type));
233 if (rte_logs.dynamic_types == NULL)
236 /* register legacy log types, keep sync'd with RTE_LOGTYPE_* */
237 __rte_log_register("eal", 0);
238 __rte_log_register("malloc", 1);
239 __rte_log_register("ring", 2);
240 __rte_log_register("mempool", 3);
241 __rte_log_register("timer", 4);
242 __rte_log_register("pmd", 5);
243 __rte_log_register("hash", 6);
244 __rte_log_register("lpm", 7);
245 __rte_log_register("kni", 8);
246 __rte_log_register("acl", 9);
247 __rte_log_register("power", 10);
248 __rte_log_register("meter", 11);
249 __rte_log_register("sched", 12);
250 __rte_log_register("port", 13);
251 __rte_log_register("table", 14);
252 __rte_log_register("pipeline", 15);
253 __rte_log_register("mbuf", 16);
254 __rte_log_register("cryptodev", 17);
255 __rte_log_register("user1", 24);
256 __rte_log_register("user2", 25);
257 __rte_log_register("user3", 26);
258 __rte_log_register("user4", 27);
259 __rte_log_register("user5", 28);
260 __rte_log_register("user6", 29);
261 __rte_log_register("user7", 30);
262 __rte_log_register("user8", 31);
264 rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
268 loglevel_to_string(uint32_t level)
271 case RTE_LOG_EMERG: return "emerg";
272 case RTE_LOG_ALERT: return "alert";
273 case RTE_LOG_CRIT: return "critical";
274 case RTE_LOG_ERR: return "error";
275 case RTE_LOG_WARNING: return "warning";
276 case RTE_LOG_NOTICE: return "notice";
277 case RTE_LOG_INFO: return "info";
278 case RTE_LOG_DEBUG: return "debug";
279 default: return "unknown";
283 /* dump global level and registered log types */
285 rte_log_dump(FILE *f)
289 fprintf(f, "global log level is %s\n",
290 loglevel_to_string(rte_get_log_level()));
292 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
293 if (rte_logs.dynamic_types[i].name == NULL)
295 fprintf(f, "id %zu: %s, level is %s\n",
296 i, rte_logs.dynamic_types[i].name,
297 loglevel_to_string(rte_logs.dynamic_types[i].loglevel));
302 * Generates a log message The message will be sent in the stream
303 * defined by the previous call to rte_openlog_stream().
306 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
309 FILE *f = rte_logs.file;
311 f = default_log_stream;
314 * Grab the current value of stderr here, rather than
315 * just initializing default_log_stream to stderr. This
316 * ensures that we will always use the current value
317 * of stderr, even if the application closes and
324 if (level > rte_logs.level)
326 if (logtype >= rte_logs.dynamic_types_len)
328 if (level > rte_logs.dynamic_types[logtype].loglevel)
331 /* save loglevel and logtype in a global per-lcore variable */
332 RTE_PER_LCORE(log_cur_msg).loglevel = level;
333 RTE_PER_LCORE(log_cur_msg).logtype = logtype;
335 ret = vfprintf(f, format, ap);
341 * Generates a log message The message will be sent in the stream
342 * defined by the previous call to rte_openlog_stream().
343 * No need to check level here, done by rte_vlog().
346 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
351 va_start(ap, format);
352 ret = rte_vlog(level, logtype, format, ap);
358 * Called by environment-specific initialization functions.
361 eal_log_set_default(FILE *default_log)
363 default_log_stream = default_log;
365 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
367 "Debug dataplane logs available - lower performance\n");