lttng: move log level name to numerical value conversions to a common util
[lttng-tools.git] / src / bin / lttng / loglevel.c
1 /*
2 * Copyright (C) 2021 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include "loglevel.h"
9 #include <string.h>
10 #include <ctype.h>
11 #include <assert.h>
12
13 #define LOGLEVEL_NAME_VALUE_ARRAY_COUNT(name) (sizeof(name) / sizeof(struct loglevel_name_value))
14
15 struct loglevel_name_value {
16 const char *name;
17 int value;
18 };
19
20 static
21 const struct loglevel_name_value loglevel_values[] = {
22 { .name = "TRACE_EMERG", .value = LTTNG_LOGLEVEL_EMERG },
23 { .name = "EMERG", .value = LTTNG_LOGLEVEL_EMERG },
24 { .name = "TRACE_ALERT", .value = LTTNG_LOGLEVEL_ALERT },
25 { .name = "ALERT", .value = LTTNG_LOGLEVEL_ALERT },
26 { .name = "TRACE_CRIT", .value = LTTNG_LOGLEVEL_CRIT },
27 { .name = "CRIT", .value = LTTNG_LOGLEVEL_CRIT },
28 { .name = "TRACE_ERR", .value = LTTNG_LOGLEVEL_ERR },
29 { .name = "ERR", .value = LTTNG_LOGLEVEL_ERR },
30 { .name = "TRACE_WARNING", .value = LTTNG_LOGLEVEL_WARNING },
31 { .name = "WARNING", .value = LTTNG_LOGLEVEL_WARNING },
32 { .name = "TRACE_NOTICE", .value = LTTNG_LOGLEVEL_NOTICE },
33 { .name = "NOTICE", .value = LTTNG_LOGLEVEL_NOTICE },
34 { .name = "TRACE_INFO", .value = LTTNG_LOGLEVEL_INFO },
35 { .name = "INFO", .value = LTTNG_LOGLEVEL_INFO },
36 { .name = "TRACE_DEBUG_SYSTEM", .value = LTTNG_LOGLEVEL_DEBUG_SYSTEM },
37 { .name = "DEBUG_SYSTEM", .value = LTTNG_LOGLEVEL_DEBUG_SYSTEM },
38 { .name = "SYSTEM", .value = LTTNG_LOGLEVEL_DEBUG_SYSTEM },
39 { .name = "TRACE_DEBUG_PROGRAM", .value = LTTNG_LOGLEVEL_DEBUG_PROGRAM },
40 { .name = "DEBUG_PROGRAM", .value = LTTNG_LOGLEVEL_DEBUG_PROGRAM },
41 { .name = "PROGRAM", .value = LTTNG_LOGLEVEL_DEBUG_PROGRAM },
42 { .name = "TRACE_DEBUG_PROCESS", .value = LTTNG_LOGLEVEL_DEBUG_PROCESS },
43 { .name = "DEBUG_PROCESS", .value = LTTNG_LOGLEVEL_DEBUG_PROCESS },
44 { .name = "PROCESS", .value = LTTNG_LOGLEVEL_DEBUG_PROCESS },
45 { .name = "TRACE_DEBUG_MODULE", .value = LTTNG_LOGLEVEL_DEBUG_MODULE },
46 { .name = "DEBUG_MODULE", .value = LTTNG_LOGLEVEL_DEBUG_MODULE },
47 { .name = "MODULE", .value = LTTNG_LOGLEVEL_DEBUG_MODULE },
48 { .name = "TRACE_DEBUG_UNIT", .value = LTTNG_LOGLEVEL_DEBUG_UNIT },
49 { .name = "DEBUG_UNIT", .value = LTTNG_LOGLEVEL_DEBUG_UNIT },
50 { .name = "UNIT", .value = LTTNG_LOGLEVEL_DEBUG_UNIT },
51 { .name = "TRACE_DEBUG_FUNCTION", .value = LTTNG_LOGLEVEL_DEBUG_FUNCTION },
52 { .name = "DEBUG_FUNCTION", .value = LTTNG_LOGLEVEL_DEBUG_FUNCTION },
53 { .name = "FUNCTION", .value = LTTNG_LOGLEVEL_DEBUG_FUNCTION },
54 { .name = "TRACE_DEBUG_LINE", .value = LTTNG_LOGLEVEL_DEBUG_LINE },
55 { .name = "DEBUG_LINE", .value = LTTNG_LOGLEVEL_DEBUG_LINE },
56 { .name = "LINE", .value = LTTNG_LOGLEVEL_DEBUG_LINE },
57 { .name = "TRACE_DEBUG", .value = LTTNG_LOGLEVEL_DEBUG },
58 { .name = "DEBUG", .value = LTTNG_LOGLEVEL_DEBUG },
59 };
60
61 static
62 const struct loglevel_name_value loglevel_log4j_values[] = {
63 { .name = "LOG4J_OFF", .value = LTTNG_LOGLEVEL_LOG4J_OFF },
64 { .name = "OFF", .value = LTTNG_LOGLEVEL_LOG4J_OFF },
65 { .name = "LOG4J_FATAL", .value = LTTNG_LOGLEVEL_LOG4J_FATAL },
66 { .name = "FATAL", .value = LTTNG_LOGLEVEL_LOG4J_FATAL },
67 { .name = "LOG4J_ERROR", .value = LTTNG_LOGLEVEL_LOG4J_ERROR },
68 { .name = "ERROR", .value = LTTNG_LOGLEVEL_LOG4J_ERROR },
69 { .name = "LOG4J_WARN", .value = LTTNG_LOGLEVEL_LOG4J_WARN },
70 { .name = "WARN", .value = LTTNG_LOGLEVEL_LOG4J_WARN },
71 { .name = "LOG4J_INFO", .value = LTTNG_LOGLEVEL_LOG4J_INFO },
72 { .name = "INFO", .value = LTTNG_LOGLEVEL_LOG4J_INFO },
73 { .name = "LOG4J_DEBUG", .value = LTTNG_LOGLEVEL_LOG4J_DEBUG },
74 { .name = "DEBUG", .value = LTTNG_LOGLEVEL_LOG4J_DEBUG },
75 { .name = "LOG4J_TRACE", .value = LTTNG_LOGLEVEL_LOG4J_TRACE },
76 { .name = "TRACE", .value = LTTNG_LOGLEVEL_LOG4J_TRACE },
77 { .name = "LOG4J_ALL", .value = LTTNG_LOGLEVEL_LOG4J_ALL },
78 { .name = "ALL", .value = LTTNG_LOGLEVEL_LOG4J_ALL },
79 };
80
81 static
82 const struct loglevel_name_value loglevel_jul_values[] = {
83 { .name = "JUL_OFF", .value = LTTNG_LOGLEVEL_JUL_OFF },
84 { .name = "OFF", .value = LTTNG_LOGLEVEL_JUL_OFF },
85 { .name = "JUL_SEVERE", .value = LTTNG_LOGLEVEL_JUL_SEVERE },
86 { .name = "SEVERE", .value = LTTNG_LOGLEVEL_JUL_SEVERE },
87 { .name = "JUL_WARNING", .value = LTTNG_LOGLEVEL_JUL_WARNING },
88 { .name = "WARNING", .value = LTTNG_LOGLEVEL_JUL_WARNING },
89 { .name = "JUL_INFO", .value = LTTNG_LOGLEVEL_JUL_INFO },
90 { .name = "INFO", .value = LTTNG_LOGLEVEL_JUL_INFO },
91 { .name = "JUL_CONFIG", .value = LTTNG_LOGLEVEL_JUL_CONFIG },
92 { .name = "CONFIG", .value = LTTNG_LOGLEVEL_JUL_CONFIG },
93 { .name = "JUL_FINE", .value = LTTNG_LOGLEVEL_JUL_FINE },
94 { .name = "FINE", .value = LTTNG_LOGLEVEL_JUL_FINE },
95 { .name = "JUL_FINER", .value = LTTNG_LOGLEVEL_JUL_FINER },
96 { .name = "FINER", .value = LTTNG_LOGLEVEL_JUL_FINER },
97 { .name = "JUL_FINEST", .value = LTTNG_LOGLEVEL_JUL_FINEST },
98 { .name = "FINEST", .value = LTTNG_LOGLEVEL_JUL_FINEST },
99 { .name = "JUL_ALL", .value = LTTNG_LOGLEVEL_JUL_ALL },
100 { .name = "ALL", .value = LTTNG_LOGLEVEL_JUL_ALL },
101 };
102
103 static
104 const struct loglevel_name_value loglevel_python_values[] = {
105 { .name = "PYTHON_CRITICAL", .value = LTTNG_LOGLEVEL_PYTHON_CRITICAL },
106 { .name = "CRITICAL", .value = LTTNG_LOGLEVEL_PYTHON_CRITICAL },
107 { .name = "PYTHON_ERROR", .value = LTTNG_LOGLEVEL_PYTHON_ERROR },
108 { .name = "ERROR", .value = LTTNG_LOGLEVEL_PYTHON_ERROR },
109 { .name = "PYTHON_WARNING", .value = LTTNG_LOGLEVEL_PYTHON_WARNING },
110 { .name = "WARNING", .value = LTTNG_LOGLEVEL_PYTHON_WARNING },
111 { .name = "PYTHON_INFO", .value = LTTNG_LOGLEVEL_PYTHON_INFO },
112 { .name = "INFO", .value = LTTNG_LOGLEVEL_PYTHON_INFO },
113 { .name = "PYTNON_DEBUG", .value = LTTNG_LOGLEVEL_PYTHON_DEBUG },
114 { .name = "DEBUG", .value = LTTNG_LOGLEVEL_PYTHON_DEBUG },
115 { .name = "PYTHON_NOTSET", .value = LTTNG_LOGLEVEL_PYTHON_NOTSET },
116 { .name = "NOTSET", .value = LTTNG_LOGLEVEL_PYTHON_NOTSET },
117 };
118
119 static
120 bool string_equal_insensitive(const char *a, const char *b)
121 {
122 bool result;
123
124 assert(a && b);
125
126 while (*a && *b) {
127 if (toupper(*a) != toupper(*b)) {
128 result = false;
129 goto end;
130 }
131
132 a++;
133 b++;
134 }
135
136 /* If a and b don't have the same length, consider them unequal. */
137 result = *a == *b;
138
139 end:
140 return result;
141 }
142
143 static
144 int lookup_value_from_name(const struct loglevel_name_value values[],
145 size_t values_count, const char *name)
146 {
147 size_t i;
148 int ret = -1;
149
150 if (!name) {
151 goto end;
152 }
153
154 for (i = 0; i < values_count; i++) {
155 if (string_equal_insensitive(values[i].name, name)) {
156 /* Match found. */
157 ret = values[i].value;
158 goto end;
159 }
160 }
161
162 end:
163 return ret;
164 }
165
166 LTTNG_HIDDEN
167 int loglevel_name_to_value(const char *name, enum lttng_loglevel *loglevel)
168 {
169 int ret = lookup_value_from_name(loglevel_values,
170 LOGLEVEL_NAME_VALUE_ARRAY_COUNT(loglevel_values), name);
171
172 if (ret >= 0) {
173 *loglevel = (typeof(*loglevel)) ret;
174 ret = 0;
175 }
176
177 return ret;
178 }
179
180 LTTNG_HIDDEN
181 int loglevel_log4j_name_to_value(
182 const char *name, enum lttng_loglevel_log4j *loglevel)
183 {
184 int ret = lookup_value_from_name(loglevel_log4j_values,
185 LOGLEVEL_NAME_VALUE_ARRAY_COUNT(loglevel_log4j_values),
186 name);
187
188 if (ret >= 0) {
189 *loglevel = (typeof(*loglevel)) ret;
190 ret = 0;
191 }
192
193 return ret;
194 }
195
196 LTTNG_HIDDEN
197 int loglevel_jul_name_to_value(
198 const char *name, enum lttng_loglevel_jul *loglevel)
199 {
200 int ret = lookup_value_from_name(loglevel_jul_values,
201 LOGLEVEL_NAME_VALUE_ARRAY_COUNT(loglevel_jul_values),
202 name);
203
204 if (ret >= 0) {
205 *loglevel = (typeof(*loglevel)) ret;
206 ret = 0;
207 }
208
209 return ret;
210 }
211
212 LTTNG_HIDDEN
213 int loglevel_python_name_to_value(
214 const char *name, enum lttng_loglevel_python *loglevel)
215 {
216 int ret = lookup_value_from_name(loglevel_python_values,
217 LOGLEVEL_NAME_VALUE_ARRAY_COUNT(loglevel_python_values),
218 name);
219
220 if (ret >= 0) {
221 *loglevel = (typeof(*loglevel)) ret;
222 ret = 0;
223 }
224
225 return ret;
226 }
This page took 0.063525 seconds and 5 git commands to generate.