X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fjournal%2Fjournalctl.c;h=10959423f695d377426c09f33637c8b9a595482f;hb=de2c390731e563db0ee383bfd0073fdbef643ca2;hp=016750c20153cbaca13462d68526415ed051fdba;hpb=25277cd7fbd77e4c8b20572570aa77c7da9abcc2;p=elogind.git diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 016750c20..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; @@ -198,6 +251,21 @@ static int parse_argv(int argc, char *argv[]) { return 1; } +static bool on_tty(void) { + static int t = -1; + + /* Note that this is invoked relatively early, before we start + * the pager. That means the value we return reflects whether + * we originally were started on a tty, not if we currently + * are. But this is intended, since we want colour and so on + * when run in our own pager. */ + + if (_unlikely_(t < 0)) + t = isatty(STDOUT_FILENO) > 0; + + return t; +} + static int generate_new_id128(void) { sd_id128_t id; int r; @@ -256,8 +324,7 @@ static int add_matches(sd_journal *j, char **args) { t = strappend("_EXE=", path); if (!t) { free(p); - log_error("Out of memory"); - return -ENOMEM; + return log_oom(); } r = sd_journal_add_match(j, t, 0); @@ -286,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; @@ -305,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; @@ -312,6 +406,7 @@ int main(int argc, char *argv[]) { bool need_seek = false; sd_id128_t previous_boot_id; bool previous_boot_id_valid = false; + bool have_pager; log_parse_environment(); log_open(); @@ -354,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]; @@ -397,10 +496,8 @@ int main(int argc, char *argv[]) { goto finish; } - if (!arg_no_pager && !arg_follow) { - columns(); - pager_open(); - } + on_tty(); + have_pager = !arg_no_pager && !arg_follow && pager_open(); if (arg_output == OUTPUT_JSON) { fputc('[', stdout); @@ -410,6 +507,10 @@ int main(int argc, char *argv[]) { for (;;) { for (;;) { sd_id128_t boot_id; + int flags = + arg_show_all * OUTPUT_SHOW_ALL | + have_pager * OUTPUT_FULL_WIDTH | + on_tty() * OUTPUT_COLOR; if (need_seek) { r = sd_journal_next(j); @@ -434,8 +535,7 @@ int main(int argc, char *argv[]) { line ++; - r = output_journal(j, arg_output, line, 0, - arg_show_all ? OUTPUT_SHOW_ALL : 0); + r = output_journal(j, arg_output, line, 0, flags); if (r < 0) goto finish;