From: Lennart Poettering Date: Fri, 27 Jul 2012 08:31:33 +0000 (+0200) Subject: journalctl: add --priority= switch for filtering by priority X-Git-Tag: v188~63 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=941e990db1f2682abaa2966b1c48602901d0c599 journalctl: add --priority= switch for filtering by priority --- diff --git a/TODO b/TODO index 3d075f17e..2df612689 100644 --- a/TODO +++ b/TODO @@ -39,6 +39,8 @@ Bugfixes: Features: +* logind: different policy actions for idle, suspend, shutdown blockers: allow idle blockers by default, don't allow suspend blockers by default + * install README to /etc/rc.d/init.d (if support for that is enabled) helping people who use "ls" there to figure out which services exist. * logind: ignore inactive login screens when checking whether power key should be handled diff --git a/man/journalctl.xml b/man/journalctl.xml index 922ff4fe0..cd17ca6ae 100644 --- a/man/journalctl.xml +++ b/man/journalctl.xml @@ -270,6 +270,38 @@ accessed. + + + + + Filter output by + message priorities or priority + ranges. Takes either a single numeric + or textual log level (i.e. between + 0/emerg and + 7/debug), or a + range of numeric/text log levels in + the form FROM..TO. The log levels are + the usual syslog log levels as + documented in + syslog3, + i.e. emerg (0), + alert (1), + crit (2), + err (3), + warning (4), + notice (5), + info (6), + debug (7). If a + single log level is specified all + messages with this log levels or a + lower (hence more important) log level + are shown. If a range is specified all + messages within the range are shown, + including both the start and the end + value of the range. + + diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index abcfabe75..10959423f 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -54,6 +54,7 @@ static bool arg_quiet = false; static bool arg_local = false; static bool arg_this_boot = false; static const char *arg_directory = NULL; +static int arg_priorities = 0xFF; static int help(void) { @@ -72,6 +73,7 @@ static int help(void) { " -l --local Only local entries\n" " -b --this-boot Show data only from current boot\n" " -D --directory=PATH Show journal files from directory\n" + " -p --priority=RANGE Show only messages within the specified priority range\n" " --header Show journal header information\n" " --new-id128 Generate a new 128 Bit id\n", program_invocation_short_name); @@ -104,6 +106,7 @@ static int parse_argv(int argc, char *argv[]) { { "this-boot", no_argument, NULL, 'b' }, { "directory", required_argument, NULL, 'D' }, { "header", no_argument, NULL, ARG_HEADER }, + { "priority", no_argument, NULL, 'p' }, { NULL, 0, NULL, 0 } }; @@ -112,7 +115,7 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); - while ((c = getopt_long(argc, argv, "hfo:an:qlbD:", options, NULL)) >= 0) { + while ((c = getopt_long(argc, argv, "hfo:an:qlbD:p:", options, NULL)) >= 0) { switch (c) { @@ -183,6 +186,56 @@ static int parse_argv(int argc, char *argv[]) { arg_print_header = true; break; + case 'p': { + const char *dots; + + dots = strstr(optarg, ".."); + if (dots) { + char *a; + int from, to, i; + + /* a range */ + a = strndup(optarg, dots - optarg); + if (!a) + return log_oom(); + + from = log_level_from_string(a); + to = log_level_from_string(dots + 2); + free(a); + + if (from < 0 || to < 0) { + log_error("Failed to parse log level range %s", optarg); + return -EINVAL; + } + + arg_priorities = 0; + + if (from < to) { + for (i = from; i <= to; i++) + arg_priorities |= 1 << i; + } else { + for (i = to; i <= from; i++) + arg_priorities |= 1 << i; + } + + } else { + int p, i; + + p = log_level_from_string(optarg); + if (p < 0) { + log_error("Unknown log level %s", optarg); + return -EINVAL; + } + + arg_priorities = 0; + + for (i = 0; i <= p; i++) + arg_priorities |= 1 << i; + } + + break; + } + case '?': return -EINVAL; @@ -300,6 +353,8 @@ static int add_this_boot(sd_journal *j) { sd_id128_t boot_id; int r; + assert(j); + if (!arg_this_boot) return 0; @@ -319,6 +374,31 @@ static int add_this_boot(sd_journal *j) { return 0; } +static int add_priorities(sd_journal *j) { + char match[] = "PRIORITY=0"; + int i, r; + + assert(j); + + if (arg_priorities == 0xFF) + return 0; + + for (i = LOG_EMERG; i <= LOG_DEBUG; i++) + if (arg_priorities & (1 << i)) { + match[sizeof(match)-2] = '0' + i; + + log_info("adding match %s", match); + + r = sd_journal_add_match(j, match, strlen(match)); + if (r < 0) { + log_error("Failed to add match: %s", strerror(-r)); + return r; + } + } + + return 0; +} + int main(int argc, char *argv[]) { int r; sd_journal *j = NULL; @@ -369,6 +449,10 @@ int main(int argc, char *argv[]) { if (r < 0) goto finish; + r = add_priorities(j); + if (r < 0) + goto finish; + if (!arg_quiet) { usec_t start, end; char start_buf[FORMAT_TIMESTAMP_MAX], end_buf[FORMAT_TIMESTAMP_MAX];