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