]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl-filter: several trivial cleanups
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 21 Mar 2024 18:08:54 +0000 (03:08 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 26 Mar 2024 14:51:47 +0000 (23:51 +0900)
- declare iterator in loop,
- use journal_add_match_pair() and journal_add_matchf(),
- log failures in the caller.

src/journal/journalctl-filter.c
src/journal/journalctl.c

index 37fa00f2b67dc8a671d5c54d2437b1f2e1cbd25b..1223eeec02f880eadaa869ad4646633aa987a985 100644 (file)
@@ -69,16 +69,11 @@ static int add_dmesg(sd_journal *j) {
         if (!arg_dmesg)
                 return 0;
 
-        r = sd_journal_add_match(j, "_TRANSPORT=kernel",
-                                 STRLEN("_TRANSPORT=kernel"));
+        r = sd_journal_add_match(j, "_TRANSPORT=kernel", 0);
         if (r < 0)
-                return log_error_errno(r, "Failed to add match: %m");
-
-        r = sd_journal_add_conjunction(j);
-        if (r < 0)
-                return log_error_errno(r, "Failed to add conjunction: %m");
+                return r;
 
-        return 0;
+        return sd_journal_add_conjunction(j);
 }
 
 static int get_possible_units(
@@ -255,13 +250,11 @@ static int add_syslog_identifier(sd_journal *j) {
 
         assert(j);
 
-        STRV_FOREACH(i, arg_syslog_identifier) {
-                _cleanup_free_ char *u = NULL;
+        if (strv_isempty(arg_syslog_identifier))
+                return 0;
 
-                u = strjoin("SYSLOG_IDENTIFIER=", *i);
-                if (!u)
-                        return -ENOMEM;
-                r = sd_journal_add_match(j, u, 0);
+        STRV_FOREACH(i, arg_syslog_identifier) {
+                r = journal_add_match_pair(j, "SYSLOG_IDENTIFIER", *i);
                 if (r < 0)
                         return r;
                 r = sd_journal_add_disjunction(j);
@@ -269,11 +262,7 @@ static int add_syslog_identifier(sd_journal *j) {
                         return r;
         }
 
-        r = sd_journal_add_conjunction(j);
-        if (r < 0)
-                return r;
-
-        return 0;
+        return sd_journal_add_conjunction(j);
 }
 
 static int add_exclude_identifier(sd_journal *j) {
@@ -290,42 +279,36 @@ static int add_exclude_identifier(sd_journal *j) {
 }
 
 static int add_priorities(sd_journal *j) {
-        char match[] = "PRIORITY=0";
-        int i, r;
+        int r;
 
         assert(j);
 
-        if (arg_priorities == 0xFF)
+        if (arg_priorities == 0)
                 return 0;
 
-        for (i = LOG_EMERG; i <= LOG_DEBUG; i++)
+        for (int i = LOG_EMERG; i <= LOG_DEBUG; i++)
                 if (arg_priorities & (1 << i)) {
-                        match[sizeof(match)-2] = '0' + i;
-
-                        r = sd_journal_add_match(j, match, strlen(match));
+                        r = journal_add_matchf(j, "PRIORITY=%d", i);
                         if (r < 0)
-                                return log_error_errno(r, "Failed to add match: %m");
+                                return r;
                 }
 
-        r = sd_journal_add_conjunction(j);
-        if (r < 0)
-                return log_error_errno(r, "Failed to add conjunction: %m");
-
-        return 0;
+        return sd_journal_add_conjunction(j);
 }
 
 static int add_facilities(sd_journal *j) {
-        void *p;
         int r;
 
-        SET_FOREACH(p, arg_facilities) {
-                char match[STRLEN("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)];
+        assert(j);
 
-                xsprintf(match, "SYSLOG_FACILITY=%d", PTR_TO_INT(p));
+        if (set_isempty(arg_facilities))
+                return 0;
 
-                r = sd_journal_add_match(j, match, strlen(match));
+        void *p;
+        SET_FOREACH(p, arg_facilities) {
+                r = journal_add_matchf(j, "SYSLOG_FACILITY=%d", PTR_TO_INT(p));
                 if (r < 0)
-                        return log_error_errno(r, "Failed to add match: %m");
+                        return r;
         }
 
         return 0;
@@ -496,7 +479,7 @@ int add_filters(sd_journal *j, char **matches) {
 
         r = add_dmesg(j);
         if (r < 0)
-                return r;
+                return log_error_errno(r, "Failed to add filter for dmesg: %m");
 
         r = add_units(j);
         if (r < 0)
@@ -512,11 +495,11 @@ int add_filters(sd_journal *j, char **matches) {
 
         r = add_priorities(j);
         if (r < 0)
-                return r;
+                return log_error_errno(r, "Failed to add filter for priorities: %m");
 
         r = add_facilities(j);
         if (r < 0)
-                return r;
+                return log_error_errno(r, "Failed to add filter for facilities: %m");
 
         r = add_matches(j, matches);
         if (r < 0)
index bbc6aa6dacb9660feeb1af73a1009642fcba0f97..0008e8a7557420bf5b0c520c991e10f75c9db96a 100644 (file)
@@ -57,7 +57,7 @@ bool arg_show_cursor = false;
 const char *arg_directory = NULL;
 char **arg_file = NULL;
 bool arg_file_stdin = false;
-int arg_priorities = 0xFF;
+int arg_priorities = 0;
 Set *arg_facilities = NULL;
 char *arg_verify_key = NULL;
 #if HAVE_GCRYPT