summaryrefslogtreecommitdiffstats
path: root/collectors/log2journal/log2journal-pattern.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-09 13:19:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-09 13:20:02 +0000
commit58daab21cd043e1dc37024a7f99b396788372918 (patch)
tree96771e43bb69f7c1c2b0b4f7374cb74d7866d0cb /collectors/log2journal/log2journal-pattern.c
parentReleasing debian version 1.43.2-1. (diff)
downloadnetdata-58daab21cd043e1dc37024a7f99b396788372918.tar.xz
netdata-58daab21cd043e1dc37024a7f99b396788372918.zip
Merging upstream version 1.44.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'collectors/log2journal/log2journal-pattern.c')
-rw-r--r--collectors/log2journal/log2journal-pattern.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/collectors/log2journal/log2journal-pattern.c b/collectors/log2journal/log2journal-pattern.c
new file mode 100644
index 000000000..4b7e9026b
--- /dev/null
+++ b/collectors/log2journal/log2journal-pattern.c
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#include "log2journal.h"
+
+void search_pattern_cleanup(SEARCH_PATTERN *sp) {
+ if(sp->pattern) {
+ freez((void *)sp->pattern);
+ sp->pattern = NULL;
+ }
+
+ if(sp->re) {
+ pcre2_code_free(sp->re);
+ sp->re = NULL;
+ }
+
+ if(sp->match_data) {
+ pcre2_match_data_free(sp->match_data);
+ sp->match_data = NULL;
+ }
+
+ txt_cleanup(&sp->error);
+}
+
+static void pcre2_error_message(SEARCH_PATTERN *sp, int rc, int pos) {
+ char msg[1024];
+ pcre2_get_error_in_buffer(msg, sizeof(msg), rc, pos);
+ txt_replace(&sp->error, msg, strlen(msg));
+}
+
+static inline bool compile_pcre2(SEARCH_PATTERN *sp) {
+ int error_number;
+ PCRE2_SIZE error_offset;
+ PCRE2_SPTR pattern_ptr = (PCRE2_SPTR)sp->pattern;
+
+ sp->re = pcre2_compile(pattern_ptr, PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, NULL);
+ if (!sp->re) {
+ pcre2_error_message(sp, error_number, (int) error_offset);
+ return false;
+ }
+
+ return true;
+}
+
+bool search_pattern_set(SEARCH_PATTERN *sp, const char *search_pattern, size_t search_pattern_len) {
+ search_pattern_cleanup(sp);
+
+ sp->pattern = strndupz(search_pattern, search_pattern_len);
+ if (!compile_pcre2(sp))
+ return false;
+
+ sp->match_data = pcre2_match_data_create_from_pattern(sp->re, NULL);
+
+ return true;
+}