eal: deprecate log functions
[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 /* Set global log level */
93 /* replaced by rte_log_set_global_level */
94 __rte_deprecated void
95 rte_set_log_level(uint32_t level)
96 {
97         rte_log_set_global_level(level);
98 }
99
100 /* Get global log level */
101 uint32_t
102 rte_log_get_global_level(void)
103 {
104         return rte_logs.level;
105 }
106
107 /* Get global log level */
108 /* replaced by rte_log_get_global_level */
109 uint32_t
110 rte_get_log_level(void)
111 {
112         return rte_log_get_global_level();
113 }
114
115 /* Set global log type */
116 __rte_deprecated void
117 rte_set_log_type(uint32_t type, int enable)
118 {
119         if (type < RTE_LOGTYPE_FIRST_EXT_ID) {
120                 if (enable)
121                         rte_logs.type |= type;
122                 else
123                         rte_logs.type &= ~type;
124         }
125
126         if (enable)
127                 rte_log_set_level(type, 0);
128         else
129                 rte_log_set_level(type, RTE_LOG_DEBUG);
130 }
131
132 /* Get global log type */
133 __rte_deprecated uint32_t
134 rte_get_log_type(void)
135 {
136         return rte_logs.type;
137 }
138
139 int
140 rte_log_set_level(uint32_t type, uint32_t level)
141 {
142         if (type >= rte_logs.dynamic_types_len)
143                 return -1;
144         if (level > RTE_LOG_DEBUG)
145                 return -1;
146
147         rte_logs.dynamic_types[type].loglevel = level;
148
149         return 0;
150 }
151
152 /* set level */
153 int
154 rte_log_set_level_regexp(const char *pattern, uint32_t level)
155 {
156         regex_t r;
157         size_t i;
158
159         if (level > RTE_LOG_DEBUG)
160                 return -1;
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 (regexec(&r, pattern, 0, NULL, 0) == 0)
166                         rte_logs.dynamic_types[i].loglevel = level;
167         }
168
169         return 0;
170 }
171
172 /* get the current loglevel for the message beeing processed */
173 int rte_log_cur_msg_loglevel(void)
174 {
175         return RTE_PER_LCORE(log_cur_msg).loglevel;
176 }
177
178 /* get the current logtype for the message beeing processed */
179 int rte_log_cur_msg_logtype(void)
180 {
181         return RTE_PER_LCORE(log_cur_msg).logtype;
182 }
183
184 static int
185 rte_log_lookup(const char *name)
186 {
187         size_t i;
188
189         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
190                 if (rte_logs.dynamic_types[i].name == NULL)
191                         continue;
192                 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
193                         return i;
194         }
195
196         return -1;
197 }
198
199 /* register an extended log type, assuming table is large enough, and id
200  * is not yet registered.
201  */
202 static int
203 __rte_log_register(const char *name, int id)
204 {
205         char *dup_name = strdup(name);
206
207         if (dup_name == NULL)
208                 return -ENOMEM;
209
210         rte_logs.dynamic_types[id].name = dup_name;
211         rte_logs.dynamic_types[id].loglevel = RTE_LOG_DEBUG;
212
213         return id;
214 }
215
216 /* register an extended log type */
217 int
218 rte_log_register(const char *name)
219 {
220         struct rte_log_dynamic_type *new_dynamic_types;
221         int id, ret;
222
223         id = rte_log_lookup(name);
224         if (id >= 0)
225                 return id;
226
227         new_dynamic_types = realloc(rte_logs.dynamic_types,
228                 sizeof(struct rte_log_dynamic_type) *
229                 (rte_logs.dynamic_types_len + 1));
230         if (new_dynamic_types == NULL)
231                 return -ENOMEM;
232         rte_logs.dynamic_types = new_dynamic_types;
233
234         ret = __rte_log_register(name, rte_logs.dynamic_types_len);
235         if (ret < 0)
236                 return ret;
237
238         rte_logs.dynamic_types_len++;
239
240         return ret;
241 }
242
243 RTE_INIT(rte_log_init);
244 static void
245 rte_log_init(void)
246 {
247         rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
248                 sizeof(struct rte_log_dynamic_type));
249         if (rte_logs.dynamic_types == NULL)
250                 return;
251
252         /* register legacy log types, keep sync'd with RTE_LOGTYPE_* */
253         __rte_log_register("eal", 0);
254         __rte_log_register("malloc", 1);
255         __rte_log_register("ring", 2);
256         __rte_log_register("mempool", 3);
257         __rte_log_register("timer", 4);
258         __rte_log_register("pmd", 5);
259         __rte_log_register("hash", 6);
260         __rte_log_register("lpm", 7);
261         __rte_log_register("kni", 8);
262         __rte_log_register("acl", 9);
263         __rte_log_register("power", 10);
264         __rte_log_register("meter", 11);
265         __rte_log_register("sched", 12);
266         __rte_log_register("port", 13);
267         __rte_log_register("table", 14);
268         __rte_log_register("pipeline", 15);
269         __rte_log_register("mbuf", 16);
270         __rte_log_register("cryptodev", 17);
271         __rte_log_register("user1", 24);
272         __rte_log_register("user2", 25);
273         __rte_log_register("user3", 26);
274         __rte_log_register("user4", 27);
275         __rte_log_register("user5", 28);
276         __rte_log_register("user6", 29);
277         __rte_log_register("user7", 30);
278         __rte_log_register("user8", 31);
279
280         rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
281 }
282
283 static const char *
284 loglevel_to_string(uint32_t level)
285 {
286         switch (level) {
287         case RTE_LOG_EMERG: return "emerg";
288         case RTE_LOG_ALERT: return "alert";
289         case RTE_LOG_CRIT: return "critical";
290         case RTE_LOG_ERR: return "error";
291         case RTE_LOG_WARNING: return "warning";
292         case RTE_LOG_NOTICE: return "notice";
293         case RTE_LOG_INFO: return "info";
294         case RTE_LOG_DEBUG: return "debug";
295         default: return "unknown";
296         }
297 }
298
299 /* dump global level and registered log types */
300 void
301 rte_log_dump(FILE *f)
302 {
303         size_t i;
304
305         fprintf(f, "global log level is %s\n",
306                 loglevel_to_string(rte_log_get_global_level()));
307
308         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
309                 if (rte_logs.dynamic_types[i].name == NULL)
310                         continue;
311                 fprintf(f, "id %zu: %s, level is %s\n",
312                         i, rte_logs.dynamic_types[i].name,
313                         loglevel_to_string(rte_logs.dynamic_types[i].loglevel));
314         }
315 }
316
317 /*
318  * Generates a log message The message will be sent in the stream
319  * defined by the previous call to rte_openlog_stream().
320  */
321 int
322 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
323 {
324         int ret;
325         FILE *f = rte_logs.file;
326         if (f == NULL) {
327                 f = default_log_stream;
328                 if (f == NULL) {
329                         /*
330                          * Grab the current value of stderr here, rather than
331                          * just initializing default_log_stream to stderr. This
332                          * ensures that we will always use the current value
333                          * of stderr, even if the application closes and
334                          * reopens it.
335                          */
336                         f = stderr;
337                 }
338         }
339
340         if (level > rte_logs.level)
341                 return 0;
342         if (logtype >= rte_logs.dynamic_types_len)
343                 return -1;
344         if (level > rte_logs.dynamic_types[logtype].loglevel)
345                 return 0;
346
347         /* save loglevel and logtype in a global per-lcore variable */
348         RTE_PER_LCORE(log_cur_msg).loglevel = level;
349         RTE_PER_LCORE(log_cur_msg).logtype = logtype;
350
351         ret = vfprintf(f, format, ap);
352         fflush(f);
353         return ret;
354 }
355
356 /*
357  * Generates a log message The message will be sent in the stream
358  * defined by the previous call to rte_openlog_stream().
359  * No need to check level here, done by rte_vlog().
360  */
361 int
362 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
363 {
364         va_list ap;
365         int ret;
366
367         va_start(ap, format);
368         ret = rte_vlog(level, logtype, format, ap);
369         va_end(ap);
370         return ret;
371 }
372
373 /*
374  * Called by environment-specific initialization functions.
375  */
376 void
377 eal_log_set_default(FILE *default_log)
378 {
379         default_log_stream = default_log;
380
381 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
382         RTE_LOG(NOTICE, EAL,
383                 "Debug dataplane logs available - lower performance\n");
384 #endif
385 }