Merge pull request #26669 from YHNdnzj/journalctl-lines-since-until
journalctl: fix output when --lines is used with --since or --until
This commit is contained in:
+42
-13
@@ -2100,7 +2100,7 @@ static int wait_for_change(sd_journal *j, int poll_fd) {
|
||||
static int run(int argc, char *argv[]) {
|
||||
_cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
|
||||
_cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL;
|
||||
bool previous_boot_id_valid = false, first_line = true, ellipsized = false, need_seek = false;
|
||||
bool previous_boot_id_valid = false, first_line = true, ellipsized = false, need_seek = false, since_seeked = false;
|
||||
bool use_cursor = false, after_cursor = false;
|
||||
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
|
||||
sd_id128_t previous_boot_id = SD_ID128_NULL, previous_boot_id_output = SD_ID128_NULL;
|
||||
@@ -2475,19 +2475,21 @@ static int run(int argc, char *argv[]) {
|
||||
arg_lines = 0;
|
||||
}
|
||||
|
||||
} else if (arg_since_set && !arg_reverse) {
|
||||
r = sd_journal_seek_realtime_usec(j, arg_since);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to seek to date: %m");
|
||||
} else if (arg_until_set && (arg_reverse || arg_lines >= 0)) {
|
||||
/* If both --until and any of --reverse and --lines is specified, things get
|
||||
* a little tricky. We seek to the place of --until first. If only --reverse or
|
||||
* --reverse and --lines is specified, we search backwards and let the output
|
||||
* counter handle --lines for us. If only --lines is used, we just jump backwards
|
||||
* arg_lines and search afterwards from there. */
|
||||
|
||||
r = sd_journal_next(j);
|
||||
|
||||
} else if (arg_until_set && arg_reverse) {
|
||||
r = sd_journal_seek_realtime_usec(j, arg_until);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to seek to date: %m");
|
||||
|
||||
r = sd_journal_previous(j);
|
||||
if (arg_reverse)
|
||||
r = sd_journal_previous(j);
|
||||
else /* arg_lines >= 0 */
|
||||
r = sd_journal_previous_skip(j, arg_lines);
|
||||
|
||||
} else if (arg_reverse) {
|
||||
r = sd_journal_seek_tail(j);
|
||||
@@ -2503,6 +2505,19 @@ static int run(int argc, char *argv[]) {
|
||||
|
||||
r = sd_journal_previous_skip(j, arg_lines);
|
||||
|
||||
} else if (arg_since_set) {
|
||||
/* This is placed after arg_reverse and arg_lines. If --since is used without
|
||||
* both, we seek to the place of --since and search afterwards from there.
|
||||
* If used with --reverse or --lines, we seek to the tail first and check if
|
||||
* the entry is within the range of --since later. */
|
||||
|
||||
r = sd_journal_seek_realtime_usec(j, arg_since);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to seek to date: %m");
|
||||
since_seeked = true;
|
||||
|
||||
r = sd_journal_next(j);
|
||||
|
||||
} else {
|
||||
r = sd_journal_seek_head(j);
|
||||
if (r < 0)
|
||||
@@ -2552,7 +2567,7 @@ static int run(int argc, char *argv[]) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (arg_until_set && !arg_reverse) {
|
||||
if (arg_until_set && !arg_reverse && arg_lines < 0) {
|
||||
usec_t usec;
|
||||
|
||||
r = sd_journal_get_realtime_usec(j, &usec);
|
||||
@@ -2562,14 +2577,28 @@ static int run(int argc, char *argv[]) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (arg_since_set && arg_reverse) {
|
||||
if (arg_since_set && (arg_reverse || !since_seeked)) {
|
||||
usec_t usec;
|
||||
|
||||
r = sd_journal_get_realtime_usec(j, &usec);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to determine timestamp: %m");
|
||||
if (usec < arg_since)
|
||||
break;
|
||||
|
||||
if (usec < arg_since) {
|
||||
if (arg_reverse)
|
||||
break; /* Reached the earliest entry */
|
||||
|
||||
/* arg_lines >= 0 (!since_seeked):
|
||||
* We jumped arg_lines back and it seems to be too much */
|
||||
r = sd_journal_seek_realtime_usec(j, arg_since);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to seek to date: %m");
|
||||
since_seeked = true;
|
||||
|
||||
need_seek = true;
|
||||
continue;
|
||||
}
|
||||
since_seeked = true; /* We're surely within the range of --since now */
|
||||
}
|
||||
|
||||
if (!arg_merge && !arg_quiet) {
|
||||
|
||||
Reference in New Issue
Block a user