Rename *exclusion* to *name_pattern_exclusion*
[lttng-tools.git] / src / bin / lttng / loglevel.c
CommitLineData
7e8f2e9c
JG
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>
f41294ed 10#include <strings.h>
7e8f2e9c
JG
11#include <ctype.h>
12#include <assert.h>
13
7e8f2e9c
JG
14struct loglevel_name_value {
15 const char *name;
16 int value;
17};
18
19static
20const struct loglevel_name_value loglevel_values[] = {
7e8f2e9c 21 { .name = "EMERG", .value = LTTNG_LOGLEVEL_EMERG },
85b05318 22 { .name = "TRACE_EMERG", .value = LTTNG_LOGLEVEL_EMERG },
7e8f2e9c 23 { .name = "ALERT", .value = LTTNG_LOGLEVEL_ALERT },
85b05318 24 { .name = "TRACE_ALERT", .value = LTTNG_LOGLEVEL_ALERT },
7e8f2e9c 25 { .name = "CRIT", .value = LTTNG_LOGLEVEL_CRIT },
85b05318 26 { .name = "TRACE_CRIT", .value = LTTNG_LOGLEVEL_CRIT },
7e8f2e9c 27 { .name = "ERR", .value = LTTNG_LOGLEVEL_ERR },
85b05318 28 { .name = "TRACE_ERR", .value = LTTNG_LOGLEVEL_ERR },
7e8f2e9c 29 { .name = "WARNING", .value = LTTNG_LOGLEVEL_WARNING },
85b05318 30 { .name = "TRACE_WARNING", .value = LTTNG_LOGLEVEL_WARNING },
7e8f2e9c 31 { .name = "NOTICE", .value = LTTNG_LOGLEVEL_NOTICE },
85b05318 32 { .name = "TRACE_NOTICE", .value = LTTNG_LOGLEVEL_NOTICE },
7e8f2e9c 33 { .name = "INFO", .value = LTTNG_LOGLEVEL_INFO },
85b05318 34 { .name = "TRACE_INFO", .value = LTTNG_LOGLEVEL_INFO },
7e8f2e9c 35 { .name = "DEBUG_SYSTEM", .value = LTTNG_LOGLEVEL_DEBUG_SYSTEM },
85b05318 36 { .name = "TRACE_DEBUG_SYSTEM", .value = LTTNG_LOGLEVEL_DEBUG_SYSTEM },
7e8f2e9c 37 { .name = "SYSTEM", .value = LTTNG_LOGLEVEL_DEBUG_SYSTEM },
7e8f2e9c 38 { .name = "DEBUG_PROGRAM", .value = LTTNG_LOGLEVEL_DEBUG_PROGRAM },
85b05318 39 { .name = "TRACE_DEBUG_PROGRAM", .value = LTTNG_LOGLEVEL_DEBUG_PROGRAM },
7e8f2e9c 40 { .name = "PROGRAM", .value = LTTNG_LOGLEVEL_DEBUG_PROGRAM },
7e8f2e9c 41 { .name = "DEBUG_PROCESS", .value = LTTNG_LOGLEVEL_DEBUG_PROCESS },
85b05318 42 { .name = "TRACE_DEBUG_PROCESS", .value = LTTNG_LOGLEVEL_DEBUG_PROCESS },
7e8f2e9c 43 { .name = "PROCESS", .value = LTTNG_LOGLEVEL_DEBUG_PROCESS },
7e8f2e9c 44 { .name = "DEBUG_MODULE", .value = LTTNG_LOGLEVEL_DEBUG_MODULE },
85b05318 45 { .name = "TRACE_DEBUG_MODULE", .value = LTTNG_LOGLEVEL_DEBUG_MODULE },
7e8f2e9c 46 { .name = "MODULE", .value = LTTNG_LOGLEVEL_DEBUG_MODULE },
7e8f2e9c 47 { .name = "DEBUG_UNIT", .value = LTTNG_LOGLEVEL_DEBUG_UNIT },
85b05318 48 { .name = "TRACE_DEBUG_UNIT", .value = LTTNG_LOGLEVEL_DEBUG_UNIT },
7e8f2e9c 49 { .name = "UNIT", .value = LTTNG_LOGLEVEL_DEBUG_UNIT },
7e8f2e9c 50 { .name = "DEBUG_FUNCTION", .value = LTTNG_LOGLEVEL_DEBUG_FUNCTION },
85b05318 51 { .name = "TRACE_DEBUG_FUNCTION", .value = LTTNG_LOGLEVEL_DEBUG_FUNCTION },
7e8f2e9c 52 { .name = "FUNCTION", .value = LTTNG_LOGLEVEL_DEBUG_FUNCTION },
7e8f2e9c 53 { .name = "DEBUG_LINE", .value = LTTNG_LOGLEVEL_DEBUG_LINE },
85b05318 54 { .name = "TRACE_DEBUG_LINE", .value = LTTNG_LOGLEVEL_DEBUG_LINE },
7e8f2e9c 55 { .name = "LINE", .value = LTTNG_LOGLEVEL_DEBUG_LINE },
7e8f2e9c 56 { .name = "DEBUG", .value = LTTNG_LOGLEVEL_DEBUG },
85b05318 57 { .name = "TRACE_DEBUG", .value = LTTNG_LOGLEVEL_DEBUG },
7e8f2e9c
JG
58};
59
60static
61const struct loglevel_name_value loglevel_log4j_values[] = {
7e8f2e9c 62 { .name = "OFF", .value = LTTNG_LOGLEVEL_LOG4J_OFF },
85b05318 63 { .name = "LOG4J_OFF", .value = LTTNG_LOGLEVEL_LOG4J_OFF },
7e8f2e9c 64 { .name = "FATAL", .value = LTTNG_LOGLEVEL_LOG4J_FATAL },
85b05318 65 { .name = "LOG4J_FATAL", .value = LTTNG_LOGLEVEL_LOG4J_FATAL },
7e8f2e9c 66 { .name = "ERROR", .value = LTTNG_LOGLEVEL_LOG4J_ERROR },
85b05318 67 { .name = "LOG4J_ERROR", .value = LTTNG_LOGLEVEL_LOG4J_ERROR },
7e8f2e9c 68 { .name = "WARN", .value = LTTNG_LOGLEVEL_LOG4J_WARN },
85b05318 69 { .name = "LOG4J_WARN", .value = LTTNG_LOGLEVEL_LOG4J_WARN },
7e8f2e9c 70 { .name = "INFO", .value = LTTNG_LOGLEVEL_LOG4J_INFO },
85b05318 71 { .name = "LOG4J_INFO", .value = LTTNG_LOGLEVEL_LOG4J_INFO },
7e8f2e9c 72 { .name = "DEBUG", .value = LTTNG_LOGLEVEL_LOG4J_DEBUG },
85b05318 73 { .name = "LOG4J_DEBUG", .value = LTTNG_LOGLEVEL_LOG4J_DEBUG },
7e8f2e9c 74 { .name = "TRACE", .value = LTTNG_LOGLEVEL_LOG4J_TRACE },
85b05318 75 { .name = "LOG4J_TRACE", .value = LTTNG_LOGLEVEL_LOG4J_TRACE },
7e8f2e9c 76 { .name = "ALL", .value = LTTNG_LOGLEVEL_LOG4J_ALL },
85b05318 77 { .name = "LOG4J_ALL", .value = LTTNG_LOGLEVEL_LOG4J_ALL },
7e8f2e9c
JG
78};
79
80static
81const struct loglevel_name_value loglevel_jul_values[] = {
7e8f2e9c 82 { .name = "OFF", .value = LTTNG_LOGLEVEL_JUL_OFF },
85b05318 83 { .name = "JUL_OFF", .value = LTTNG_LOGLEVEL_JUL_OFF },
7e8f2e9c 84 { .name = "SEVERE", .value = LTTNG_LOGLEVEL_JUL_SEVERE },
85b05318 85 { .name = "JUL_SEVERE", .value = LTTNG_LOGLEVEL_JUL_SEVERE },
7e8f2e9c 86 { .name = "WARNING", .value = LTTNG_LOGLEVEL_JUL_WARNING },
85b05318 87 { .name = "JUL_WARNING", .value = LTTNG_LOGLEVEL_JUL_WARNING },
7e8f2e9c 88 { .name = "INFO", .value = LTTNG_LOGLEVEL_JUL_INFO },
85b05318 89 { .name = "JUL_INFO", .value = LTTNG_LOGLEVEL_JUL_INFO },
7e8f2e9c 90 { .name = "CONFIG", .value = LTTNG_LOGLEVEL_JUL_CONFIG },
85b05318 91 { .name = "JUL_CONFIG", .value = LTTNG_LOGLEVEL_JUL_CONFIG },
7e8f2e9c 92 { .name = "FINE", .value = LTTNG_LOGLEVEL_JUL_FINE },
85b05318 93 { .name = "JUL_FINE", .value = LTTNG_LOGLEVEL_JUL_FINE },
7e8f2e9c 94 { .name = "FINER", .value = LTTNG_LOGLEVEL_JUL_FINER },
85b05318 95 { .name = "JUL_FINER", .value = LTTNG_LOGLEVEL_JUL_FINER },
7e8f2e9c 96 { .name = "FINEST", .value = LTTNG_LOGLEVEL_JUL_FINEST },
85b05318 97 { .name = "JUL_FINEST", .value = LTTNG_LOGLEVEL_JUL_FINEST },
7e8f2e9c 98 { .name = "ALL", .value = LTTNG_LOGLEVEL_JUL_ALL },
85b05318 99 { .name = "JUL_ALL", .value = LTTNG_LOGLEVEL_JUL_ALL },
7e8f2e9c
JG
100};
101
102static
103const struct loglevel_name_value loglevel_python_values[] = {
7e8f2e9c 104 { .name = "CRITICAL", .value = LTTNG_LOGLEVEL_PYTHON_CRITICAL },
85b05318 105 { .name = "PYTHON_CRITICAL", .value = LTTNG_LOGLEVEL_PYTHON_CRITICAL },
7e8f2e9c 106 { .name = "ERROR", .value = LTTNG_LOGLEVEL_PYTHON_ERROR },
85b05318 107 { .name = "PYTHON_ERROR", .value = LTTNG_LOGLEVEL_PYTHON_ERROR },
7e8f2e9c 108 { .name = "WARNING", .value = LTTNG_LOGLEVEL_PYTHON_WARNING },
85b05318 109 { .name = "PYTHON_WARNING", .value = LTTNG_LOGLEVEL_PYTHON_WARNING },
7e8f2e9c 110 { .name = "INFO", .value = LTTNG_LOGLEVEL_PYTHON_INFO },
85b05318 111 { .name = "PYTHON_INFO", .value = LTTNG_LOGLEVEL_PYTHON_INFO },
7e8f2e9c 112 { .name = "DEBUG", .value = LTTNG_LOGLEVEL_PYTHON_DEBUG },
85b05318 113 { .name = "PYTNON_DEBUG", .value = LTTNG_LOGLEVEL_PYTHON_DEBUG },
7e8f2e9c 114 { .name = "NOTSET", .value = LTTNG_LOGLEVEL_PYTHON_NOTSET },
85b05318 115 { .name = "PYTHON_NOTSET", .value = LTTNG_LOGLEVEL_PYTHON_NOTSET },
7e8f2e9c
JG
116};
117
118static
119bool string_equal_insensitive(const char *a, const char *b)
120{
f41294ed 121 return strcasecmp(a, b) == 0;
7e8f2e9c
JG
122}
123
124static
125int lookup_value_from_name(const struct loglevel_name_value values[],
126 size_t values_count, const char *name)
127{
128 size_t i;
129 int ret = -1;
130
131 if (!name) {
132 goto end;
133 }
134
135 for (i = 0; i < values_count; i++) {
136 if (string_equal_insensitive(values[i].name, name)) {
137 /* Match found. */
138 ret = values[i].value;
139 goto end;
140 }
141 }
142
143end:
144 return ret;
145}
146
949f049b
SM
147static bool loglevel_parse_range_string_common(const char *str,
148 const struct loglevel_name_value *nvs,
149 size_t nvs_count,
150 int *min,
151 int *max)
152{
153 bool ret;
154 int i;
155 const struct loglevel_name_value *nv;
156
157 for (i = 0; i < nvs_count; i++) {
158 nv = &nvs[i];
159
160 if (strncmp(str, nv->name, strlen(nv->name)) == 0) {
161 break;
162 }
163 }
164
165 if (i == nvs_count) {
166 goto error;
167 }
168
169 *min = nv->value;
170 str += strlen(nv->name);
171
172 if (*str == '\0') {
173 *max = nv->value;
174 ret = true;
175 goto end;
176 }
177
178 if (strncmp(str, "..", strlen("..")) != 0) {
179 goto error;
180 }
181
182 str += strlen("..");
183
184 if (*str == '\0') {
185 *max = LTTNG_LOGLEVEL_EMERG;
186 ret = true;
187 goto end;
188 }
189
190 for (i = 0; i < nvs_count; i++) {
191 nv = &nvs[i];
192
193 if (strcmp(str, nv->name) == 0) {
194 break;
195 }
196 }
197
198 if (i == nvs_count) {
199 goto error;
200 }
201
202 *max = nv->value;
203
204 ret = true;
205 goto end;
206
207error:
208 ret = false;
209
210end:
211 return ret;
212}
213
7e8f2e9c
JG
214LTTNG_HIDDEN
215int loglevel_name_to_value(const char *name, enum lttng_loglevel *loglevel)
216{
217 int ret = lookup_value_from_name(loglevel_values,
00608d5a 218 ARRAY_SIZE(loglevel_values), name);
7e8f2e9c
JG
219
220 if (ret >= 0) {
221 *loglevel = (typeof(*loglevel)) ret;
222 ret = 0;
223 }
224
225 return ret;
226}
227
949f049b
SM
228LTTNG_HIDDEN
229bool loglevel_parse_range_string(const char *str,
230 enum lttng_loglevel *min,
231 enum lttng_loglevel *max)
232{
233 int min_int, max_int;
234 bool ret = loglevel_parse_range_string_common(str, loglevel_values,
235 ARRAY_SIZE(loglevel_values), &min_int, &max_int);
236
237 *min = min_int;
238 *max = max_int;
239
240 return ret;
241}
242
7e8f2e9c
JG
243LTTNG_HIDDEN
244int loglevel_log4j_name_to_value(
245 const char *name, enum lttng_loglevel_log4j *loglevel)
246{
247 int ret = lookup_value_from_name(loglevel_log4j_values,
00608d5a 248 ARRAY_SIZE(loglevel_log4j_values),
7e8f2e9c
JG
249 name);
250
251 if (ret >= 0) {
252 *loglevel = (typeof(*loglevel)) ret;
253 ret = 0;
254 }
255
256 return ret;
257}
258
949f049b
SM
259LTTNG_HIDDEN
260bool loglevel_log4j_parse_range_string(const char *str,
261 enum lttng_loglevel_log4j *min,
262 enum lttng_loglevel_log4j *max)
263{
264 int min_int, max_int;
265 bool ret = loglevel_parse_range_string_common(str,
266 loglevel_log4j_values,
267 ARRAY_SIZE(loglevel_log4j_values), &min_int, &max_int);
268
269 *min = min_int;
270 *max = max_int;
271
272 return ret;
273}
274
7e8f2e9c
JG
275LTTNG_HIDDEN
276int loglevel_jul_name_to_value(
277 const char *name, enum lttng_loglevel_jul *loglevel)
278{
279 int ret = lookup_value_from_name(loglevel_jul_values,
00608d5a 280 ARRAY_SIZE(loglevel_jul_values),
7e8f2e9c
JG
281 name);
282
283 if (ret >= 0) {
284 *loglevel = (typeof(*loglevel)) ret;
285 ret = 0;
286 }
287
288 return ret;
289}
290
949f049b
SM
291LTTNG_HIDDEN
292bool loglevel_jul_parse_range_string(const char *str,
293 enum lttng_loglevel_jul *min,
294 enum lttng_loglevel_jul *max)
295{
296 int min_int, max_int;
297 bool ret = loglevel_parse_range_string_common(str, loglevel_jul_values,
298 ARRAY_SIZE(loglevel_jul_values), &min_int, &max_int);
299
300 *min = min_int;
301 *max = max_int;
302
303 return ret;
304}
305
7e8f2e9c
JG
306LTTNG_HIDDEN
307int loglevel_python_name_to_value(
308 const char *name, enum lttng_loglevel_python *loglevel)
309{
310 int ret = lookup_value_from_name(loglevel_python_values,
00608d5a 311 ARRAY_SIZE(loglevel_python_values),
7e8f2e9c
JG
312 name);
313
314 if (ret >= 0) {
315 *loglevel = (typeof(*loglevel)) ret;
316 ret = 0;
317 }
318
949f049b
SM
319 return ret;
320}
321
322LTTNG_HIDDEN
323bool loglevel_python_parse_range_string(const char *str,
324 enum lttng_loglevel_python *min,
325 enum lttng_loglevel_python *max)
326{
327 int min_int, max_int;
328 bool ret = loglevel_parse_range_string_common(str,
329 loglevel_python_values,
330 ARRAY_SIZE(loglevel_python_values), &min_int, &max_int);
331
332 *min = min_int;
333 *max = max_int;
334
7e8f2e9c 335 return ret;
00608d5a 336}
85b05318
JR
337
338static
339const char *lookup_name_from_value(const struct loglevel_name_value values[],
340 size_t values_count, int loglevel)
341{
342 size_t i;
343 const char *name = NULL;
344
345 for (i = 0; i < values_count; i++) {
346 if (values[i].value == loglevel) {
347 /* Match found. */
348 name = values[i].name;
349 goto end;
350 }
351 }
352
353end:
354 return name;
355}
356
357LTTNG_HIDDEN
358const char *loglevel_value_to_name(int loglevel)
359{
360 return lookup_name_from_value(
361 loglevel_values, ARRAY_SIZE(loglevel_values), loglevel);
362}
363
364LTTNG_HIDDEN
365const char *loglevel_log4j_value_to_name(int loglevel)
366{
367 return lookup_name_from_value(loglevel_log4j_values,
368 ARRAY_SIZE(loglevel_log4j_values), loglevel);
369}
370
371LTTNG_HIDDEN
372const char *loglevel_jul_value_to_name(int loglevel)
373{
374 return lookup_name_from_value(loglevel_jul_values,
375 ARRAY_SIZE(loglevel_jul_values), loglevel);
376}
377
378LTTNG_HIDDEN
379const char *loglevel_python_value_to_name(int loglevel)
380{
381 return lookup_name_from_value(loglevel_python_values,
382 ARRAY_SIZE(loglevel_python_values), loglevel);
383}
This page took 0.042493 seconds and 4 git commands to generate.