From: Yu Watanabe Date: Thu, 21 Mar 2024 18:08:54 +0000 (+0900) Subject: journalctl-filter: several trivial cleanups X-Git-Tag: v256-rc1~382^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=22f2b5566d4432e8ab36c0120e46cc22e46c7526;p=thirdparty%2Fsystemd.git journalctl-filter: several trivial cleanups - declare iterator in loop, - use journal_add_match_pair() and journal_add_matchf(), - log failures in the caller. --- diff --git a/src/journal/journalctl-filter.c b/src/journal/journalctl-filter.c index 37fa00f2b67..1223eeec02f 100644 --- a/src/journal/journalctl-filter.c +++ b/src/journal/journalctl-filter.c @@ -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) diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index bbc6aa6dacb..0008e8a7557 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -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