chiark / gitweb /
journalctl: fix counting of -n parameter
authorLennart Poettering <lennart@poettering.net>
Wed, 4 Jan 2012 03:00:14 +0000 (04:00 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 4 Jan 2012 03:00:14 +0000 (04:00 +0100)
src/journal/journalctl.c
src/journal/sd-journal.c
src/systemctl.c

index fc1fed277c4df6a0921b2be11f5d9125ad43097e..0e1fb66de6da8a5aa49af56369dbb4d6cf6d3cc7 100644 (file)
@@ -148,6 +148,7 @@ int main(int argc, char *argv[]) {
         int r, i, fd;
         sd_journal *j = NULL;
         unsigned line = 0;
         int r, i, fd;
         sd_journal *j = NULL;
         unsigned line = 0;
+        bool need_seek = false;
 
         log_parse_environment();
         log_open();
 
         log_parse_environment();
         log_open();
@@ -184,16 +185,19 @@ int main(int argc, char *argv[]) {
                 }
 
                 r = sd_journal_previous_skip(j, arg_lines);
                 }
 
                 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;
                 }
         } 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) {
         }
 
         if (!arg_no_pager && !arg_follow) {
@@ -210,11 +214,12 @@ int main(int argc, char *argv[]) {
                 struct pollfd pollfd;
 
                 for (;;) {
                 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)
                         }
 
                         if (r == 0)
@@ -225,6 +230,8 @@ int main(int argc, char *argv[]) {
                         r = output_journal(j, arg_output, line, arg_show_all);
                         if (r < 0)
                                 goto finish;
                         r = output_journal(j, arg_output, line, arg_show_all);
                         if (r < 0)
                                 goto finish;
+
+                        need_seek = true;
                 }
 
                 if (!arg_follow)
                 }
 
                 if (!arg_follow)
index 78a91a3eb00c46d3b88ef691f7fc259ea0ab8100..05c0d96ce180165f225a872398090e2366e09660 100644 (file)
@@ -683,14 +683,23 @@ _public_ int sd_journal_previous(sd_journal *j) {
         return real_journal_next(j, DIRECTION_UP);
 }
 
         return real_journal_next(j, DIRECTION_UP);
 }
 
-_public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
+static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
         int c = 0, r;
 
         if (!j)
                 return -EINVAL;
 
         int c = 0, r;
 
         if (!j)
                 return -EINVAL;
 
-        while (skip > 0) {
-                r = sd_journal_next(j);
+        if (skip == 0) {
+                /* If this is not a discrete skip, then at least
+                 * resolve the current location */
+                if (j->current_location.type != LOCATION_DISCRETE)
+                        return real_journal_next(j, direction);
+
+                return 0;
+        }
+
+        do {
+                r = real_journal_next(j, direction);
                 if (r < 0)
                         return r;
 
                 if (r < 0)
                         return r;
 
@@ -699,30 +708,17 @@ _public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
 
                 skip--;
                 c++;
 
                 skip--;
                 c++;
-        }
+        } while (skip > 0);
 
         return c;
 }
 
 
         return c;
 }
 
-_public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
-        int c = 0, r;
-
-        if (!j)
-                return -EINVAL;
-
-        while (skip > 0) {
-                r = sd_journal_previous(j);
-                if (r < 0)
-                        return r;
-
-                if (r == 0)
-                        return c;
-
-                skip--;
-                c++;
-        }
+_public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
+        return real_journal_next_skip(j, DIRECTION_DOWN, skip);
+}
 
 
-        return 1;
+_public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
+        return real_journal_next_skip(j, DIRECTION_UP, skip);
 }
 
 _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
 }
 
 _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
index 10e3991745a59878ab10d48fcdc5359f55458b63..6f87b06dd3401c5640bf1e89bdb6fc98940be98f 100644 (file)
@@ -2262,8 +2262,10 @@ static void print_status_info(UnitStatusInfo *i) {
                 }
         }
 
                 }
         }
 
-        if (i->id && arg_transport != TRANSPORT_SSH)
+        if (i->id && arg_transport != TRANSPORT_SSH) {
+                printf("\n");
                 show_journal_by_service(i->id, OUTPUT_SHORT, NULL, 0, 0, 0, arg_all);
                 show_journal_by_service(i->id, OUTPUT_SHORT, NULL, 0, 0, 0, arg_all);
+        }
 
         if (i->need_daemon_reload)
                 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
 
         if (i->need_daemon_reload)
                 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",