be404136db1129a6d91a34aace4a317a0505ca24
[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_log_set_global_level(uint32_t level)
88 {
89         rte_logs.level = (uint32_t)level;
90 }
91
92 /* Get global log level */
93 uint32_t
94 rte_log_get_global_level(void)
95 {
96         return rte_logs.level;
97 }
98
99 int
100 rte_log_get_level(uint32_t type)
101 {
102         if (type >= rte_logs.dynamic_types_len)
103                 return -1;
104
105         return rte_logs.dynamic_types[type].loglevel;
106 }
107
108 int
109 rte_log_set_level(uint32_t type, uint32_t level)
110 {
111         if (type >= rte_logs.dynamic_types_len)
112                 return -1;
113         if (level > RTE_LOG_DEBUG)
114                 return -1;
115
116         rte_logs.dynamic_types[type].loglevel = level;
117
118         return 0;
119 }
120
121 /* set level */
122 int
123 rte_log_set_level_regexp(const char *pattern, uint32_t level)
124 {
125         regex_t r;
126         size_t i;
127
128         if (level > RTE_LOG_DEBUG)
129                 return -1;
130
131         if (regcomp(&r, pattern, 0) != 0)
132                 return -1;
133
134         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
135                 if (rte_logs.dynamic_types[i].name == NULL)
136                         continue;
137                 if (regexec(&r, rte_logs.dynamic_types[i].name, 0,
138                                 NULL, 0) == 0)
139                         rte_logs.dynamic_types[i].loglevel = level;
140         }
141
142         return 0;
143 }
144
145 /* get the current loglevel for the message being processed */
146 int rte_log_cur_msg_loglevel(void)
147 {
148         return RTE_PER_LCORE(log_cur_msg).loglevel;
149 }
150
151 /* get the current logtype for the message being processed */
152 int rte_log_cur_msg_logtype(void)
153 {
154         return RTE_PER_LCORE(log_cur_msg).logtype;
155 }
156
157 static int
158 rte_log_lookup(const char *name)
159 {
160         size_t i;
161
162         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
163                 if (rte_logs.dynamic_types[i].name == NULL)
164                         continue;
165                 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
166                         return i;
167         }
168
169         return -1;
170 }
171
172 /* register an extended log type, assuming table is large enough, and id
173  * is not yet registered.
174  */
175 static int
176 __rte_log_register(const char *name, int id)
177 {
178         char *dup_name = strdup(name);
179
180         if (dup_name == NULL)
181                 return -ENOMEM;
182
183         rte_logs.dynamic_types[id].name = dup_name;
184         rte_logs.dynamic_types[id].loglevel = RTE_LOG_DEBUG;
185
186         return id;
187 }
188
189 /* register an extended log type */
190 int
191 rte_log_register(const char *name)
192 {
193         struct rte_log_dynamic_type *new_dynamic_types;
194         int id, ret;
195
196         id = rte_log_lookup(name);
197         if (id >= 0)
198                 return id;
199
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)
204                 return -ENOMEM;
205         rte_logs.dynamic_types = new_dynamic_types;
206
207         ret = __rte_log_register(name, rte_logs.dynamic_types_len);
208         if (ret < 0)
209                 return ret;
210
211         rte_logs.dynamic_types_len++;
212
213         return ret;
214 }
215
216 struct logtype {
217         uint32_t log_id;
218         const char *logtype;
219 };
220
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"}
250 };
251
252 /* Logging should be first initialzer (before drivers and bus) */
253 RTE_INIT_PRIO(rte_log_init, 101);
254 static void
255 rte_log_init(void)
256 {
257         uint32_t i;
258
259 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
260         rte_log_set_global_level(RTE_LOG_INFO);
261 #else
262         rte_log_set_global_level(RTE_LOG_LEVEL);
263 #endif
264
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)
268                 return;
269
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);
274
275         rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
276 }
277
278 static const char *
279 loglevel_to_string(uint32_t level)
280 {
281         switch (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";
292         }
293 }
294
295 /* dump global level and registered log types */
296 void
297 rte_log_dump(FILE *f)
298 {
299         size_t i;
300
301         fprintf(f, "global log level is %s\n",
302                 loglevel_to_string(rte_log_get_global_level()));
303
304         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
305                 if (rte_logs.dynamic_types[i].name == NULL)
306                         continue;
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));
310         }
311 }
312
313 /*
314  * Generates a log message The message will be sent in the stream
315  * defined by the previous call to rte_openlog_stream().
316  */
317 int
318 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
319 {
320         int ret;
321         FILE *f = rte_logs.file;
322         if (f == NULL) {
323                 f = default_log_stream;
324                 if (f == NULL) {
325                         /*
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
330                          * reopens it.
331                          */
332                         f = stderr;
333                 }
334         }
335
336         if (level > rte_logs.level)
337                 return 0;
338         if (logtype >= rte_logs.dynamic_types_len)
339                 return -1;
340         if (level > rte_logs.dynamic_types[logtype].loglevel)
341                 return 0;
342
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;
346
347         ret = vfprintf(f, format, ap);
348         fflush(f);
349         return ret;
350 }
351
352 /*
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().
356  */
357 int
358 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
359 {
360         va_list ap;
361         int ret;
362
363         va_start(ap, format);
364         ret = rte_vlog(level, logtype, format, ap);
365         va_end(ap);
366         return ret;
367 }
368
369 /*
370  * Called by environment-specific initialization functions.
371  */
372 void
373 eal_log_set_default(FILE *default_log)
374 {
375         default_log_stream = default_log;
376
377 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
378         RTE_LOG(NOTICE, EAL,
379                 "Debug dataplane logs available - lower performance\n");
380 #endif
381 }