Extract synchronize tracer notifier functions
[lttng-tools.git] / src / common / log-level-rule.c
CommitLineData
85b05318
JR
1/*
2 * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8#include <assert.h>
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>
16#include <stdbool.h>
17#include <stdlib.h>
18
19static bool is_log_level_rule_exactly_type(const struct lttng_log_level_rule *rule)
20{
21 enum lttng_log_level_rule_type type =
22 lttng_log_level_rule_get_type(rule);
23
24 return type == LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY;
25}
26
27static bool is_log_level_rule_at_least_as_severe_type(const struct lttng_log_level_rule *rule)
28{
29
30 enum lttng_log_level_rule_type type =
31 lttng_log_level_rule_get_type(rule);
32
33 return type == LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS;
34}
35
36enum lttng_log_level_rule_type lttng_log_level_rule_get_type(
37 const struct lttng_log_level_rule *rule)
38{
39 return rule ? rule->type : LTTNG_LOG_LEVEL_RULE_TYPE_UNKNOWN;
40}
41
42struct lttng_log_level_rule *lttng_log_level_rule_exactly_create(
43 int level)
44{
45 struct lttng_log_level_rule *rule = NULL;
46
47 rule = zmalloc(sizeof(struct lttng_log_level_rule));
48 if (!rule) {
49 goto end;
50 }
51
52 rule->type = LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY;
53 rule->level = level;
54
55end:
56 return rule;
57}
58
59enum lttng_log_level_rule_status lttng_log_level_rule_exactly_get_level(
60 const struct lttng_log_level_rule *rule, int *level)
61{
62 enum lttng_log_level_rule_status status =
63 LTTNG_LOG_LEVEL_RULE_STATUS_OK;
64
65 if (!rule || !level || !is_log_level_rule_exactly_type(rule)) {
66 status = LTTNG_LOG_LEVEL_RULE_STATUS_INVALID;
67 goto end;
68 }
69
70 *level = rule->level;
71end:
72 return status;
73}
74
75struct lttng_log_level_rule *
76lttng_log_level_rule_at_least_as_severe_as_create(int level)
77{
78 struct lttng_log_level_rule *rule = NULL;
79
80 rule = zmalloc(sizeof(struct lttng_log_level_rule));
81 if (!rule) {
82 goto end;
83 }
84
85 rule->type = LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS;
86 rule->level = level;
87
88end:
89 return rule;
90}
91
92enum lttng_log_level_rule_status
93lttng_log_level_rule_at_least_as_severe_as_get_level(
94 const struct lttng_log_level_rule *rule, int *level)
95{
96 enum lttng_log_level_rule_status status = LTTNG_LOG_LEVEL_RULE_STATUS_OK;
97
98 if (!rule || !level ||
99 !is_log_level_rule_at_least_as_severe_type(rule)) {
100 status = LTTNG_LOG_LEVEL_RULE_STATUS_INVALID;
101 goto end;
102 }
103
104 *level = rule->level;
105end:
106 return status;
107}
108
109void lttng_log_level_rule_destroy(struct lttng_log_level_rule *log_level_rule)
110{
111 free(log_level_rule);
112}
113
114LTTNG_HIDDEN
115ssize_t lttng_log_level_rule_create_from_payload(
116 struct lttng_payload_view *view,
117 struct lttng_log_level_rule **_rule)
118{
119 ssize_t ret;
120 size_t offset = 0;
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 *)
124 view->buffer.data;
125
126 offset += sizeof(*comm);
127
128 if (!_rule) {
129 ret = -1;
130 goto end;
131 }
132
133 if (view->buffer.size < sizeof(*comm)) {
134 ret = -1;
135 goto end;
136 }
137
138 switch (comm->type) {
139 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
140 rule = lttng_log_level_rule_exactly_create((int) comm->level);
141 break;
142 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
143 rule = lttng_log_level_rule_at_least_as_severe_as_create(
144 (int) comm->level);
145 break;
146 default:
147 abort();
148 }
149
150 if (!rule) {
151 ret = -1;
152 goto end;
153 }
154
155 *_rule = rule;
156 ret = offset;
157
158end:
159 return ret;
160}
161
162LTTNG_HIDDEN
163int lttng_log_level_rule_serialize(const struct lttng_log_level_rule *rule,
164 struct lttng_payload *payload)
165{
166 int ret;
167 struct lttng_log_level_rule_comm comm;
168
169
170 if (!rule) {
171 ret = 0;
172 goto end;
173 }
174
175 comm.type = (int8_t) rule->type;
176 comm.level = (int32_t) rule->level;
177
178 DBG("Serializing log level rule of type %d", rule->type);
179 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm,
180 sizeof(comm));
181 if (ret) {
182 goto end;
183 }
184
185end:
186 return ret;
187}
188
189LTTNG_HIDDEN
190bool lttng_log_level_rule_is_equal(const struct lttng_log_level_rule *a,
191 const struct lttng_log_level_rule *b)
192{
193 bool is_equal = false;
194
195 if (a == NULL && b == NULL) {
196 /* Both are null. */
197 is_equal = true;
198 goto end;
199 }
200
201 if (a == NULL || b == NULL) {
202 /* One is NULL.*/
203 goto end;
204 }
205
206 if (a == b) {
207 /* Same object.*/
208 is_equal = true;
209 goto end;
210 }
211
212 if (a->type != b->type) {
213 goto end;
214 }
215
216 if (a->level != b->level) {
217 goto end;
218 }
219
220 is_equal = true;
221
222end:
223 return is_equal;
224}
225
226LTTNG_HIDDEN
227struct lttng_log_level_rule *lttng_log_level_rule_copy(
228 const struct lttng_log_level_rule *source)
229{
230 struct lttng_log_level_rule *copy = NULL;
231
232 assert(source);
233
234 copy = zmalloc(sizeof(struct lttng_log_level_rule));
235 if (!copy) {
236 goto end;
237 }
238
239 copy->type = source->type;
240 copy->level = source->level;
241end:
242 return copy;
243}
244
245LTTNG_HIDDEN
246void lttng_log_level_rule_to_loglevel(
247 const struct lttng_log_level_rule *log_level_rule,
248 enum lttng_loglevel_type *loglevel_type,
249 int *loglevel_value)
250{
251 assert(log_level_rule);
252
253 switch (log_level_rule->type) {
254 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
255 *loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
256 break;
257 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
258 *loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
259 break;
260 default:
261 abort();
262 }
263
264 *loglevel_value = log_level_rule->level;
265}
266
267LTTNG_HIDDEN
268unsigned long lttng_log_level_rule_hash(
269 const struct lttng_log_level_rule *log_level_rule)
270{
271 unsigned long hash;
272 enum lttng_log_level_rule_status llr_status;
273 int log_level_value;
274 enum lttng_log_level_rule_type type;
275
276 assert(log_level_rule);
277
278 type = lttng_log_level_rule_get_type(log_level_rule);
279
280 switch (type) {
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);
284 break;
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);
288 break;
289 default:
290 abort();
291 break;
292 }
293
294 assert(llr_status == LTTNG_LOG_LEVEL_RULE_STATUS_OK);
295
296 hash = hash_key_ulong((void *) (unsigned long) type, lttng_ht_seed);
297
298 hash ^= hash_key_ulong((void *) (unsigned long) log_level_value,
299 lttng_ht_seed);
300
301 return hash;
302}
This page took 0.033148 seconds and 4 git commands to generate.