2 * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/dynamic-buffer.hpp>
9 #include <common/error.hpp>
10 #include <common/hashtable/hashtable.hpp>
11 #include <common/hashtable/utils.hpp>
12 #include <common/macros.hpp>
13 #include <common/mi-lttng.hpp>
14 #include <lttng/log-level-rule-internal.hpp>
15 #include <lttng/log-level-rule.h>
19 static bool is_log_level_rule_exactly_type(const struct lttng_log_level_rule
*rule
)
21 enum lttng_log_level_rule_type type
=
22 lttng_log_level_rule_get_type(rule
);
24 return type
== LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
;
27 static bool is_log_level_rule_at_least_as_severe_type(const struct lttng_log_level_rule
*rule
)
30 enum lttng_log_level_rule_type type
=
31 lttng_log_level_rule_get_type(rule
);
33 return type
== LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
;
36 enum lttng_log_level_rule_type
lttng_log_level_rule_get_type(
37 const struct lttng_log_level_rule
*rule
)
39 return rule
? rule
->type
: LTTNG_LOG_LEVEL_RULE_TYPE_UNKNOWN
;
42 struct lttng_log_level_rule
*lttng_log_level_rule_exactly_create(
45 struct lttng_log_level_rule
*rule
= NULL
;
47 rule
= zmalloc
<lttng_log_level_rule
>();
52 rule
->type
= LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
;
59 enum lttng_log_level_rule_status
lttng_log_level_rule_exactly_get_level(
60 const struct lttng_log_level_rule
*rule
, int *level
)
62 enum lttng_log_level_rule_status status
=
63 LTTNG_LOG_LEVEL_RULE_STATUS_OK
;
65 if (!rule
|| !level
|| !is_log_level_rule_exactly_type(rule
)) {
66 status
= LTTNG_LOG_LEVEL_RULE_STATUS_INVALID
;
75 struct lttng_log_level_rule
*
76 lttng_log_level_rule_at_least_as_severe_as_create(int level
)
78 struct lttng_log_level_rule
*rule
= NULL
;
80 rule
= zmalloc
<lttng_log_level_rule
>();
85 rule
->type
= LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
;
92 enum lttng_log_level_rule_status
93 lttng_log_level_rule_at_least_as_severe_as_get_level(
94 const struct lttng_log_level_rule
*rule
, int *level
)
96 enum lttng_log_level_rule_status status
= LTTNG_LOG_LEVEL_RULE_STATUS_OK
;
98 if (!rule
|| !level
||
99 !is_log_level_rule_at_least_as_severe_type(rule
)) {
100 status
= LTTNG_LOG_LEVEL_RULE_STATUS_INVALID
;
104 *level
= rule
->level
;
109 void lttng_log_level_rule_destroy(struct lttng_log_level_rule
*log_level_rule
)
111 free(log_level_rule
);
114 ssize_t
lttng_log_level_rule_create_from_payload(
115 struct lttng_payload_view
*view
,
116 struct lttng_log_level_rule
**_rule
)
120 struct lttng_log_level_rule
*rule
= NULL
;
121 const struct lttng_log_level_rule_comm
*comm
=
122 (const struct lttng_log_level_rule_comm
*)
125 offset
+= sizeof(*comm
);
132 if (view
->buffer
.size
< sizeof(*comm
)) {
137 switch (comm
->type
) {
138 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
139 rule
= lttng_log_level_rule_exactly_create((int) comm
->level
);
141 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
142 rule
= lttng_log_level_rule_at_least_as_severe_as_create(
161 int lttng_log_level_rule_serialize(const struct lttng_log_level_rule
*rule
,
162 struct lttng_payload
*payload
)
165 struct lttng_log_level_rule_comm comm
;
173 comm
.type
= (int8_t) rule
->type
;
174 comm
.level
= (int32_t) rule
->level
;
176 DBG("Serializing log level rule of type %d", rule
->type
);
177 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
187 bool lttng_log_level_rule_is_equal(const struct lttng_log_level_rule
*a
,
188 const struct lttng_log_level_rule
*b
)
190 bool is_equal
= false;
192 if (a
== NULL
&& b
== NULL
) {
198 if (a
== NULL
|| b
== NULL
) {
209 if (a
->type
!= b
->type
) {
213 if (a
->level
!= b
->level
) {
223 struct lttng_log_level_rule
*lttng_log_level_rule_copy(
224 const struct lttng_log_level_rule
*source
)
226 struct lttng_log_level_rule
*copy
= NULL
;
228 LTTNG_ASSERT(source
);
230 copy
= zmalloc
<lttng_log_level_rule
>();
235 copy
->type
= source
->type
;
236 copy
->level
= source
->level
;
241 void lttng_log_level_rule_to_loglevel(
242 const struct lttng_log_level_rule
*log_level_rule
,
243 enum lttng_loglevel_type
*loglevel_type
,
246 LTTNG_ASSERT(log_level_rule
);
248 switch (log_level_rule
->type
) {
249 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
250 *loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
252 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
253 *loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
259 *loglevel_value
= log_level_rule
->level
;
262 unsigned long lttng_log_level_rule_hash(
263 const struct lttng_log_level_rule
*log_level_rule
)
266 enum lttng_log_level_rule_status llr_status
;
268 enum lttng_log_level_rule_type type
;
270 LTTNG_ASSERT(log_level_rule
);
272 type
= lttng_log_level_rule_get_type(log_level_rule
);
275 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
276 llr_status
= lttng_log_level_rule_exactly_get_level(
277 log_level_rule
, &log_level_value
);
279 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
280 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
281 log_level_rule
, &log_level_value
);
288 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
290 hash
= hash_key_ulong((void *) (unsigned long) type
, lttng_ht_seed
);
292 hash
^= hash_key_ulong((void *) (unsigned long) log_level_value
,
298 enum lttng_error_code
lttng_log_level_rule_mi_serialize(
299 const struct lttng_log_level_rule
*rule
,
300 struct mi_writer
*writer
)
303 enum lttng_error_code ret_code
;
304 enum lttng_log_level_rule_status status
;
305 const char *element_str
= NULL
;
309 LTTNG_ASSERT(writer
);
311 switch (lttng_log_level_rule_get_type(rule
)) {
312 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
313 status
= lttng_log_level_rule_exactly_get_level(rule
, &level
);
314 element_str
= mi_lttng_element_log_level_rule_exactly
;
316 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
317 element_str
= mi_lttng_element_log_level_rule_at_least_as_severe_as
;
318 status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
326 LTTNG_ASSERT(status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
328 /* Open log level rule element. */
329 ret
= mi_lttng_writer_open_element(
330 writer
, mi_lttng_element_log_level_rule
);
335 /* Log level rule type element. */
336 ret
= mi_lttng_writer_open_element(writer
, element_str
);
342 ret
= mi_lttng_writer_write_element_signed_int(
343 writer
, mi_lttng_element_log_level_rule_level
, level
);
348 /* Close log level rule type element. */
349 ret
= mi_lttng_writer_close_element(writer
);
354 /* Close log level rule element. */
355 ret
= mi_lttng_writer_close_element(writer
);
364 ret_code
= LTTNG_ERR_MI_IO_FAIL
;