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.
43 #include <rte_per_lcore.h>
45 #include "eal_private.h"
47 /* global log structure */
48 struct rte_logs rte_logs = {
50 .level = RTE_LOG_DEBUG,
54 /* Stream to use for logging if rte_logs.file is NULL */
55 static FILE *default_log_stream;
58 * This global structure stores some informations about the message
59 * that is currently being processed by one lcore
62 uint32_t loglevel; /**< log level - see rte_log.h */
63 uint32_t logtype; /**< log type - see rte_log.h */
66 struct rte_log_dynamic_type {
72 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
76 /* Change the stream that will be used by logging system */
78 rte_openlog_stream(FILE *f)
84 /* Set global log level */
86 rte_set_log_level(uint32_t level)
88 rte_logs.level = (uint32_t)level;
91 /* Get global log level */
93 rte_get_log_level(void)
95 return rte_logs.level;
98 /* Set global log type */
100 rte_set_log_type(uint32_t type, int enable)
102 if (type < RTE_LOGTYPE_FIRST_EXT_ID) {
104 rte_logs.type |= type;
106 rte_logs.type &= ~type;
110 rte_log_set_level(type, 0);
112 rte_log_set_level(type, RTE_LOG_DEBUG);
115 /* Get global log type */
117 rte_get_log_type(void)
119 return rte_logs.type;
123 rte_log_set_level(uint32_t type, uint32_t level)
125 if (type >= rte_logs.dynamic_types_len)
127 if (level > RTE_LOG_DEBUG)
130 rte_logs.dynamic_types[type].loglevel = level;
135 /* get the current loglevel for the message beeing processed */
136 int rte_log_cur_msg_loglevel(void)
138 return RTE_PER_LCORE(log_cur_msg).loglevel;
141 /* get the current logtype for the message beeing processed */
142 int rte_log_cur_msg_logtype(void)
144 return RTE_PER_LCORE(log_cur_msg).logtype;
148 rte_log_lookup(const char *name)
152 for (i = 0; i < rte_logs.dynamic_types_len; i++) {
153 if (rte_logs.dynamic_types[i].name == NULL)
155 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
162 /* register an extended log type, assuming table is large enough, and id
163 * is not yet registered.
166 __rte_log_register(const char *name, int id)
168 char *dup_name = strdup(name);
170 if (dup_name == NULL)
173 rte_logs.dynamic_types[id].name = dup_name;
174 rte_logs.dynamic_types[id].loglevel = RTE_LOG_DEBUG;
179 /* register an extended log type */
181 rte_log_register(const char *name)
183 struct rte_log_dynamic_type *new_dynamic_types;
186 id = rte_log_lookup(name);
190 new_dynamic_types = realloc(rte_logs.dynamic_types,
191 sizeof(struct rte_log_dynamic_type) *
192 (rte_logs.dynamic_types_len + 1));
193 if (new_dynamic_types == NULL)
195 rte_logs.dynamic_types = new_dynamic_types;
197 ret = __rte_log_register(name, rte_logs.dynamic_types_len);
201 rte_logs.dynamic_types_len++;
206 RTE_INIT(rte_log_init);
210 rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
211 sizeof(struct rte_log_dynamic_type));
212 if (rte_logs.dynamic_types == NULL)
215 /* register legacy log types, keep sync'd with RTE_LOGTYPE_* */
216 __rte_log_register("eal", 0);
217 __rte_log_register("malloc", 1);
218 __rte_log_register("ring", 2);
219 __rte_log_register("mempool", 3);
220 __rte_log_register("timer", 4);
221 __rte_log_register("pmd", 5);
222 __rte_log_register("hash", 6);
223 __rte_log_register("lpm", 7);
224 __rte_log_register("kni", 8);
225 __rte_log_register("acl", 9);
226 __rte_log_register("power", 10);
227 __rte_log_register("meter", 11);
228 __rte_log_register("sched", 12);
229 __rte_log_register("port", 13);
230 __rte_log_register("table", 14);
231 __rte_log_register("pipeline", 15);
232 __rte_log_register("mbuf", 16);
233 __rte_log_register("cryptodev", 17);
234 __rte_log_register("user1", 24);
235 __rte_log_register("user2", 25);
236 __rte_log_register("user3", 26);
237 __rte_log_register("user4", 27);
238 __rte_log_register("user5", 28);
239 __rte_log_register("user6", 29);
240 __rte_log_register("user7", 30);
241 __rte_log_register("user8", 31);
243 rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
247 * Generates a log message The message will be sent in the stream
248 * defined by the previous call to rte_openlog_stream().
251 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
254 FILE *f = rte_logs.file;
256 f = default_log_stream;
259 * Grab the current value of stderr here, rather than
260 * just initializing default_log_stream to stderr. This
261 * ensures that we will always use the current value
262 * of stderr, even if the application closes and
269 if (level > rte_logs.level)
271 if (logtype >= rte_logs.dynamic_types_len)
273 if (level > rte_logs.dynamic_types[logtype].loglevel)
276 /* save loglevel and logtype in a global per-lcore variable */
277 RTE_PER_LCORE(log_cur_msg).loglevel = level;
278 RTE_PER_LCORE(log_cur_msg).logtype = logtype;
280 ret = vfprintf(f, format, ap);
286 * Generates a log message The message will be sent in the stream
287 * defined by the previous call to rte_openlog_stream().
288 * No need to check level here, done by rte_vlog().
291 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
296 va_start(ap, format);
297 ret = rte_vlog(level, logtype, format, ap);
303 * Called by environment-specific initialization functions.
306 eal_log_set_default(FILE *default_log)
308 default_log_stream = default_log;
310 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
312 "Debug dataplane logs available - lower performance\n");