2 * Copyright (C) 2021 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "loglevel.hpp"
14 struct loglevel_name_value
{
21 const struct loglevel_name_value loglevel_values
[] = {
22 { .name
= "EMERG", .value
= LTTNG_LOGLEVEL_EMERG
},
23 { .name
= "TRACE_EMERG", .value
= LTTNG_LOGLEVEL_EMERG
},
24 { .name
= "ALERT", .value
= LTTNG_LOGLEVEL_ALERT
},
25 { .name
= "TRACE_ALERT", .value
= LTTNG_LOGLEVEL_ALERT
},
26 { .name
= "CRIT", .value
= LTTNG_LOGLEVEL_CRIT
},
27 { .name
= "TRACE_CRIT", .value
= LTTNG_LOGLEVEL_CRIT
},
28 { .name
= "ERR", .value
= LTTNG_LOGLEVEL_ERR
},
29 { .name
= "TRACE_ERR", .value
= LTTNG_LOGLEVEL_ERR
},
30 { .name
= "WARNING", .value
= LTTNG_LOGLEVEL_WARNING
},
31 { .name
= "TRACE_WARNING", .value
= LTTNG_LOGLEVEL_WARNING
},
32 { .name
= "NOTICE", .value
= LTTNG_LOGLEVEL_NOTICE
},
33 { .name
= "TRACE_NOTICE", .value
= LTTNG_LOGLEVEL_NOTICE
},
34 { .name
= "INFO", .value
= LTTNG_LOGLEVEL_INFO
},
35 { .name
= "TRACE_INFO", .value
= LTTNG_LOGLEVEL_INFO
},
36 { .name
= "DEBUG_SYSTEM", .value
= LTTNG_LOGLEVEL_DEBUG_SYSTEM
},
37 { .name
= "TRACE_DEBUG_SYSTEM", .value
= LTTNG_LOGLEVEL_DEBUG_SYSTEM
},
38 { .name
= "SYSTEM", .value
= LTTNG_LOGLEVEL_DEBUG_SYSTEM
},
39 { .name
= "DEBUG_PROGRAM", .value
= LTTNG_LOGLEVEL_DEBUG_PROGRAM
},
40 { .name
= "TRACE_DEBUG_PROGRAM", .value
= LTTNG_LOGLEVEL_DEBUG_PROGRAM
},
41 { .name
= "PROGRAM", .value
= LTTNG_LOGLEVEL_DEBUG_PROGRAM
},
42 { .name
= "DEBUG_PROCESS", .value
= LTTNG_LOGLEVEL_DEBUG_PROCESS
},
43 { .name
= "TRACE_DEBUG_PROCESS", .value
= LTTNG_LOGLEVEL_DEBUG_PROCESS
},
44 { .name
= "PROCESS", .value
= LTTNG_LOGLEVEL_DEBUG_PROCESS
},
45 { .name
= "DEBUG_MODULE", .value
= LTTNG_LOGLEVEL_DEBUG_MODULE
},
46 { .name
= "TRACE_DEBUG_MODULE", .value
= LTTNG_LOGLEVEL_DEBUG_MODULE
},
47 { .name
= "MODULE", .value
= LTTNG_LOGLEVEL_DEBUG_MODULE
},
48 { .name
= "DEBUG_UNIT", .value
= LTTNG_LOGLEVEL_DEBUG_UNIT
},
49 { .name
= "TRACE_DEBUG_UNIT", .value
= LTTNG_LOGLEVEL_DEBUG_UNIT
},
50 { .name
= "UNIT", .value
= LTTNG_LOGLEVEL_DEBUG_UNIT
},
51 { .name
= "DEBUG_FUNCTION", .value
= LTTNG_LOGLEVEL_DEBUG_FUNCTION
},
52 { .name
= "TRACE_DEBUG_FUNCTION", .value
= LTTNG_LOGLEVEL_DEBUG_FUNCTION
},
53 { .name
= "FUNCTION", .value
= LTTNG_LOGLEVEL_DEBUG_FUNCTION
},
54 { .name
= "DEBUG_LINE", .value
= LTTNG_LOGLEVEL_DEBUG_LINE
},
55 { .name
= "TRACE_DEBUG_LINE", .value
= LTTNG_LOGLEVEL_DEBUG_LINE
},
56 { .name
= "LINE", .value
= LTTNG_LOGLEVEL_DEBUG_LINE
},
57 { .name
= "DEBUG", .value
= LTTNG_LOGLEVEL_DEBUG
},
58 { .name
= "TRACE_DEBUG", .value
= LTTNG_LOGLEVEL_DEBUG
},
62 const struct loglevel_name_value loglevel_log4j_values
[] = {
63 { .name
= "OFF", .value
= LTTNG_LOGLEVEL_LOG4J_OFF
},
64 { .name
= "LOG4J_OFF", .value
= LTTNG_LOGLEVEL_LOG4J_OFF
},
65 { .name
= "FATAL", .value
= LTTNG_LOGLEVEL_LOG4J_FATAL
},
66 { .name
= "LOG4J_FATAL", .value
= LTTNG_LOGLEVEL_LOG4J_FATAL
},
67 { .name
= "ERROR", .value
= LTTNG_LOGLEVEL_LOG4J_ERROR
},
68 { .name
= "LOG4J_ERROR", .value
= LTTNG_LOGLEVEL_LOG4J_ERROR
},
69 { .name
= "WARN", .value
= LTTNG_LOGLEVEL_LOG4J_WARN
},
70 { .name
= "LOG4J_WARN", .value
= LTTNG_LOGLEVEL_LOG4J_WARN
},
71 { .name
= "INFO", .value
= LTTNG_LOGLEVEL_LOG4J_INFO
},
72 { .name
= "LOG4J_INFO", .value
= LTTNG_LOGLEVEL_LOG4J_INFO
},
73 { .name
= "DEBUG", .value
= LTTNG_LOGLEVEL_LOG4J_DEBUG
},
74 { .name
= "LOG4J_DEBUG", .value
= LTTNG_LOGLEVEL_LOG4J_DEBUG
},
75 { .name
= "TRACE", .value
= LTTNG_LOGLEVEL_LOG4J_TRACE
},
76 { .name
= "LOG4J_TRACE", .value
= LTTNG_LOGLEVEL_LOG4J_TRACE
},
77 { .name
= "ALL", .value
= LTTNG_LOGLEVEL_LOG4J_ALL
},
78 { .name
= "LOG4J_ALL", .value
= LTTNG_LOGLEVEL_LOG4J_ALL
},
82 const struct loglevel_name_value loglevel_jul_values
[] = {
83 { .name
= "OFF", .value
= LTTNG_LOGLEVEL_JUL_OFF
},
84 { .name
= "JUL_OFF", .value
= LTTNG_LOGLEVEL_JUL_OFF
},
85 { .name
= "SEVERE", .value
= LTTNG_LOGLEVEL_JUL_SEVERE
},
86 { .name
= "JUL_SEVERE", .value
= LTTNG_LOGLEVEL_JUL_SEVERE
},
87 { .name
= "WARNING", .value
= LTTNG_LOGLEVEL_JUL_WARNING
},
88 { .name
= "JUL_WARNING", .value
= LTTNG_LOGLEVEL_JUL_WARNING
},
89 { .name
= "INFO", .value
= LTTNG_LOGLEVEL_JUL_INFO
},
90 { .name
= "JUL_INFO", .value
= LTTNG_LOGLEVEL_JUL_INFO
},
91 { .name
= "CONFIG", .value
= LTTNG_LOGLEVEL_JUL_CONFIG
},
92 { .name
= "JUL_CONFIG", .value
= LTTNG_LOGLEVEL_JUL_CONFIG
},
93 { .name
= "FINE", .value
= LTTNG_LOGLEVEL_JUL_FINE
},
94 { .name
= "JUL_FINE", .value
= LTTNG_LOGLEVEL_JUL_FINE
},
95 { .name
= "FINER", .value
= LTTNG_LOGLEVEL_JUL_FINER
},
96 { .name
= "JUL_FINER", .value
= LTTNG_LOGLEVEL_JUL_FINER
},
97 { .name
= "FINEST", .value
= LTTNG_LOGLEVEL_JUL_FINEST
},
98 { .name
= "JUL_FINEST", .value
= LTTNG_LOGLEVEL_JUL_FINEST
},
99 { .name
= "ALL", .value
= LTTNG_LOGLEVEL_JUL_ALL
},
100 { .name
= "JUL_ALL", .value
= LTTNG_LOGLEVEL_JUL_ALL
},
104 const struct loglevel_name_value loglevel_python_values
[] = {
105 { .name
= "CRITICAL", .value
= LTTNG_LOGLEVEL_PYTHON_CRITICAL
},
106 { .name
= "PYTHON_CRITICAL", .value
= LTTNG_LOGLEVEL_PYTHON_CRITICAL
},
107 { .name
= "ERROR", .value
= LTTNG_LOGLEVEL_PYTHON_ERROR
},
108 { .name
= "PYTHON_ERROR", .value
= LTTNG_LOGLEVEL_PYTHON_ERROR
},
109 { .name
= "WARNING", .value
= LTTNG_LOGLEVEL_PYTHON_WARNING
},
110 { .name
= "PYTHON_WARNING", .value
= LTTNG_LOGLEVEL_PYTHON_WARNING
},
111 { .name
= "INFO", .value
= LTTNG_LOGLEVEL_PYTHON_INFO
},
112 { .name
= "PYTHON_INFO", .value
= LTTNG_LOGLEVEL_PYTHON_INFO
},
113 { .name
= "DEBUG", .value
= LTTNG_LOGLEVEL_PYTHON_DEBUG
},
114 { .name
= "PYTNON_DEBUG", .value
= LTTNG_LOGLEVEL_PYTHON_DEBUG
},
115 { .name
= "NOTSET", .value
= LTTNG_LOGLEVEL_PYTHON_NOTSET
},
116 { .name
= "PYTHON_NOTSET", .value
= LTTNG_LOGLEVEL_PYTHON_NOTSET
},
120 bool string_equal_insensitive(const char *a
, const char *b
)
122 return strcasecmp(a
, b
) == 0;
126 int lookup_value_from_name(const struct loglevel_name_value values
[],
127 size_t values_count
, const char *name
)
136 for (i
= 0; i
< values_count
; i
++) {
137 if (string_equal_insensitive(values
[i
].name
, name
)) {
139 ret
= values
[i
].value
;
148 static bool loglevel_parse_range_string_common(const char *str
,
149 const struct loglevel_name_value
*nvs
,
156 const struct loglevel_name_value
*nv
;
158 for (i
= 0; i
< nvs_count
; i
++) {
161 if (strncmp(str
, nv
->name
, strlen(nv
->name
)) == 0) {
166 if (i
== nvs_count
) {
171 str
+= strlen(nv
->name
);
179 if (strncmp(str
, "..", strlen("..")) != 0) {
186 *max
= LTTNG_LOGLEVEL_EMERG
;
191 for (i
= 0; i
< nvs_count
; i
++) {
194 if (strcmp(str
, nv
->name
) == 0) {
199 if (i
== nvs_count
) {
215 int loglevel_name_to_value(const char *name
, enum lttng_loglevel
*loglevel
)
217 int ret
= lookup_value_from_name(loglevel_values
,
218 ARRAY_SIZE(loglevel_values
), name
);
221 *loglevel
= (typeof(*loglevel
)) ret
;
228 bool loglevel_parse_range_string(const char *str
,
229 enum lttng_loglevel
*min
,
230 enum lttng_loglevel
*max
)
232 int min_int
, max_int
;
233 bool ret
= loglevel_parse_range_string_common(str
, loglevel_values
,
234 ARRAY_SIZE(loglevel_values
), &min_int
, &max_int
);
236 *min
= (lttng_loglevel
) min_int
;
237 *max
= (lttng_loglevel
) max_int
;
242 int loglevel_log4j_name_to_value(
243 const char *name
, enum lttng_loglevel_log4j
*loglevel
)
245 int ret
= lookup_value_from_name(loglevel_log4j_values
,
246 ARRAY_SIZE(loglevel_log4j_values
),
250 *loglevel
= (typeof(*loglevel
)) ret
;
257 bool loglevel_log4j_parse_range_string(const char *str
,
258 enum lttng_loglevel_log4j
*min
,
259 enum lttng_loglevel_log4j
*max
)
261 int min_int
, max_int
;
262 bool ret
= loglevel_parse_range_string_common(str
,
263 loglevel_log4j_values
,
264 ARRAY_SIZE(loglevel_log4j_values
), &min_int
, &max_int
);
266 *min
= (lttng_loglevel_log4j
) min_int
;
267 *max
= (lttng_loglevel_log4j
) max_int
;
272 int loglevel_jul_name_to_value(
273 const char *name
, enum lttng_loglevel_jul
*loglevel
)
275 int ret
= lookup_value_from_name(loglevel_jul_values
,
276 ARRAY_SIZE(loglevel_jul_values
),
280 *loglevel
= (typeof(*loglevel
)) ret
;
287 bool loglevel_jul_parse_range_string(const char *str
,
288 enum lttng_loglevel_jul
*min
,
289 enum lttng_loglevel_jul
*max
)
291 int min_int
, max_int
;
292 bool ret
= loglevel_parse_range_string_common(str
, loglevel_jul_values
,
293 ARRAY_SIZE(loglevel_jul_values
), &min_int
, &max_int
);
295 *min
= (lttng_loglevel_jul
) min_int
;
296 *max
= (lttng_loglevel_jul
) max_int
;
301 int loglevel_python_name_to_value(
302 const char *name
, enum lttng_loglevel_python
*loglevel
)
304 int ret
= lookup_value_from_name(loglevel_python_values
,
305 ARRAY_SIZE(loglevel_python_values
),
309 *loglevel
= (typeof(*loglevel
)) ret
;
316 bool loglevel_python_parse_range_string(const char *str
,
317 enum lttng_loglevel_python
*min
,
318 enum lttng_loglevel_python
*max
)
320 int min_int
, max_int
;
321 bool ret
= loglevel_parse_range_string_common(str
,
322 loglevel_python_values
,
323 ARRAY_SIZE(loglevel_python_values
), &min_int
, &max_int
);
325 *min
= (lttng_loglevel_python
) min_int
;
326 *max
= (lttng_loglevel_python
) max_int
;
332 const char *lookup_name_from_value(const struct loglevel_name_value values
[],
333 size_t values_count
, int loglevel
)
336 const char *name
= NULL
;
338 for (i
= 0; i
< values_count
; i
++) {
339 if (values
[i
].value
== loglevel
) {
341 name
= values
[i
].name
;
350 const char *loglevel_value_to_name(int loglevel
)
352 return lookup_name_from_value(
353 loglevel_values
, ARRAY_SIZE(loglevel_values
), loglevel
);
356 const char *loglevel_log4j_value_to_name(int loglevel
)
358 return lookup_name_from_value(loglevel_log4j_values
,
359 ARRAY_SIZE(loglevel_log4j_values
), loglevel
);
362 const char *loglevel_jul_value_to_name(int loglevel
)
364 return lookup_name_from_value(loglevel_jul_values
,
365 ARRAY_SIZE(loglevel_jul_values
), loglevel
);
368 const char *loglevel_python_value_to_name(int loglevel
)
370 return lookup_name_from_value(loglevel_python_values
,
371 ARRAY_SIZE(loglevel_python_values
), loglevel
);