d0268939093b796ad740813b042735196372c55b
[dpdk.git] / lib / librte_eal / common / eal_common_log.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <regex.h>
41
42 #include <rte_eal.h>
43 #include <rte_log.h>
44 #include <rte_per_lcore.h>
45
46 #include "eal_private.h"
47
48 /* global log structure */
49 struct rte_logs rte_logs = {
50         .type = ~0,
51         .level = RTE_LOG_DEBUG,
52         .file = NULL,
53 };
54
55 /* Stream to use for logging if rte_logs.file is NULL */
56 static FILE *default_log_stream;
57
58 /**
59  * This global structure stores some informations about the message
60  * that is currently being processed by one lcore
61  */
62 struct log_cur_msg {
63         uint32_t loglevel; /**< log level - see rte_log.h */
64         uint32_t logtype;  /**< log type  - see rte_log.h */
65 };
66
67 struct rte_log_dynamic_type {
68         const char *name;
69         uint32_t loglevel;
70 };
71
72  /* per core log */
73 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
74
75 /* default logs */
76
77 /* Change the stream that will be used by logging system */
78 int
79 rte_openlog_stream(FILE *f)
80 {
81         rte_logs.file = f;
82         return 0;
83 }
84
85 /* Set global log level */
86 void
87 rte_set_log_level(uint32_t level)
88 {
89         rte_logs.level = (uint32_t)level;
90 }
91
92 /* Get global log level */
93 uint32_t
94 rte_get_log_level(void)
95 {
96         return rte_logs.level;
97 }
98
99 /* Set global log type */
100 void
101 rte_set_log_type(uint32_t type, int enable)
102 {
103         if (type < RTE_LOGTYPE_FIRST_EXT_ID) {
104                 if (enable)
105                         rte_logs.type |= type;
106                 else
107                         rte_logs.type &= ~type;
108         }
109
110         if (enable)
111                 rte_log_set_level(type, 0);
112         else
113                 rte_log_set_level(type, RTE_LOG_DEBUG);
114 }
115
116 /* Get global log type */
117 uint32_t
118 rte_get_log_type(void)
119 {
120         return rte_logs.type;
121 }
122
123 int
124 rte_log_set_level(uint32_t type, uint32_t level)
125 {
126         if (type >= rte_logs.dynamic_types_len)
127                 return -1;
128         if (level > RTE_LOG_DEBUG)
129                 return -1;
130
131         rte_logs.dynamic_types[type].loglevel = level;
132
133         return 0;
134 }
135
136 /* set level */
137 int
138 rte_log_set_level_regexp(const char *pattern, uint32_t level)
139 {
140         regex_t r;
141         size_t i;
142
143         if (level > RTE_LOG_DEBUG)
144                 return -1;
145
146         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
147                 if (rte_logs.dynamic_types[i].name == NULL)
148                         continue;
149                 if (regexec(&r, pattern, 0, NULL, 0) == 0)
150                         rte_logs.dynamic_types[i].loglevel = level;
151         }
152
153         return 0;
154 }
155
156 /* get the current loglevel for the message beeing processed */
157 int rte_log_cur_msg_loglevel(void)
158 {
159         return RTE_PER_LCORE(log_cur_msg).loglevel;
160 }
161
162 /* get the current logtype for the message beeing processed */
163 int rte_log_cur_msg_logtype(void)
164 {
165         return RTE_PER_LCORE(log_cur_msg).logtype;
166 }
167
168 static int
169 rte_log_lookup(const char *name)
170 {
171         size_t i;
172
173         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
174                 if (rte_logs.dynamic_types[i].name == NULL)
175                         continue;
176                 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
177                         return i;
178         }
179
180         return -1;
181 }
182
183 /* register an extended log type, assuming table is large enough, and id
184  * is not yet registered.
185  */
186 static int
187 __rte_log_register(const char *name, int id)
188 {
189         char *dup_name = strdup(name);
190
191         if (dup_name == NULL)
192                 return -ENOMEM;
193
194         rte_logs.dynamic_types[id].name = dup_name;
195         rte_logs.dynamic_types[id].loglevel = RTE_LOG_DEBUG;
196
197         return id;
198 }
199
200 /* register an extended log type */
201 int
202 rte_log_register(const char *name)
203 {
204         struct rte_log_dynamic_type *new_dynamic_types;
205         int id, ret;
206
207         id = rte_log_lookup(name);
208         if (id >= 0)
209                 return id;
210
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)
215                 return -ENOMEM;
216         rte_logs.dynamic_types = new_dynamic_types;
217
218         ret = __rte_log_register(name, rte_logs.dynamic_types_len);
219         if (ret < 0)
220                 return ret;
221
222         rte_logs.dynamic_types_len++;
223
224         return ret;
225 }
226
227 RTE_INIT(rte_log_init);
228 static void
229 rte_log_init(void)
230 {
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)
234                 return;
235
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);
263
264         rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
265 }
266
267 static const char *
268 loglevel_to_string(uint32_t level)
269 {
270         switch (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";
280         }
281 }
282
283 /* dump global level and registered log types */
284 void
285 rte_log_dump(FILE *f)
286 {
287         size_t i;
288
289         fprintf(f, "global log level is %s\n",
290                 loglevel_to_string(rte_get_log_level()));
291
292         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
293                 if (rte_logs.dynamic_types[i].name == NULL)
294                         continue;
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));
298         }
299 }
300
301 /*
302  * Generates a log message The message will be sent in the stream
303  * defined by the previous call to rte_openlog_stream().
304  */
305 int
306 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
307 {
308         int ret;
309         FILE *f = rte_logs.file;
310         if (f == NULL) {
311                 f = default_log_stream;
312                 if (f == NULL) {
313                         /*
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
318                          * reopens it.
319                          */
320                         f = stderr;
321                 }
322         }
323
324         if (level > rte_logs.level)
325                 return 0;
326         if (logtype >= rte_logs.dynamic_types_len)
327                 return -1;
328         if (level > rte_logs.dynamic_types[logtype].loglevel)
329                 return 0;
330
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;
334
335         ret = vfprintf(f, format, ap);
336         fflush(f);
337         return ret;
338 }
339
340 /*
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().
344  */
345 int
346 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
347 {
348         va_list ap;
349         int ret;
350
351         va_start(ap, format);
352         ret = rte_vlog(level, logtype, format, ap);
353         va_end(ap);
354         return ret;
355 }
356
357 /*
358  * Called by environment-specific initialization functions.
359  */
360 void
361 eal_log_set_default(FILE *default_log)
362 {
363         default_log_stream = default_log;
364
365 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
366         RTE_LOG(NOTICE, EAL,
367                 "Debug dataplane logs available - lower performance\n");
368 #endif
369 }