New upstream version 18.08
[deb_dpdk.git] / lib / librte_eal / common / eal_common_log.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <stdarg.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <regex.h>
12 #include <fnmatch.h>
13
14 #include <rte_eal.h>
15 #include <rte_log.h>
16 #include <rte_per_lcore.h>
17
18 #include "eal_private.h"
19
20 /* global log structure */
21 struct rte_logs rte_logs = {
22         .type = ~0,
23         .level = RTE_LOG_DEBUG,
24         .file = NULL,
25 };
26
27 struct rte_eal_opt_loglevel {
28         /** Next list entry */
29         TAILQ_ENTRY(rte_eal_opt_loglevel) next;
30         /** Compiled regular expression obtained from the option */
31         regex_t re_match;
32         /** Glob match string option */
33         char *pattern;
34         /** Log level value obtained from the option */
35         uint32_t level;
36 };
37
38 TAILQ_HEAD(rte_eal_opt_loglevel_list, rte_eal_opt_loglevel);
39
40 /** List of valid EAL log level options */
41 static struct rte_eal_opt_loglevel_list opt_loglevel_list =
42         TAILQ_HEAD_INITIALIZER(opt_loglevel_list);
43
44 /* Stream to use for logging if rte_logs.file is NULL */
45 static FILE *default_log_stream;
46
47 /**
48  * This global structure stores some informations about the message
49  * that is currently being processed by one lcore
50  */
51 struct log_cur_msg {
52         uint32_t loglevel; /**< log level - see rte_log.h */
53         uint32_t logtype;  /**< log type  - see rte_log.h */
54 };
55
56 struct rte_log_dynamic_type {
57         const char *name;
58         uint32_t loglevel;
59 };
60
61  /* per core log */
62 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
63
64 /* default logs */
65
66 /* Change the stream that will be used by logging system */
67 int
68 rte_openlog_stream(FILE *f)
69 {
70         rte_logs.file = f;
71         return 0;
72 }
73
74 /* Set global log level */
75 void
76 rte_log_set_global_level(uint32_t level)
77 {
78         rte_logs.level = (uint32_t)level;
79 }
80
81 /* Get global log level */
82 uint32_t
83 rte_log_get_global_level(void)
84 {
85         return rte_logs.level;
86 }
87
88 int
89 rte_log_get_level(uint32_t type)
90 {
91         if (type >= rte_logs.dynamic_types_len)
92                 return -1;
93
94         return rte_logs.dynamic_types[type].loglevel;
95 }
96
97 int
98 rte_log_set_level(uint32_t type, uint32_t level)
99 {
100         if (type >= rte_logs.dynamic_types_len)
101                 return -1;
102         if (level > RTE_LOG_DEBUG)
103                 return -1;
104
105         rte_logs.dynamic_types[type].loglevel = level;
106
107         return 0;
108 }
109
110 /* set log level by regular expression */
111 int
112 rte_log_set_level_regexp(const char *regex, uint32_t level)
113 {
114         regex_t r;
115         size_t i;
116
117         if (level > RTE_LOG_DEBUG)
118                 return -1;
119
120         if (regcomp(&r, regex, 0) != 0)
121                 return -1;
122
123         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
124                 if (rte_logs.dynamic_types[i].name == NULL)
125                         continue;
126                 if (regexec(&r, rte_logs.dynamic_types[i].name, 0,
127                                 NULL, 0) == 0)
128                         rte_logs.dynamic_types[i].loglevel = level;
129         }
130
131         regfree(&r);
132
133         return 0;
134 }
135
136 /*
137  * Save the type string and the loglevel for later dynamic
138  * logtypes which may register later.
139  */
140 static int rte_log_save_level(int priority,
141                               const char *regex, const char *pattern)
142 {
143         struct rte_eal_opt_loglevel *opt_ll = NULL;
144
145         opt_ll = malloc(sizeof(*opt_ll));
146         if (opt_ll == NULL)
147                 goto fail;
148
149         opt_ll->level = priority;
150
151         if (regex) {
152                 opt_ll->pattern = NULL;
153                 if (regcomp(&opt_ll->re_match, regex, 0) != 0)
154                         goto fail;
155         } else if (pattern) {
156                 opt_ll->pattern = strdup(pattern);
157                 if (opt_ll->pattern == NULL)
158                         goto fail;
159         } else
160                 goto fail;
161
162         TAILQ_INSERT_HEAD(&opt_loglevel_list, opt_ll, next);
163         return 0;
164 fail:
165         free(opt_ll);
166         return -1;
167 }
168
169 int rte_log_save_regexp(const char *regex, int tmp)
170 {
171         return rte_log_save_level(tmp, regex, NULL);
172 }
173
174 /* set log level based on glob (file match) pattern */
175 int
176 rte_log_set_level_pattern(const char *pattern, uint32_t level)
177 {
178         size_t i;
179
180         if (level > RTE_LOG_DEBUG)
181                 return -1;
182
183         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
184                 if (rte_logs.dynamic_types[i].name == NULL)
185                         continue;
186
187                 if (fnmatch(pattern, rte_logs.dynamic_types[i].name, 0) == 0)
188                         rte_logs.dynamic_types[i].loglevel = level;
189         }
190
191         return 0;
192 }
193
194 int rte_log_save_pattern(const char *pattern, int priority)
195 {
196         return rte_log_save_level(priority, NULL, pattern);
197 }
198
199 /* get the current loglevel for the message being processed */
200 int rte_log_cur_msg_loglevel(void)
201 {
202         return RTE_PER_LCORE(log_cur_msg).loglevel;
203 }
204
205 /* get the current logtype for the message being processed */
206 int rte_log_cur_msg_logtype(void)
207 {
208         return RTE_PER_LCORE(log_cur_msg).logtype;
209 }
210
211 static int
212 rte_log_lookup(const char *name)
213 {
214         size_t i;
215
216         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
217                 if (rte_logs.dynamic_types[i].name == NULL)
218                         continue;
219                 if (strcmp(name, rte_logs.dynamic_types[i].name) == 0)
220                         return i;
221         }
222
223         return -1;
224 }
225
226 /* register an extended log type, assuming table is large enough, and id
227  * is not yet registered.
228  */
229 static int
230 __rte_log_register(const char *name, int id)
231 {
232         char *dup_name = strdup(name);
233
234         if (dup_name == NULL)
235                 return -ENOMEM;
236
237         rte_logs.dynamic_types[id].name = dup_name;
238         rte_logs.dynamic_types[id].loglevel = RTE_LOG_INFO;
239
240         return id;
241 }
242
243 /* register an extended log type */
244 int
245 rte_log_register(const char *name)
246 {
247         struct rte_log_dynamic_type *new_dynamic_types;
248         int id, ret;
249
250         id = rte_log_lookup(name);
251         if (id >= 0)
252                 return id;
253
254         new_dynamic_types = realloc(rte_logs.dynamic_types,
255                 sizeof(struct rte_log_dynamic_type) *
256                 (rte_logs.dynamic_types_len + 1));
257         if (new_dynamic_types == NULL)
258                 return -ENOMEM;
259         rte_logs.dynamic_types = new_dynamic_types;
260
261         ret = __rte_log_register(name, rte_logs.dynamic_types_len);
262         if (ret < 0)
263                 return ret;
264
265         rte_logs.dynamic_types_len++;
266
267         return ret;
268 }
269
270 /* Register an extended log type and try to pick its level from EAL options */
271 int __rte_experimental
272 rte_log_register_type_and_pick_level(const char *name, uint32_t level_def)
273 {
274         struct rte_eal_opt_loglevel *opt_ll;
275         uint32_t level = level_def;
276         int type;
277
278         type = rte_log_register(name);
279         if (type < 0)
280                 return type;
281
282         TAILQ_FOREACH(opt_ll, &opt_loglevel_list, next) {
283                 if (opt_ll->level > RTE_LOG_DEBUG)
284                         continue;
285
286                 if (opt_ll->pattern) {
287                         if (fnmatch(opt_ll->pattern, name, 0))
288                                 level = opt_ll->level;
289                 } else {
290                         if (regexec(&opt_ll->re_match, name, 0, NULL, 0) == 0)
291                                 level = opt_ll->level;
292                 }
293         }
294
295         rte_logs.dynamic_types[type].loglevel = level;
296
297         return type;
298 }
299
300 struct logtype {
301         uint32_t log_id;
302         const char *logtype;
303 };
304
305 static const struct logtype logtype_strings[] = {
306         {RTE_LOGTYPE_EAL,        "lib.eal"},
307         {RTE_LOGTYPE_MALLOC,     "lib.malloc"},
308         {RTE_LOGTYPE_RING,       "lib.ring"},
309         {RTE_LOGTYPE_MEMPOOL,    "lib.mempool"},
310         {RTE_LOGTYPE_TIMER,      "lib.timer"},
311         {RTE_LOGTYPE_PMD,        "pmd"},
312         {RTE_LOGTYPE_HASH,       "lib.hash"},
313         {RTE_LOGTYPE_LPM,        "lib.lpm"},
314         {RTE_LOGTYPE_KNI,        "lib.kni"},
315         {RTE_LOGTYPE_ACL,        "lib.acl"},
316         {RTE_LOGTYPE_POWER,      "lib.power"},
317         {RTE_LOGTYPE_METER,      "lib.meter"},
318         {RTE_LOGTYPE_SCHED,      "lib.sched"},
319         {RTE_LOGTYPE_PORT,       "lib.port"},
320         {RTE_LOGTYPE_TABLE,      "lib.table"},
321         {RTE_LOGTYPE_PIPELINE,   "lib.pipeline"},
322         {RTE_LOGTYPE_MBUF,       "lib.mbuf"},
323         {RTE_LOGTYPE_CRYPTODEV,  "lib.cryptodev"},
324         {RTE_LOGTYPE_EFD,        "lib.efd"},
325         {RTE_LOGTYPE_EVENTDEV,   "lib.eventdev"},
326         {RTE_LOGTYPE_GSO,        "lib.gso"},
327         {RTE_LOGTYPE_USER1,      "user1"},
328         {RTE_LOGTYPE_USER2,      "user2"},
329         {RTE_LOGTYPE_USER3,      "user3"},
330         {RTE_LOGTYPE_USER4,      "user4"},
331         {RTE_LOGTYPE_USER5,      "user5"},
332         {RTE_LOGTYPE_USER6,      "user6"},
333         {RTE_LOGTYPE_USER7,      "user7"},
334         {RTE_LOGTYPE_USER8,      "user8"}
335 };
336
337 /* Logging should be first initializer (before drivers and bus) */
338 RTE_INIT_PRIO(rte_log_init, LOG)
339 {
340         uint32_t i;
341
342         rte_log_set_global_level(RTE_LOG_DEBUG);
343
344         rte_logs.dynamic_types = calloc(RTE_LOGTYPE_FIRST_EXT_ID,
345                 sizeof(struct rte_log_dynamic_type));
346         if (rte_logs.dynamic_types == NULL)
347                 return;
348
349         /* register legacy log types */
350         for (i = 0; i < RTE_DIM(logtype_strings); i++)
351                 __rte_log_register(logtype_strings[i].logtype,
352                                 logtype_strings[i].log_id);
353
354         rte_logs.dynamic_types_len = RTE_LOGTYPE_FIRST_EXT_ID;
355 }
356
357 static const char *
358 loglevel_to_string(uint32_t level)
359 {
360         switch (level) {
361         case 0: return "disabled";
362         case RTE_LOG_EMERG: return "emerg";
363         case RTE_LOG_ALERT: return "alert";
364         case RTE_LOG_CRIT: return "critical";
365         case RTE_LOG_ERR: return "error";
366         case RTE_LOG_WARNING: return "warning";
367         case RTE_LOG_NOTICE: return "notice";
368         case RTE_LOG_INFO: return "info";
369         case RTE_LOG_DEBUG: return "debug";
370         default: return "unknown";
371         }
372 }
373
374 /* dump global level and registered log types */
375 void
376 rte_log_dump(FILE *f)
377 {
378         size_t i;
379
380         fprintf(f, "global log level is %s\n",
381                 loglevel_to_string(rte_log_get_global_level()));
382
383         for (i = 0; i < rte_logs.dynamic_types_len; i++) {
384                 if (rte_logs.dynamic_types[i].name == NULL)
385                         continue;
386                 fprintf(f, "id %zu: %s, level is %s\n",
387                         i, rte_logs.dynamic_types[i].name,
388                         loglevel_to_string(rte_logs.dynamic_types[i].loglevel));
389         }
390 }
391
392 /*
393  * Generates a log message The message will be sent in the stream
394  * defined by the previous call to rte_openlog_stream().
395  */
396 int
397 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
398 {
399         int ret;
400         FILE *f = rte_logs.file;
401         if (f == NULL) {
402                 f = default_log_stream;
403                 if (f == NULL) {
404                         /*
405                          * Grab the current value of stderr here, rather than
406                          * just initializing default_log_stream to stderr. This
407                          * ensures that we will always use the current value
408                          * of stderr, even if the application closes and
409                          * reopens it.
410                          */
411                         f = stderr;
412                 }
413         }
414
415         if (level > rte_logs.level)
416                 return 0;
417         if (logtype >= rte_logs.dynamic_types_len)
418                 return -1;
419         if (level > rte_logs.dynamic_types[logtype].loglevel)
420                 return 0;
421
422         /* save loglevel and logtype in a global per-lcore variable */
423         RTE_PER_LCORE(log_cur_msg).loglevel = level;
424         RTE_PER_LCORE(log_cur_msg).logtype = logtype;
425
426         ret = vfprintf(f, format, ap);
427         fflush(f);
428         return ret;
429 }
430
431 /*
432  * Generates a log message The message will be sent in the stream
433  * defined by the previous call to rte_openlog_stream().
434  * No need to check level here, done by rte_vlog().
435  */
436 int
437 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
438 {
439         va_list ap;
440         int ret;
441
442         va_start(ap, format);
443         ret = rte_vlog(level, logtype, format, ap);
444         va_end(ap);
445         return ret;
446 }
447
448 /*
449  * Called by environment-specific initialization functions.
450  */
451 void
452 eal_log_set_default(FILE *default_log)
453 {
454         default_log_stream = default_log;
455
456 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
457         RTE_LOG(NOTICE, EAL,
458                 "Debug dataplane logs available - lower performance\n");
459 #endif
460 }