journalctl: add --priority= switch for filtering by priority

This commit is contained in:
Lennart Poettering 2012-07-27 10:31:33 +02:00
parent 0d7e32fa0a
commit 941e990db1
3 changed files with 119 additions and 1 deletions

2
TODO
View File

@ -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

View File

@ -270,6 +270,38 @@
accessed.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-p</option></term>
<term><option>--priority=</option></term>
<listitem><para>Filter output by
message priorities or priority
ranges. Takes either a single numeric
or textual log level (i.e. between
0/<literal>emerg</literal> and
7/<literal>debug</literal>), 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
<citerefentry><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
i.e. <literal>emerg</literal> (0),
<literal>alert</literal> (1),
<literal>crit</literal> (2),
<literal>err</literal> (3),
<literal>warning</literal> (4),
<literal>notice</literal> (5),
<literal>info</literal> (6),
<literal>debug</literal> (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.</para></listitem>
</varlistentry>
</variablelist>
</refsect1>

View File

@ -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];