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