]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-journal: introduce two helper functions for adding filter
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 21 Mar 2024 19:43:31 +0000 (04:43 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 22 Mar 2024 08:54:13 +0000 (17:54 +0900)
src/libsystemd/sd-journal/journal-internal.h
src/libsystemd/sd-journal/sd-journal.c

index cddc2316af495ed7a4adf51bd0af12d5224af093..b95080c527a68529b5822adef75b549286b80b68 100644 (file)
@@ -140,6 +140,9 @@ char *journal_make_match_string(sd_journal *j);
 void journal_print_header(sd_journal *j);
 int journal_get_directories(sd_journal *j, char ***ret);
 
+int journal_add_match_pair(sd_journal *j, const char *field, const char *value);
+int journal_add_matchf(sd_journal *j, const char *format, ...) _printf_(2, 3);
+
 #define JOURNAL_FOREACH_DATA_RETVAL(j, data, l, retval)                     \
         for (sd_journal_restart_data(j); ((retval) = sd_journal_enumerate_data((j), &(data), &(l))) > 0; )
 
index ef57f156ac5910378b877302106b8b9356416de7..383f3e5d572fcc53ec116ed27494204637f7a7ed 100644 (file)
@@ -325,6 +325,37 @@ fail:
         return -ENOMEM;
 }
 
+int journal_add_match_pair(sd_journal *j, const char *field, const char *value) {
+        _cleanup_free_ char *s = NULL;
+
+        assert(j);
+        assert(field);
+        assert(value);
+
+        s = strjoin(field, "=", value);
+        if (!s)
+                return -ENOMEM;
+
+        return sd_journal_add_match(j, s, 0);
+}
+
+int journal_add_matchf(sd_journal *j, const char *format, ...) {
+        _cleanup_free_ char *s = NULL;
+        va_list ap;
+        int r;
+
+        assert(j);
+        assert(format);
+
+        va_start(ap, format);
+        r = vasprintf(&s, format, ap);
+        va_end(ap);
+        if (r < 0)
+                return -ENOMEM;
+
+        return sd_journal_add_match(j, s, 0);
+}
+
 _public_ int sd_journal_add_conjunction(sd_journal *j) {
         assert_return(j, -EINVAL);
         assert_return(!journal_origin_changed(j), -ECHILD);