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