New upstream version 17.11.1
[deb_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         regfree(&r);
143
144         return 0;
145 }
146
147 /* get the current loglevel for the message being processed */
148 int rte_log_cur_msg_loglevel(void)
149 {
150         return RTE_PER_LCORE(log_cur_msg).loglevel;
151 }
152
153 /* get the current logtype for the message being processed */
154 int rte_log_cur_msg_logtype(void)
155 {
156         return RTE_PER_LCORE(log_cur_msg).logtype;
157 }
158
159 static int
160 rte_log_lookup(const char *name)
161 {
162         size_t i;
163
164         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
165                 if (rte_logs.dynamic_types[i].name == NULL)
166                         continue;
167                 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
168                         return i;
169         }
170
171         return -1;
172 }
173
174 /* register an extended log type, assuming table is large enough, and id
175  * is not yet registered.
176  */
177 static int
178 __rte_log_register(const char *name, int id)
179 {
180         char *dup_name = strdup(name);
181
182         if (dup_name == NULL)
183                 return -ENOMEM;
184
185         rte_logs.dynamic_types[id].name = dup_name;
186         rte_logs.dynamic_types[id].loglevel = RTE_LOG_DEBUG;
187
188         return id;
189 }
190
191 /* register an extended log type */
192 int
193 rte_log_register(const char *name)
194 {
195         struct rte_log_dynamic_type *new_dynamic_types;
196         int id, ret;
197
198         id = rte_log_lookup(name);
199         if (id >= 0)
200                 return id;
201
202         new_dynamic_types = realloc(rte_logs.dynamic_types,
203                 sizeof(struct rte_log_dynamic_type) *
204                 (rte_logs.dynamic_types_len + 1));
205         if (new_dynamic_types == NULL)
206                 return -ENOMEM;
207         rte_logs.dynamic_types = new_dynamic_types;
208
209         ret = __rte_log_register(name, rte_logs.dynamic_types_len);
210         if (ret < 0)
211                 return ret;
212
213         rte_logs.dynamic_types_len++;
214
215         return ret;
216 }
217
218 struct logtype {
219         uint32_t log_id;
220         const char *logtype;
221 };
222
223 static const struct logtype logtype_strings[] = {
224         {RTE_LOGTYPE_EAL,        "eal"},
225         {RTE_LOGTYPE_MALLOC,     "malloc"},
226         {RTE_LOGTYPE_RING,       "ring"},
227         {RTE_LOGTYPE_MEMPOOL,    "mempool"},
228         {RTE_LOGTYPE_TIMER,      "timer"},
229         {RTE_LOGTYPE_PMD,        "pmd"},
230         {RTE_LOGTYPE_HASH,       "hash"},
231         {RTE_LOGTYPE_LPM,        "lpm"},
232         {RTE_LOGTYPE_KNI,        "kni"},
233         {RTE_LOGTYPE_ACL,        "acl"},
234         {RTE_LOGTYPE_POWER,      "power"},
235         {RTE_LOGTYPE_METER,      "meter"},
236         {RTE_LOGTYPE_SCHED,      "sched"},
237         {RTE_LOGTYPE_PORT,       "port"},
238         {RTE_LOGTYPE_TABLE,      "table"},
239         {RTE_LOGTYPE_PIPELINE,   "pipeline"},
240         {RTE_LOGTYPE_MBUF,       "mbuf"},
241         {RTE_LOGTYPE_CRYPTODEV,  "cryptodev"},
242         {RTE_LOGTYPE_EFD,        "efd"},
243         {RTE_LOGTYPE_EVENTDEV,   "eventdev"},
244         {RTE_LOGTYPE_USER1,      "user1"},
245         {RTE_LOGTYPE_USER2,      "user2"},
246         {RTE_LOGTYPE_USER3,      "user3"},
247         {RTE_LOGTYPE_USER4,      "user4"},
248         {RTE_LOGTYPE_USER5,      "user5"},
249         {RTE_LOGTYPE_USER6,      "user6"},
250         {RTE_LOGTYPE_USER7,      "user7"},
251         {RTE_LOGTYPE_USER8,      "user8"}
252 };
253
254 /* Logging should be first initializer (before drivers and bus) */
255 RTE_INIT_PRIO(rte_log_init, 101);
256 static void
257 rte_log_init(void)
258 {
259         uint32_t i;
260
261 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
262         rte_log_set_global_level(RTE_LOG_INFO);
263 #else
264         rte_log_set_global_level(RTE_LOG_LEVEL);
265 #endif
266
267         rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
268                 sizeof(struct rte_log_dynamic_type));
269         if (rte_logs.dynamic_types == NULL)
270                 return;
271
272         /* register legacy log types */
273         for (i = 0; i < RTE_DIM(logtype_strings); i++)
274                 __rte_log_register(logtype_strings[i].logtype,
275                                 logtype_strings[i].log_id);
276
277         rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
278 }
279
280 static const char *
281 loglevel_to_string(uint32_t level)
282 {
283         switch (level) {
284         case 0: return "disabled";
285         case RTE_LOG_EMERG: return "emerg";
286         case RTE_LOG_ALERT: return "alert";
287         case RTE_LOG_CRIT: return "critical";
288         case RTE_LOG_ERR: return "error";
289         case RTE_LOG_WARNING: return "warning";
290         case RTE_LOG_NOTICE: return "notice";
291         case RTE_LOG_INFO: return "info";
292         case RTE_LOG_DEBUG: return "debug";
293         default: return "unknown";
294         }
295 }
296
297 /* dump global level and registered log types */
298 void
299 rte_log_dump(FILE *f)
300 {
301         size_t i;
302
303         fprintf(f, "global log level is %s\n",
304                 loglevel_to_string(rte_log_get_global_level()));
305
306         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
307                 if (rte_logs.dynamic_types[i].name == NULL)
308                         continue;
309                 fprintf(f, "id %zu: %s, level is %s\n",
310                         i, rte_logs.dynamic_types[i].name,
311                         loglevel_to_string(rte_logs.dynamic_types[i].loglevel));
312         }
313 }
314
315 /*
316  * Generates a log message The message will be sent in the stream
317  * defined by the previous call to rte_openlog_stream().
318  */
319 int
320 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
321 {
322         int ret;
323         FILE *f = rte_logs.file;
324         if (f == NULL) {
325                 f = default_log_stream;
326                 if (f == NULL) {
327                         /*
328                          * Grab the current value of stderr here, rather than
329                          * just initializing default_log_stream to stderr. This
330                          * ensures that we will always use the current value
331                          * of stderr, even if the application closes and
332                          * reopens it.
333                          */
334                         f = stderr;
335                 }
336         }
337
338         if (level > rte_logs.level)
339                 return 0;
340         if (logtype >= rte_logs.dynamic_types_len)
341                 return -1;
342         if (level > rte_logs.dynamic_types[logtype].loglevel)
343                 return 0;
344
345         /* save loglevel and logtype in a global per-lcore variable */
346         RTE_PER_LCORE(log_cur_msg).loglevel = level;
347         RTE_PER_LCORE(log_cur_msg).logtype = logtype;
348
349         ret = vfprintf(f, format, ap);
350         fflush(f);
351         return ret;
352 }
353
354 /*
355  * Generates a log message The message will be sent in the stream
356  * defined by the previous call to rte_openlog_stream().
357  * No need to check level here, done by rte_vlog().
358  */
359 int
360 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
361 {
362         va_list ap;
363         int ret;
364
365         va_start(ap, format);
366         ret = rte_vlog(level, logtype, format, ap);
367         va_end(ap);
368         return ret;
369 }
370
371 /*
372  * Called by environment-specific initialization functions.
373  */
374 void
375 eal_log_set_default(FILE *default_log)
376 {
377         default_log_stream = default_log;
378
379 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
380         RTE_LOG(NOTICE, EAL,
381                 "Debug dataplane logs available - lower performance\n");
382 #endif
383 }