Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2017 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : :
8 : : #include "spdk/log.h"
9 : :
10 : : static TAILQ_HEAD(spdk_log_flag_head,
11 : : spdk_log_flag) g_log_flags = TAILQ_HEAD_INITIALIZER(g_log_flags);
12 : :
13 : : static struct spdk_log_flag *
14 : 134298523 : get_log_flag(const char *name)
15 : : {
16 : : struct spdk_log_flag *flag;
17 : :
18 [ + + - + : 4154004993 : TAILQ_FOREACH(flag, &g_log_flags, tailq) {
- + - + ]
19 [ + + + + : 4153835988 : if (strcasecmp(name, flag->name) == 0) {
+ + + - +
+ ]
20 : 134129518 : return flag;
21 : : }
22 : 63869931 : }
23 : :
24 : 169005 : return NULL;
25 : 2607933 : }
26 : :
27 : : void
28 : 169005 : spdk_log_register_flag(const char *name, struct spdk_log_flag *flag)
29 : : {
30 : : struct spdk_log_flag *iter;
31 : :
32 [ + - - + ]: 169005 : if (name == NULL || flag == NULL) {
33 : 0 : SPDK_ERRLOG("missing spdk_log_flag parameters\n");
34 [ # # ]: 0 : assert(false);
35 : : return;
36 : : }
37 : :
38 [ + + ]: 169005 : if (get_log_flag(name)) {
39 : 0 : SPDK_ERRLOG("duplicate spdk_log_flag '%s'\n", name);
40 [ # # ]: 0 : assert(false);
41 : : return;
42 : : }
43 : :
44 [ + + + - : 2962042 : TAILQ_FOREACH(iter, &g_log_flags, tailq) {
+ - + - ]
45 [ + + + + : 2944221 : if (strcasecmp(iter->name, flag->name) > 0) {
+ + + - +
- + - +
+ ]
46 [ + - + - : 151184 : TAILQ_INSERT_BEFORE(iter, flag, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
47 : 151184 : return;
48 : : }
49 : 160012 : }
50 : :
51 [ + - + - : 17821 : TAILQ_INSERT_TAIL(&g_log_flags, flag, tailq);
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
52 : 7851 : }
53 : :
54 : : bool
55 : 134129021 : spdk_log_get_flag(const char *name)
56 : : {
57 : 134129021 : struct spdk_log_flag *flag = get_log_flag(name);
58 : :
59 [ + + + + : 134129021 : if (flag && flag->enabled) {
+ + + - +
- ]
60 : 828 : return true;
61 : : }
62 : :
63 : 134128193 : return false;
64 : 2599469 : }
65 : :
66 : : static int
67 : 144 : log_set_flag(const char *name, bool value)
68 : : {
69 : : struct spdk_log_flag *flag;
70 : 144 : int rc = -EINVAL;
71 : :
72 [ - + + + : 144 : if (strcasecmp(name, "all") == 0) {
# # ]
73 [ + + # # : 118 : TAILQ_FOREACH(flag, &g_log_flags, tailq) {
# # # # ]
74 [ # # # # : 110 : flag->enabled = value;
# # ]
75 : 0 : }
76 : 8 : return 0;
77 : : }
78 : :
79 [ + + # # : 8645 : TAILQ_FOREACH(flag, &g_log_flags, tailq) {
# # # # ]
80 [ + + # # : 8509 : if (fnmatch(name, flag->name, FNM_CASEFOLD) == 0) {
# # # # #
# ]
81 [ # # # # : 136 : flag->enabled = value;
# # ]
82 : 136 : rc = 0;
83 : 3 : }
84 : 3 : }
85 : :
86 : 136 : return rc;
87 : 3 : }
88 : :
89 : : int
90 : 138 : spdk_log_set_flag(const char *name)
91 : : {
92 : 138 : return log_set_flag(name, true);
93 : : }
94 : :
95 : : int
96 : 6 : spdk_log_clear_flag(const char *name)
97 : : {
98 : 6 : return log_set_flag(name, false);
99 : : }
100 : :
101 : : struct spdk_log_flag *
102 : 6 : spdk_log_get_first_flag(void)
103 : : {
104 : 6 : return TAILQ_FIRST(&g_log_flags);
105 : : }
106 : :
107 : : struct spdk_log_flag *
108 : 420 : spdk_log_get_next_flag(struct spdk_log_flag *flag)
109 : : {
110 [ # # # # : 420 : return TAILQ_NEXT(flag, tailq);
# # ]
111 : : }
112 : :
113 : : void
114 : 23 : spdk_log_usage(FILE *f, const char *log_arg)
115 : : {
116 : : #define LINE_PREFIX " "
117 : : #define ENTRY_SEPARATOR ", "
118 : : #define MAX_LINE_LENGTH 100
119 : 23 : uint64_t prefix_len = strlen(LINE_PREFIX);
120 : 23 : uint64_t separator_len = strlen(ENTRY_SEPARATOR);
121 : 23 : const char *first_entry = "--logflag <flag> enable log flag (all, ";
122 : : uint64_t curr_line_len;
123 : : uint64_t curr_entry_len;
124 : : struct spdk_log_flag *flag;
125 : 23 : char first_line[MAX_LINE_LENGTH] = {};
126 : :
127 : 23 : snprintf(first_line, sizeof(first_line), " %s, %s", log_arg, first_entry);
128 [ - + ]: 23 : fprintf(f, "%s", first_line);
129 [ # # ]: 23 : curr_line_len = strlen(first_line);
130 : :
131 [ + + # # : 665 : TAILQ_FOREACH(flag, &g_log_flags, tailq) {
# # # # ]
132 [ - + # # : 665 : curr_entry_len = strlen(flag->name);
# # ]
133 [ + + ]: 665 : if ((curr_line_len + curr_entry_len + separator_len) > MAX_LINE_LENGTH) {
134 [ - + ]: 92 : fprintf(f, "\n%s", LINE_PREFIX);
135 : 92 : curr_line_len = prefix_len;
136 : 0 : }
137 : :
138 [ - + - + ]: 665 : fprintf(f, "%s", flag->name);
139 : 665 : curr_line_len += curr_entry_len;
140 : :
141 [ + + # # : 665 : if (TAILQ_LAST(&g_log_flags, spdk_log_flag_head) == flag) {
# # # # #
# ]
142 : 23 : break;
143 : : }
144 : :
145 [ - + ]: 642 : fprintf(f, "%s", ENTRY_SEPARATOR);
146 : 642 : curr_line_len += separator_len;
147 : 12 : }
148 : :
149 [ - + ]: 23 : fprintf(f, ")\n");
150 : 23 : }
|