Commit 67e04a48 authored by Zbigniew Jędrzejewski-Szmek's avatar Zbigniew Jędrzejewski-Szmek
Browse files

journalctl: allow --lines=0 i.e. only new

Makes it easier to watch just for new entries. Once scenario
is where the user starts 'journalctl -qfn0' to watch for changes
during some operation.
parent 390b5e55
...@@ -60,7 +60,7 @@ static bool arg_follow = false; ...@@ -60,7 +60,7 @@ static bool arg_follow = false;
static bool arg_full = false; static bool arg_full = false;
static bool arg_all = false; static bool arg_all = false;
static bool arg_no_pager = false; static bool arg_no_pager = false;
static unsigned arg_lines = 0; static int arg_lines = -1;
static bool arg_no_tail = false; static bool arg_no_tail = false;
static bool arg_quiet = false; static bool arg_quiet = false;
static bool arg_merge = false; static bool arg_merge = false;
...@@ -239,8 +239,8 @@ static int parse_argv(int argc, char *argv[]) { ...@@ -239,8 +239,8 @@ static int parse_argv(int argc, char *argv[]) {
case 'n': case 'n':
if (optarg) { if (optarg) {
r = safe_atou(optarg, &arg_lines); r = safe_atoi(optarg, &arg_lines);
if (r < 0 || arg_lines <= 0) { if (r < 0 || arg_lines < 0) {
log_error("Failed to parse lines '%s'", optarg); log_error("Failed to parse lines '%s'", optarg);
return -EINVAL; return -EINVAL;
} }
...@@ -413,7 +413,7 @@ static int parse_argv(int argc, char *argv[]) { ...@@ -413,7 +413,7 @@ static int parse_argv(int argc, char *argv[]) {
} }
} }
if (arg_follow && !arg_no_tail && arg_lines <= 0) if (arg_follow && !arg_no_tail && arg_lines < 0)
arg_lines = 10; arg_lines = 10;
if (arg_since_set && arg_until_set && arg_since_set > arg_until_set) { if (arg_since_set && arg_until_set && arg_since_set > arg_until_set) {
...@@ -849,8 +849,8 @@ int main(int argc, char *argv[]) { ...@@ -849,8 +849,8 @@ int main(int argc, char *argv[]) {
sd_journal *j = NULL; sd_journal *j = NULL;
bool need_seek = false; bool need_seek = false;
sd_id128_t previous_boot_id; sd_id128_t previous_boot_id;
bool previous_boot_id_valid = false; bool previous_boot_id_valid = false, first_line = true;
unsigned n_shown = 0; int n_shown = 0;
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
log_parse_environment(); log_parse_environment();
...@@ -937,6 +937,11 @@ int main(int argc, char *argv[]) { ...@@ -937,6 +937,11 @@ int main(int argc, char *argv[]) {
if (r < 0) if (r < 0)
goto finish; goto finish;
/* Opening the fd now means the first sd_journal_wait() will actually wait */
r = sd_journal_get_fd(j);
if (r < 0)
goto finish;
if (arg_field) { if (arg_field) {
const void *data; const void *data;
size_t size; size_t size;
...@@ -950,7 +955,7 @@ int main(int argc, char *argv[]) { ...@@ -950,7 +955,7 @@ int main(int argc, char *argv[]) {
SD_JOURNAL_FOREACH_UNIQUE(j, data, size) { SD_JOURNAL_FOREACH_UNIQUE(j, data, size) {
const void *eq; const void *eq;
if (arg_lines > 0 && n_shown >= arg_lines) if (arg_lines >= 0 && n_shown >= arg_lines)
break; break;
eq = memchr(data, '=', size); eq = memchr(data, '=', size);
...@@ -983,7 +988,7 @@ int main(int argc, char *argv[]) { ...@@ -983,7 +988,7 @@ int main(int argc, char *argv[]) {
} }
r = sd_journal_next(j); r = sd_journal_next(j);
} else if (arg_lines > 0) { } else if (arg_lines >= 0) {
r = sd_journal_seek_tail(j); r = sd_journal_seek_tail(j);
if (r < 0) { if (r < 0) {
log_error("Failed to seek to tail: %s", strerror(-r)); log_error("Failed to seek to tail: %s", strerror(-r));
...@@ -1032,7 +1037,7 @@ int main(int argc, char *argv[]) { ...@@ -1032,7 +1037,7 @@ int main(int argc, char *argv[]) {
} }
for (;;) { for (;;) {
while (arg_lines == 0 || arg_follow || n_shown < arg_lines) { while (arg_lines < 0 || n_shown < arg_lines || (arg_follow && !first_line)) {
int flags; int flags;
if (need_seek) { if (need_seek) {
...@@ -1092,6 +1097,8 @@ int main(int argc, char *argv[]) { ...@@ -1092,6 +1097,8 @@ int main(int argc, char *argv[]) {
log_error("Couldn't wait for journal event: %s", strerror(-r)); log_error("Couldn't wait for journal event: %s", strerror(-r));
goto finish; goto finish;
} }
first_line = false;
} }
finish: finish:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment