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