I often find I want to see more than the default 10 lines of output
with syslogc -f. This diff adds support for -n, like in tail. It works
both with and without -f. e.g.:

# syslogc -n 5 ospfd
Jul  9 08:28:43 gr1-pl7 last message repeated 3 times
Jul  9 08:29:18 gr1-pl7 ospfd[26625]: recv_packet: authentication error, 
interface em3
Jul  9 08:29:20 gr1-pl7 ospfd[13829]: reloading interface list and routing table
Jul  9 08:29:35 gr1-pl7 last message repeated 7 times
Jul  9 09:08:32 gr1-pl7 ospfd[13829]: reloading interface list and routing table

Any comments? OK?



Index: syslogc/syslogc.8
===================================================================
RCS file: /cvs/src/usr.sbin/syslogc/syslogc.8,v
retrieving revision 1.8
diff -u -p -r1.8 syslogc.8
--- syslogc/syslogc.8   10 Sep 2008 22:17:33 -0000      1.8
+++ syslogc/syslogc.8   9 Jul 2011 09:07:23 -0000
@@ -48,7 +48,7 @@ option to
 .Pp
 By default,
 .Nm
-will query the specified log and return it to standard output.
+will query the specified log and return all entries to standard output.
 .Pp
 The options are as follows:
 .Bl -tag -width Ds
@@ -57,11 +57,14 @@ Request that the log buffer be cleared w
 .It Fl c
 Request that the log buffer be cleared once it has been read.
 .It Fl f
-Print out the last 10 lines and read from the buffer continuously.
+Print out the last lines and read from the buffer continuously.
 Like the
 .Fl f
 option in
 .Xr tail 1 .
+Defaults to 10 lines.
+.It Fl n Ar lines
+Print the specified number of lines from the buffer.
 .It Fl o
 Check whether the specified log has overflowed.
 If the log has overflowed, then a message will be printed to
Index: syslogc/syslogc.c
===================================================================
RCS file: /cvs/src/usr.sbin/syslogc/syslogc.c,v
retrieving revision 1.15
diff -u -p -r1.15 syslogc.c
--- syslogc/syslogc.c   4 Jul 2011 20:23:09 -0000       1.15
+++ syslogc/syslogc.c   9 Jul 2011 09:07:23 -0000
@@ -22,6 +22,7 @@
 
 #include <err.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -33,7 +34,7 @@
 /*
  * Client protocol NB. all numeric fields in network byte order
  */
-#define CTL_VERSION            1
+#define CTL_VERSION            2
 
 /* Request */
 struct ctl_cmd {
@@ -45,6 +46,7 @@ struct ctl_cmd {
 #define CMD_FLAGS      5       /* Query flags only */
 #define CMD_READ_CONT  6       /* Read out log continuously */
        u_int32_t       cmd;
+       u_int32_t       lines;
        char            logname[MAX_MEMBUF_NAME];
 };
 
@@ -61,7 +63,8 @@ usage(void)
 {
        extern char *__progname;
 
-       fprintf(stderr, "usage: %s [-Ccfo] [-s reporting_socket] logname\n"
+       fprintf(stderr,
+           "usage: %s [-Ccfo] [-n lines] [-s reporting_socket] logname\n"
            "       %s -q\n", __progname, __progname);
        exit(1);
 }
@@ -78,12 +81,13 @@ main(int argc, char **argv)
        extern int optind;
        struct ctl_cmd cc;
        struct ctl_reply_hdr rr;
+       const char *errstr;
 
        memset(&cc, '\0', sizeof(cc));
 
        ctlsock_path = DEFAULT_CTLSOCK;
        rval = oflag = 0;
-       while ((ch = getopt(argc, argv, "Ccfhoqs:")) != -1) {
+       while ((ch = getopt(argc, argv, "Ccfhon:qs:")) != -1) {
                switch (ch) {
                case 'C':
                        cc.cmd = CMD_CLEAR;
@@ -96,6 +100,12 @@ main(int argc, char **argv)
                        break;
                case 'f':
                        cc.cmd = CMD_READ_CONT;
+                       break;
+               case 'n':
+                       cc.lines = strtonum(optarg, 1, UINT32_MAX, &errstr);
+                       if (errstr)
+                               errx(1, "number of lines is %s: %s",
+                                   errstr, optarg);
                        break;
                case 'o':
                        cc.cmd = CMD_FLAGS;
Index: syslogd/syslogd.c
===================================================================
RCS file: /cvs/src/usr.sbin/syslogd/syslogd.c,v
retrieving revision 1.103
diff -u -p -r1.103 syslogd.c
--- syslogd/syslogd.c   27 Oct 2009 23:59:54 -0000      1.103
+++ syslogd/syslogd.c   9 Jul 2011 09:07:23 -0000
@@ -207,7 +207,7 @@ int membuf_drop = 0;        /* logs were droppe
 /*
  * Client protocol NB. all numeric fields in network byte order
  */
-#define CTL_VERSION            1
+#define CTL_VERSION            2
 
 /* Request */
 struct {
@@ -219,6 +219,7 @@ struct      {
 #define CMD_FLAGS      5       /* Query flags only */
 #define CMD_READ_CONT  6       /* Read out log continuously */
        u_int32_t       cmd;
+       u_int32_t       lines;
        char            logname[MAX_MEMBUF_NAME];
 }      ctl_cmd;
 
@@ -1910,7 +1911,10 @@ ctlconn_read_handler(void)
                        }
                        if (ctl_cmd.cmd == CMD_READ_CONT) {
                                f->f_un.f_mb.f_attached = 1;
-                               tailify_replytext(reply_text, 10);
+                               tailify_replytext(reply_text,
+                                   ctl_cmd.lines > 0 ? ctl_cmd.lines : 10);
+                       } else if (ctl_cmd.lines > 0) {
+                               tailify_replytext(reply_text, ctl_cmd.lines);
                        }
                }
                break;

Reply via email to