Introduce lttng_event_rule_log4j_logging
[lttng-tools.git] / src / common / event-rule / log4j-logging.c
1 /*
2 * Copyright (C) 2019 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/credentials.h>
10 #include <common/error.h>
11 #include <common/macros.h>
12 #include <common/optional.h>
13 #include <common/payload.h>
14 #include <common/payload-view.h>
15 #include <common/runas.h>
16 #include <common/hashtable/hashtable.h>
17 #include <common/hashtable/utils.h>
18 #include <common/string-utils/string-utils.h>
19 #include <lttng/event-rule/event-rule-internal.h>
20 #include <lttng/event-rule/log4j-logging-internal.h>
21 #include <lttng/log-level-rule.h>
22 #include <lttng/event.h>
23
24 #define IS_LOG4J_LOGGING_EVENT_RULE(rule) \
25 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING)
26
27 static void lttng_event_rule_log4j_logging_destroy(struct lttng_event_rule *rule)
28 {
29 struct lttng_event_rule_log4j_logging *log4j_logging;
30
31 if (rule == NULL) {
32 return;
33 }
34
35 log4j_logging = container_of(
36 rule, struct lttng_event_rule_log4j_logging, parent);
37
38 lttng_log_level_rule_destroy(log4j_logging->log_level_rule);
39 free(log4j_logging->pattern);
40 free(log4j_logging->filter_expression);
41 free(log4j_logging->internal_filter.filter);
42 free(log4j_logging->internal_filter.bytecode);
43 free(log4j_logging);
44 }
45
46 static bool lttng_event_rule_log4j_logging_validate(
47 const struct lttng_event_rule *rule)
48 {
49 bool valid = false;
50 struct lttng_event_rule_log4j_logging *log4j_logging;
51
52 if (!rule) {
53 goto end;
54 }
55
56 log4j_logging = container_of(
57 rule, struct lttng_event_rule_log4j_logging, parent);
58
59 /* Required field. */
60 if (!log4j_logging->pattern) {
61 ERR("Invalid log4j_logging event rule: a pattern must be set.");
62 goto end;
63 }
64
65 valid = true;
66 end:
67 return valid;
68 }
69
70 static int lttng_event_rule_log4j_logging_serialize(
71 const struct lttng_event_rule *rule,
72 struct lttng_payload *payload)
73 {
74 int ret;
75 size_t pattern_len, filter_expression_len, header_offset;
76 size_t size_before_log_level_rule;
77 struct lttng_event_rule_log4j_logging *log4j_logging;
78 struct lttng_event_rule_log4j_logging_comm log4j_logging_comm;
79 struct lttng_event_rule_log4j_logging_comm *header;
80
81 if (!rule || !IS_LOG4J_LOGGING_EVENT_RULE(rule)) {
82 ret = -1;
83 goto end;
84 }
85
86 header_offset = payload->buffer.size;
87
88 DBG("Serializing log4j_logging event rule.");
89 log4j_logging = container_of(
90 rule, struct lttng_event_rule_log4j_logging, parent);
91
92 pattern_len = strlen(log4j_logging->pattern) + 1;
93
94 if (log4j_logging->filter_expression != NULL) {
95 filter_expression_len =
96 strlen(log4j_logging->filter_expression) + 1;
97 } else {
98 filter_expression_len = 0;
99 }
100
101 log4j_logging_comm.pattern_len = pattern_len;
102 log4j_logging_comm.filter_expression_len = filter_expression_len;
103
104 ret = lttng_dynamic_buffer_append(&payload->buffer, &log4j_logging_comm,
105 sizeof(log4j_logging_comm));
106 if (ret) {
107 goto end;
108 }
109
110 ret = lttng_dynamic_buffer_append(
111 &payload->buffer, log4j_logging->pattern, pattern_len);
112 if (ret) {
113 goto end;
114 }
115
116 ret = lttng_dynamic_buffer_append(&payload->buffer, log4j_logging->filter_expression,
117 filter_expression_len);
118 if (ret) {
119 goto end;
120 }
121
122 size_before_log_level_rule = payload->buffer.size;
123
124 ret = lttng_log_level_rule_serialize(log4j_logging->log_level_rule, payload);
125 if (ret < 0) {
126 goto end;
127 }
128
129 header = (typeof(header)) ((char *) payload->buffer.data + header_offset);
130 header->log_level_rule_len =
131 payload->buffer.size - size_before_log_level_rule;
132
133 end:
134 return ret;
135 }
136
137 static bool lttng_event_rule_log4j_logging_is_equal(
138 const struct lttng_event_rule *_a,
139 const struct lttng_event_rule *_b)
140 {
141 bool is_equal = false;
142 struct lttng_event_rule_log4j_logging *a, *b;
143
144 a = container_of(_a, struct lttng_event_rule_log4j_logging, parent);
145 b = container_of(_b, struct lttng_event_rule_log4j_logging, parent);
146
147 /* Quick checks. */
148
149 if (!!a->filter_expression != !!b->filter_expression) {
150 goto end;
151 }
152
153 /* Long check. */
154 assert(a->pattern);
155 assert(b->pattern);
156 if (strcmp(a->pattern, b->pattern)) {
157 goto end;
158 }
159
160 if (a->filter_expression && b->filter_expression) {
161 if (strcmp(a->filter_expression, b->filter_expression)) {
162 goto end;
163 }
164 } else if (!!a->filter_expression != !!b->filter_expression) {
165 /* One is set; not the other. */
166 goto end;
167 }
168
169 if (!lttng_log_level_rule_is_equal(
170 a->log_level_rule, b->log_level_rule)) {
171 goto end;
172 }
173
174 is_equal = true;
175 end:
176 return is_equal;
177 }
178
179 /*
180 * On success ret is 0;
181 *
182 * On error ret is negative.
183 *
184 * An event with NO loglevel and the name is * will return NULL.
185 */
186 static int generate_agent_filter(
187 const struct lttng_event_rule *rule, char **_agent_filter)
188 {
189 int err;
190 int ret = 0;
191 char *agent_filter = NULL;
192 const char *pattern;
193 const char *filter;
194 const struct lttng_log_level_rule *log_level_rule = NULL;
195 enum lttng_event_rule_status status;
196
197 assert(rule);
198 assert(_agent_filter);
199
200 status = lttng_event_rule_log4j_logging_get_name_pattern(rule, &pattern);
201 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
202 ret = -1;
203 goto end;
204 }
205
206 status = lttng_event_rule_log4j_logging_get_filter(rule, &filter);
207 if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
208 filter = NULL;
209 } else if (status != LTTNG_EVENT_RULE_STATUS_OK) {
210 ret = -1;
211 goto end;
212 }
213
214
215 /* Don't add filter for the '*' event. */
216 if (strcmp(pattern, "*") != 0) {
217 if (filter) {
218 err = asprintf(&agent_filter,
219 "(%s) && (logger_name == \"%s\")",
220 filter, pattern);
221 } else {
222 err = asprintf(&agent_filter, "logger_name == \"%s\"",
223 pattern);
224 }
225
226 if (err < 0) {
227 PERROR("Failed to format agent filter string");
228 ret = -1;
229 goto end;
230 }
231 }
232
233 status = lttng_event_rule_log4j_logging_get_log_level_rule(
234 rule, &log_level_rule);
235 if (status == LTTNG_EVENT_RULE_STATUS_OK) {
236 enum lttng_log_level_rule_status llr_status;
237 const char *op;
238 int level;
239
240 switch (lttng_log_level_rule_get_type(log_level_rule))
241 {
242 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
243 llr_status = lttng_log_level_rule_exactly_get_level(
244 log_level_rule, &level);
245 op = "==";
246 break;
247 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
248 llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level(
249 log_level_rule, &level);
250 op = ">=";
251 break;
252 default:
253 abort();
254 }
255
256 if (llr_status != LTTNG_LOG_LEVEL_RULE_STATUS_OK) {
257 ret = -1;
258 goto end;
259 }
260
261 if (filter || agent_filter) {
262 char *new_filter;
263
264 err = asprintf(&new_filter,
265 "(%s) && (int_loglevel %s %d)",
266 agent_filter ? agent_filter : filter,
267 op, level);
268 if (agent_filter) {
269 free(agent_filter);
270 }
271 agent_filter = new_filter;
272 } else {
273 err = asprintf(&agent_filter, "int_loglevel %s %d", op,
274 level);
275 }
276
277 if (err < 0) {
278 PERROR("Failed to format agent filter string");
279 ret = -1;
280 goto end;
281 }
282 }
283
284 *_agent_filter = agent_filter;
285 agent_filter = NULL;
286
287 end:
288 free(agent_filter);
289 return ret;
290 }
291
292 static enum lttng_error_code
293 lttng_event_rule_log4j_logging_generate_filter_bytecode(
294 struct lttng_event_rule *rule,
295 const struct lttng_credentials *creds)
296 {
297 int ret;
298 enum lttng_error_code ret_code;
299 struct lttng_event_rule_log4j_logging *log4j_logging;
300 enum lttng_event_rule_status status;
301 const char *filter;
302 struct lttng_bytecode *bytecode = NULL;
303 char *agent_filter;
304
305 assert(rule);
306
307 log4j_logging = container_of(
308 rule, struct lttng_event_rule_log4j_logging, parent);
309
310 status = lttng_event_rule_log4j_logging_get_filter(rule, &filter);
311 if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
312 filter = NULL;
313 } else if (status != LTTNG_EVENT_RULE_STATUS_OK) {
314 ret_code = LTTNG_ERR_FILTER_INVAL;
315 goto end;
316 }
317
318 if (filter && filter[0] == '\0') {
319 ret_code = LTTNG_ERR_FILTER_INVAL;
320 goto error;
321 }
322
323 ret = generate_agent_filter(rule, &agent_filter);
324 if (ret) {
325 ret_code = LTTNG_ERR_FILTER_INVAL;
326 goto error;
327 }
328
329 log4j_logging->internal_filter.filter = agent_filter;
330
331 if (log4j_logging->internal_filter.filter == NULL) {
332 ret_code = LTTNG_OK;
333 goto end;
334 }
335
336 ret = run_as_generate_filter_bytecode(
337 log4j_logging->internal_filter.filter, creds,
338 &bytecode);
339 if (ret) {
340 ret_code = LTTNG_ERR_FILTER_INVAL;
341 goto end;
342 }
343
344 log4j_logging->internal_filter.bytecode = bytecode;
345 bytecode = NULL;
346 ret_code = LTTNG_OK;
347
348 error:
349 end:
350 free(bytecode);
351 return ret_code;
352 }
353
354 static const char *lttng_event_rule_log4j_logging_get_internal_filter(
355 const struct lttng_event_rule *rule)
356 {
357 struct lttng_event_rule_log4j_logging *log4j_logging;
358
359 assert(rule);
360 log4j_logging = container_of(
361 rule, struct lttng_event_rule_log4j_logging, parent);
362 return log4j_logging->internal_filter.filter;
363 }
364
365 static const struct lttng_bytecode *
366 lttng_event_rule_log4j_logging_get_internal_filter_bytecode(
367 const struct lttng_event_rule *rule)
368 {
369 struct lttng_event_rule_log4j_logging *log4j_logging;
370
371 assert(rule);
372 log4j_logging = container_of(
373 rule, struct lttng_event_rule_log4j_logging, parent);
374 return log4j_logging->internal_filter.bytecode;
375 }
376
377 static enum lttng_event_rule_generate_exclusions_status
378 lttng_event_rule_log4j_logging_generate_exclusions(
379 const struct lttng_event_rule *rule,
380 struct lttng_event_exclusion **_exclusions)
381 {
382 /* Unsupported. */
383 *_exclusions = NULL;
384 return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE;
385 }
386
387 static unsigned long lttng_event_rule_log4j_logging_hash(
388 const struct lttng_event_rule *rule)
389 {
390 unsigned long hash;
391 struct lttng_event_rule_log4j_logging *tp_rule =
392 container_of(rule, typeof(*tp_rule), parent);
393
394 hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING,
395 lttng_ht_seed);
396 hash ^= hash_key_str(tp_rule->pattern, lttng_ht_seed);
397
398 if (tp_rule->filter_expression) {
399 hash ^= hash_key_str(tp_rule->filter_expression, lttng_ht_seed);
400 }
401
402 if (tp_rule->log_level_rule) {
403 hash ^= lttng_log_level_rule_hash(tp_rule->log_level_rule);
404 }
405
406 return hash;
407 }
408
409 static struct lttng_event *lttng_event_rule_log4j_logging_generate_lttng_event(
410 const struct lttng_event_rule *rule)
411 {
412 int ret;
413 const struct lttng_event_rule_log4j_logging *log4j_logging;
414 struct lttng_event *local_event = NULL;
415 struct lttng_event *event = NULL;
416 enum lttng_loglevel_type loglevel_type;
417 int loglevel_value = 0;
418 enum lttng_event_rule_status status;
419 const struct lttng_log_level_rule *log_level_rule;
420
421 log4j_logging = container_of(
422 rule, const struct lttng_event_rule_log4j_logging, parent);
423
424 local_event = zmalloc(sizeof(*local_event));
425 if (!local_event) {
426 goto error;
427 }
428
429 local_event->type = LTTNG_EVENT_TRACEPOINT;
430 ret = lttng_strncpy(local_event->name, log4j_logging->pattern,
431 sizeof(local_event->name));
432 if (ret) {
433 ERR("Truncation occurred when copying event rule pattern to `lttng_event` structure: pattern = '%s'",
434 log4j_logging->pattern);
435 goto error;
436 }
437
438
439 /* Map the log level rule to an equivalent lttng_loglevel. */
440 status = lttng_event_rule_log4j_logging_get_log_level_rule(
441 rule, &log_level_rule);
442 if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
443 loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
444 loglevel_value = 0;
445 } else if (status == LTTNG_EVENT_RULE_STATUS_OK) {
446 enum lttng_log_level_rule_status llr_status;
447
448 switch (lttng_log_level_rule_get_type(log_level_rule)) {
449 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
450 llr_status = lttng_log_level_rule_exactly_get_level(
451 log_level_rule, &loglevel_value);
452 loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
453 break;
454 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
455 llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level(
456 log_level_rule, &loglevel_value);
457 loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
458 break;
459 default:
460 abort();
461 break;
462 }
463
464 if (llr_status != LTTNG_LOG_LEVEL_RULE_STATUS_OK) {
465 goto error;
466 }
467 } else {
468 goto error;
469 }
470
471 local_event->loglevel_type = loglevel_type;
472 local_event->loglevel = loglevel_value;
473
474 event = local_event;
475 local_event = NULL;
476 error:
477 free(local_event);
478 return event;
479 }
480
481 struct lttng_event_rule *lttng_event_rule_log4j_logging_create(void)
482 {
483 struct lttng_event_rule *rule = NULL;
484 struct lttng_event_rule_log4j_logging *tp_rule;
485 enum lttng_event_rule_status status;
486
487 tp_rule = zmalloc(sizeof(struct lttng_event_rule_log4j_logging));
488 if (!tp_rule) {
489 goto end;
490 }
491
492 rule = &tp_rule->parent;
493 lttng_event_rule_init(&tp_rule->parent, LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING);
494 tp_rule->parent.validate = lttng_event_rule_log4j_logging_validate;
495 tp_rule->parent.serialize = lttng_event_rule_log4j_logging_serialize;
496 tp_rule->parent.equal = lttng_event_rule_log4j_logging_is_equal;
497 tp_rule->parent.destroy = lttng_event_rule_log4j_logging_destroy;
498 tp_rule->parent.generate_filter_bytecode =
499 lttng_event_rule_log4j_logging_generate_filter_bytecode;
500 tp_rule->parent.get_filter =
501 lttng_event_rule_log4j_logging_get_internal_filter;
502 tp_rule->parent.get_filter_bytecode =
503 lttng_event_rule_log4j_logging_get_internal_filter_bytecode;
504 tp_rule->parent.generate_exclusions =
505 lttng_event_rule_log4j_logging_generate_exclusions;
506 tp_rule->parent.hash = lttng_event_rule_log4j_logging_hash;
507 tp_rule->parent.generate_lttng_event =
508 lttng_event_rule_log4j_logging_generate_lttng_event;
509
510 tp_rule->log_level_rule = NULL;
511
512 /* Default pattern is '*'. */
513 status = lttng_event_rule_log4j_logging_set_name_pattern(rule, "*");
514 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
515 lttng_event_rule_destroy(rule);
516 rule = NULL;
517 }
518
519 end:
520 return rule;
521 }
522
523 LTTNG_HIDDEN
524 ssize_t lttng_event_rule_log4j_logging_create_from_payload(
525 struct lttng_payload_view *view,
526 struct lttng_event_rule **_event_rule)
527 {
528 ssize_t ret, offset = 0;
529 enum lttng_event_rule_status status;
530 const struct lttng_event_rule_log4j_logging_comm *log4j_logging_comm;
531 const char *pattern;
532 const char *filter_expression = NULL;
533 struct lttng_buffer_view current_buffer_view;
534 struct lttng_event_rule *rule = NULL;
535 struct lttng_log_level_rule *log_level_rule = NULL;
536
537 if (!_event_rule) {
538 ret = -1;
539 goto end;
540 }
541
542 current_buffer_view = lttng_buffer_view_from_view(
543 &view->buffer, offset, sizeof(*log4j_logging_comm));
544 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
545 ERR("Failed to initialize from malformed event rule log4j_logging: buffer too short to contain header.");
546 ret = -1;
547 goto end;
548 }
549
550 log4j_logging_comm = (typeof(log4j_logging_comm)) current_buffer_view.data;
551
552 rule = lttng_event_rule_log4j_logging_create();
553 if (!rule) {
554 ERR("Failed to create event rule log4j_logging.");
555 ret = -1;
556 goto end;
557 }
558
559 /* Skip to payload. */
560 offset += current_buffer_view.size;
561
562 /* Map the pattern. */
563 current_buffer_view = lttng_buffer_view_from_view(
564 &view->buffer, offset, log4j_logging_comm->pattern_len);
565
566 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
567 ret = -1;
568 goto end;
569 }
570
571 pattern = current_buffer_view.data;
572 if (!lttng_buffer_view_contains_string(&current_buffer_view, pattern,
573 log4j_logging_comm->pattern_len)) {
574 ret = -1;
575 goto end;
576 }
577
578 /* Skip after the pattern. */
579 offset += log4j_logging_comm->pattern_len;
580
581 if (!log4j_logging_comm->filter_expression_len) {
582 goto skip_filter_expression;
583 }
584
585 /* Map the filter_expression. */
586 current_buffer_view = lttng_buffer_view_from_view(&view->buffer, offset,
587 log4j_logging_comm->filter_expression_len);
588 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
589 ret = -1;
590 goto end;
591 }
592
593 filter_expression = current_buffer_view.data;
594 if (!lttng_buffer_view_contains_string(&current_buffer_view,
595 filter_expression,
596 log4j_logging_comm->filter_expression_len)) {
597 ret = -1;
598 goto end;
599 }
600
601 /* Skip after the pattern. */
602 offset += log4j_logging_comm->filter_expression_len;
603
604 skip_filter_expression:
605 if (!log4j_logging_comm->log_level_rule_len) {
606 goto skip_log_level_rule;
607 }
608
609 {
610 /* Map the log level rule. */
611 struct lttng_payload_view current_payload_view =
612 lttng_payload_view_from_view(view, offset,
613 log4j_logging_comm->log_level_rule_len);
614
615 ret = lttng_log_level_rule_create_from_payload(
616 &current_payload_view, &log_level_rule);
617 if (ret < 0) {
618 ret = -1;
619 goto end;
620 }
621
622 assert(ret == log4j_logging_comm->log_level_rule_len);
623 }
624
625 /* Skip after the log level rule. */
626 offset += log4j_logging_comm->log_level_rule_len;
627
628 skip_log_level_rule:
629
630 status = lttng_event_rule_log4j_logging_set_name_pattern(rule, pattern);
631 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
632 ERR("Failed to set event rule log4j_logging pattern.");
633 ret = -1;
634 goto end;
635 }
636
637 if (filter_expression) {
638 status = lttng_event_rule_log4j_logging_set_filter(
639 rule, filter_expression);
640 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
641 ERR("Failed to set event rule log4j_logging pattern.");
642 ret = -1;
643 goto end;
644 }
645 }
646
647 if (log_level_rule) {
648 status = lttng_event_rule_log4j_logging_set_log_level_rule(
649 rule, log_level_rule);
650 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
651 ERR("Failed to set event rule log4j_logging log level rule.");
652 ret = -1;
653 goto end;
654 }
655 }
656
657 *_event_rule = rule;
658 rule = NULL;
659 ret = offset;
660 end:
661 lttng_log_level_rule_destroy(log_level_rule);
662 lttng_event_rule_destroy(rule);
663 return ret;
664 }
665
666 enum lttng_event_rule_status lttng_event_rule_log4j_logging_set_name_pattern(
667 struct lttng_event_rule *rule, const char *pattern)
668 {
669 char *pattern_copy = NULL;
670 struct lttng_event_rule_log4j_logging *log4j_logging;
671 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
672
673 if (!rule || !IS_LOG4J_LOGGING_EVENT_RULE(rule) || !pattern ||
674 strlen(pattern) == 0) {
675 status = LTTNG_EVENT_RULE_STATUS_INVALID;
676 goto end;
677 }
678
679 log4j_logging = container_of(
680 rule, struct lttng_event_rule_log4j_logging, parent);
681 pattern_copy = strdup(pattern);
682 if (!pattern_copy) {
683 status = LTTNG_EVENT_RULE_STATUS_ERROR;
684 goto end;
685 }
686
687 /* Normalize the pattern. */
688 strutils_normalize_star_glob_pattern(pattern_copy);
689
690 free(log4j_logging->pattern);
691
692 log4j_logging->pattern = pattern_copy;
693 pattern_copy = NULL;
694 end:
695 return status;
696 }
697
698 enum lttng_event_rule_status lttng_event_rule_log4j_logging_get_name_pattern(
699 const struct lttng_event_rule *rule, const char **pattern)
700 {
701 struct lttng_event_rule_log4j_logging *log4j_logging;
702 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
703
704 if (!rule || !IS_LOG4J_LOGGING_EVENT_RULE(rule) || !pattern) {
705 status = LTTNG_EVENT_RULE_STATUS_INVALID;
706 goto end;
707 }
708
709 log4j_logging = container_of(
710 rule, struct lttng_event_rule_log4j_logging, parent);
711 if (!log4j_logging->pattern) {
712 status = LTTNG_EVENT_RULE_STATUS_UNSET;
713 goto end;
714 }
715
716 *pattern = log4j_logging->pattern;
717 end:
718 return status;
719 }
720
721 enum lttng_event_rule_status lttng_event_rule_log4j_logging_set_filter(
722 struct lttng_event_rule *rule, const char *expression)
723 {
724 char *expression_copy = NULL;
725 struct lttng_event_rule_log4j_logging *log4j_logging;
726 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
727
728 if (!rule || !IS_LOG4J_LOGGING_EVENT_RULE(rule) || !expression ||
729 strlen(expression) == 0) {
730 status = LTTNG_EVENT_RULE_STATUS_INVALID;
731 goto end;
732 }
733
734 log4j_logging = container_of(
735 rule, struct lttng_event_rule_log4j_logging, parent);
736 expression_copy = strdup(expression);
737 if (!expression_copy) {
738 PERROR("Failed to copy filter expression");
739 status = LTTNG_EVENT_RULE_STATUS_ERROR;
740 goto end;
741 }
742
743 if (log4j_logging->filter_expression) {
744 free(log4j_logging->filter_expression);
745 }
746
747 log4j_logging->filter_expression = expression_copy;
748 expression_copy = NULL;
749 end:
750 return status;
751 }
752
753 enum lttng_event_rule_status lttng_event_rule_log4j_logging_get_filter(
754 const struct lttng_event_rule *rule, const char **expression)
755 {
756 struct lttng_event_rule_log4j_logging *log4j_logging;
757 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
758
759 if (!rule || !IS_LOG4J_LOGGING_EVENT_RULE(rule) || !expression) {
760 status = LTTNG_EVENT_RULE_STATUS_INVALID;
761 goto end;
762 }
763
764 log4j_logging = container_of(
765 rule, struct lttng_event_rule_log4j_logging, parent);
766 if (!log4j_logging->filter_expression) {
767 status = LTTNG_EVENT_RULE_STATUS_UNSET;
768 goto end;
769 }
770
771 *expression = log4j_logging->filter_expression;
772 end:
773 return status;
774 }
775
776 static bool log_level_rule_valid(const struct lttng_log_level_rule *rule)
777 {
778 /*
779 * For both LOG4J custom log level are possible and can
780 * span the entire int32 range.
781 */
782 return true;
783 }
784
785 enum lttng_event_rule_status lttng_event_rule_log4j_logging_set_log_level_rule(
786 struct lttng_event_rule *rule,
787 const struct lttng_log_level_rule *log_level_rule)
788 {
789 struct lttng_event_rule_log4j_logging *log4j_logging;
790 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
791 struct lttng_log_level_rule *copy = NULL;
792
793 if (!rule || !IS_LOG4J_LOGGING_EVENT_RULE(rule)) {
794 status = LTTNG_EVENT_RULE_STATUS_INVALID;
795 goto end;
796 }
797
798 log4j_logging = container_of(
799 rule, struct lttng_event_rule_log4j_logging, parent);
800
801 if (!log_level_rule_valid(log_level_rule)) {
802 status = LTTNG_EVENT_RULE_STATUS_INVALID;
803 goto end;
804 }
805
806 copy = lttng_log_level_rule_copy(log_level_rule);
807 if (copy == NULL) {
808 status = LTTNG_EVENT_RULE_STATUS_ERROR;
809 goto end;
810 }
811
812 if (log4j_logging->log_level_rule) {
813 lttng_log_level_rule_destroy(log4j_logging->log_level_rule);
814 }
815
816 log4j_logging->log_level_rule = copy;
817
818 end:
819 return status;
820 }
821
822 enum lttng_event_rule_status lttng_event_rule_log4j_logging_get_log_level_rule(
823 const struct lttng_event_rule *rule,
824 const struct lttng_log_level_rule **log_level_rule
825 )
826 {
827 struct lttng_event_rule_log4j_logging *log4j_logging;
828 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
829
830 if (!rule || !IS_LOG4J_LOGGING_EVENT_RULE(rule) || !log_level_rule) {
831 status = LTTNG_EVENT_RULE_STATUS_INVALID;
832 goto end;
833 }
834
835 log4j_logging = container_of(
836 rule, struct lttng_event_rule_log4j_logging, parent);
837 if (log4j_logging->log_level_rule == NULL) {
838 status = LTTNG_EVENT_RULE_STATUS_UNSET;
839 goto end;
840 }
841
842 *log_level_rule = log4j_logging->log_level_rule;
843 end:
844 return status;
845 }
This page took 0.090278 seconds and 5 git commands to generate.