2 * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/dynamic-buffer.h>
10 #include <common/error.h>
11 #include <common/macros.h>
12 #include <common/hashtable/hashtable.h>
13 #include <common/hashtable/utils.h>
14 #include <lttng/log-level-rule-internal.h>
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(sizeof(struct 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(sizeof(struct 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
);
115 ssize_t
lttng_log_level_rule_create_from_payload(
116 struct lttng_payload_view
*view
,
117 struct lttng_log_level_rule
**_rule
)
121 struct lttng_log_level_rule
*rule
= NULL
;
122 const struct lttng_log_level_rule_comm
*comm
=
123 (const struct lttng_log_level_rule_comm
*)
126 offset
+= sizeof(*comm
);
133 if (view
->buffer
.size
< sizeof(*comm
)) {
138 switch (comm
->type
) {
139 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
140 rule
= lttng_log_level_rule_exactly_create((int) comm
->level
);
142 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
143 rule
= lttng_log_level_rule_at_least_as_severe_as_create(
163 int lttng_log_level_rule_serialize(const struct lttng_log_level_rule
*rule
,
164 struct lttng_payload
*payload
)
167 struct lttng_log_level_rule_comm comm
;
175 comm
.type
= (int8_t) rule
->type
;
176 comm
.level
= (int32_t) rule
->level
;
178 DBG("Serializing log level rule of type %d", rule
->type
);
179 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &comm
,
190 bool lttng_log_level_rule_is_equal(const struct lttng_log_level_rule
*a
,
191 const struct lttng_log_level_rule
*b
)
193 bool is_equal
= false;
195 if (a
== NULL
&& b
== NULL
) {
201 if (a
== NULL
|| b
== NULL
) {
212 if (a
->type
!= b
->type
) {
216 if (a
->level
!= b
->level
) {
227 struct lttng_log_level_rule
*lttng_log_level_rule_copy(
228 const struct lttng_log_level_rule
*source
)
230 struct lttng_log_level_rule
*copy
= NULL
;
234 copy
= zmalloc(sizeof(struct lttng_log_level_rule
));
239 copy
->type
= source
->type
;
240 copy
->level
= source
->level
;
246 void lttng_log_level_rule_to_loglevel(
247 const struct lttng_log_level_rule
*log_level_rule
,
248 enum lttng_loglevel_type
*loglevel_type
,
251 assert(log_level_rule
);
253 switch (log_level_rule
->type
) {
254 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
255 *loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
257 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
258 *loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
264 *loglevel_value
= log_level_rule
->level
;
268 unsigned long lttng_log_level_rule_hash(
269 const struct lttng_log_level_rule
*log_level_rule
)
272 enum lttng_log_level_rule_status llr_status
;
274 enum lttng_log_level_rule_type type
;
276 assert(log_level_rule
);
278 type
= lttng_log_level_rule_get_type(log_level_rule
);
281 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
282 llr_status
= lttng_log_level_rule_exactly_get_level(
283 log_level_rule
, &log_level_value
);
285 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
286 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
287 log_level_rule
, &log_level_value
);
294 assert(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
296 hash
= hash_key_ulong((void *) (unsigned long) type
, lttng_ht_seed
);
298 hash
^= hash_key_ulong((void *) (unsigned long) log_level_value
,