chiark / gitweb /
systemctl: shortcut log output for non-service, non-socket, non-mount, non-swap units
[elogind.git] / src / journal / journalctl.c
index fc1fed277c4df6a0921b2be11f5d9125ad43097e..e888990f23ece613d332fe3e69d49bdba73f7ade 100644 (file)
 #include "logs-show.h"
 
 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 bool arg_no_tail = false;
 
 static int help(void) {
 
@@ -54,6 +54,7 @@ static int help(void) {
                "  -a --all            Show all properties, including long and unprintable\n"
                "  -f --follow         Follow journal\n"
                "  -n --lines=INTEGER  Lines to show\n"
+               "     --no-tail        Show all lines, even in follow mode\n"
                "  -o --output=STRING  Change output mode (short, verbose, export, json)\n",
                program_invocation_short_name);
 
@@ -64,7 +65,8 @@ static int parse_argv(int argc, char *argv[]) {
 
         enum {
                 ARG_VERSION = 0x100,
-                ARG_NO_PAGER
+                ARG_NO_PAGER,
+                ARG_NO_TAIL
         };
 
         static const struct option options[] = {
@@ -75,6 +77,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "output",    required_argument, NULL, 'o'           },
                 { "all",       no_argument,       NULL, 'a'           },
                 { "lines",     required_argument, NULL, 'n'           },
+                { "no-tail",   no_argument,       NULL, ARG_NO_TAIL   },
                 { NULL,        0,                 NULL, 0             }
         };
 
@@ -126,12 +129,16 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case 'n':
                         r = safe_atoi(optarg, &arg_lines);
-                        if (r < 0) {
+                        if (r < 0 || arg_lines < 0) {
                                 log_error("Failed to parse lines '%s'", optarg);
                                 return -EINVAL;
                         }
                         break;
 
+                case ARG_NO_TAIL:
+                        arg_no_tail = true;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -141,6 +148,9 @@ static int parse_argv(int argc, char *argv[]) {
                 }
         }
 
+        if (arg_follow && !arg_no_tail)
+                arg_lines = 10;
+
         return 1;
 }
 
@@ -148,6 +158,7 @@ int main(int argc, char *argv[]) {
         int r, i, fd;
         sd_journal *j = NULL;
         unsigned line = 0;
+        bool need_seek = false;
 
         log_parse_environment();
         log_open();
@@ -184,16 +195,19 @@ int main(int argc, char *argv[]) {
                 }
 
                 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;
                 }
+
+                r = sd_journal_next(j);
+        }
+
+        if (r < 0) {
+                log_error("Failed to iterate through journal: %s", strerror(-r));
+                goto finish;
         }
 
         if (!arg_no_pager && !arg_follow) {
@@ -210,11 +224,12 @@ int main(int argc, char *argv[]) {
                 struct pollfd pollfd;
 
                 for (;;) {
-                        r = sd_journal_next(j);
-
-                        if (r < 0) {
-                                log_error("Failed to iterate through journal: %s", strerror(-r));
-                                goto finish;
+                        if (need_seek) {
+                                r = sd_journal_next(j);
+                                if (r < 0) {
+                                        log_error("Failed to iterate through journal: %s", strerror(-r));
+                                        goto finish;
+                                }
                         }
 
                         if (r == 0)
@@ -225,6 +240,8 @@ int main(int argc, char *argv[]) {
                         r = output_journal(j, arg_output, line, arg_show_all);
                         if (r < 0)
                                 goto finish;
+
+                        need_seek = true;
                 }
 
                 if (!arg_follow)