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