summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-01-04 02:14:42 +0100
committerLennart Poettering <lennart@poettering.net>2012-01-04 02:14:42 +0100
commit2100675e9fe0707fae8cee13fbe9316e94ef98f9 (patch)
tree9f7f897e8c31d6bf91766e9c3410a98857a22b89 /src
parent8e9e6d567a5a9185b6f65adf3d9d1901ce25131f (diff)
journalctl: add -n switch
Diffstat (limited to 'src')
-rw-r--r--src/journal/journalctl.c38
1 files changed, 31 insertions, 7 deletions
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 4c20ff66a8..fc1fed277c 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -42,7 +42,7 @@ static output_mode arg_output = OUTPUT_SHORT;
static bool arg_follow = false;
static bool arg_show_all = false;
static bool arg_no_pager = false;
-
+static int arg_lines = -1;
static int help(void) {
@@ -53,6 +53,7 @@ static int help(void) {
" --no-pager Do not pipe output into a pager\n"
" -a --all Show all properties, including long and unprintable\n"
" -f --follow Follow journal\n"
+ " -n --lines=INTEGER Lines to show\n"
" -o --output=STRING Change output mode (short, verbose, export, json)\n",
program_invocation_short_name);
@@ -73,15 +74,16 @@ static int parse_argv(int argc, char *argv[]) {
{ "follow", no_argument, NULL, 'f' },
{ "output", required_argument, NULL, 'o' },
{ "all", no_argument, NULL, 'a' },
+ { "lines", required_argument, NULL, 'n' },
{ NULL, 0, NULL, 0 }
};
- int c;
+ int c, r;
assert(argc >= 0);
assert(argv);
- while ((c = getopt_long(argc, argv, "hfo:a", options, NULL)) >= 0) {
+ while ((c = getopt_long(argc, argv, "hfo:an:", options, NULL)) >= 0) {
switch (c) {
@@ -122,6 +124,14 @@ static int parse_argv(int argc, char *argv[]) {
arg_show_all = true;
break;
+ case 'n':
+ r = safe_atoi(optarg, &arg_lines);
+ if (r < 0) {
+ log_error("Failed to parse lines '%s'", optarg);
+ return -EINVAL;
+ }
+ break;
+
case '?':
return -EINVAL;
@@ -166,10 +176,24 @@ int main(int argc, char *argv[]) {
goto finish;
}
- r = sd_journal_seek_head(j);
- if (r < 0) {
- log_error("Failed to seek to head: %s", strerror(-r));
- goto finish;
+ if (arg_lines >= 0) {
+ r = sd_journal_seek_tail(j);
+ if (r < 0) {
+ log_error("Failed to seek to tail: %s", strerror(-r));
+ goto finish;
+ }
+
+ r = sd_journal_previous_skip(j, arg_lines);
+ if (r < 0) {
+ log_error("Failed to iterate through journal: %s", strerror(-r));
+ goto finish;
+ }
+ } else {
+ r = sd_journal_seek_head(j);
+ if (r < 0) {
+ log_error("Failed to seek to head: %s", strerror(-r));
+ goto finish;
+ }
}
if (!arg_no_pager && !arg_follow) {