[Patch] script: Add --ts-output and --ts-script options. -v3

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch introduces two new options (actually three).

--ts-output	Add a formatted timestamp to stdout for each new line
		of output.  The timestamp is composed of two floating
		point values, the first is seconds since the script
		was started.  The second is seconds since the previous
		line of output.

--ts-script	Puts the same values as above in the typescript file
		instead of stdout.

-T		synonym for --ts-output --ts-script

I adapted this from a tweak Jack Steiner had done on a version of script.
His version implemented a -T option which added the timestamp to both
output files.


Signed-off-by: Robin Holt <holt@xxxxxxx>
Cc: Jack Steiner <steiner@xxxxxxx>
Cc: Karel Zak <kzak@xxxxxxxxxx>
Cc: util-linux-nq list <util-linux-ng@xxxxxxxxxxxxxxx>

---

Changes since v2
  - Removed the .po and .pot internationalization files.

Changes since v1
  - Refreshed my quilt tree to include all the changes.

Example output:
./misc-utils/script --ts-output -c /tmp/example.sh 
Script started, file is typescript
    0.010638 (    0.326373)| + echo -e 'This is a test...\n\n\nThis is only a test\n\n'
    0.010702 (    0.000064)| This is a test...
    0.010714 (    0.000012)| 
    0.010725 (    0.000011)| 
    0.010735 (    0.000010)| This is only a test
    0.010745 (    0.000010)| 
    0.010755 (    0.000010)| 
    0.010765 (    0.000010)| + sleep 5
    5.010613 (    4.999848)| + echo -e 'The test is now done\n\nOut.'
    5.010644 (    0.000031)| The test is now done
    5.010657 (    0.000013)| 
    5.010667 (    0.000010)| Out.
    5.010677 (    0.000010)| + sleep 1
Script done, file is typescript

I have verified that 'scriptreplay' still functions.  I have
compile-and-run tested it on i386, x86_64, and ia64 linux.  I updated
the translations to the best of my ability.


 misc-utils/script.1 |   12 +++++++
 misc-utils/script.c |   86 ++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 92 insertions(+), 6 deletions(-)
Index: util-linux-ng/misc-utils/script.c
===================================================================
--- util-linux-ng.orig/misc-utils/script.c	2009-07-13 11:02:20.000000000 -0500
+++ util-linux-ng/misc-utils/script.c	2009-07-13 11:02:21.000000000 -0500
@@ -95,12 +95,21 @@
 int	fflg = 0;
 int	qflg = 0;
 int	tflg = 0;
+int	ts_output = 0;		/* put timestamp on stdout */
+int	ts_script = 0;		/* put timestamp in script output */
 
 static char *progname;
 
 int die;
 int resized;
 
+
+static struct option long_options[] = {
+	{"ts-output", no_argument, &ts_output, 1},
+	{"ts-script", no_argument, &ts_script, 1},
+	{0, 0, 0, 0}
+};
+
 static void
 die_if_link(char *fn) {
 	struct stat s;
@@ -131,6 +140,7 @@
 	extern int optind;
 	char *p;
 	int ch;
+	int option_index = 0;
 
 	progname = argv[0];
 	if ((p = strrchr(progname, '/')) != NULL)
@@ -150,8 +160,10 @@
 		}
 	}
 
-	while ((ch = getopt(argc, argv, "ac:fqt")) != -1)
+	while ((ch = getopt_long(argc, argv, "ac:fqtT", long_options, &option_index)) != -1)
 		switch((char)ch) {
+		case 0:	/* long option flag already set. */
+			break;
 		case 'a':
 			aflg++;
 			break;
@@ -167,10 +179,14 @@
 		case 't':
 			tflg++;
 			break;
+		case 'T':
+			ts_output++;
+			ts_script++;
+			break;
 		case '?':
 		default:
 			fprintf(stderr,
-				_("usage: script [-a] [-f] [-q] [-t] [file]\n"));
+				_("usage: script [-a] [-c <command>] [-f] [-q] [-t] [--ts-output] [--ts-script] [-T] [file]\n"));
 			exit(1);
 		}
 	argc -= optind;
@@ -288,12 +304,16 @@
 dooutput() {
 	register ssize_t cc;
 	time_t tvec;
-	char obuf[BUFSIZ];
+	char obuf[BUFSIZ], *obuf_p;
 	struct timeval tv;
 	double oldtime=time(NULL), newtime;
 	int flgs = 0;
 	ssize_t wrt;
 	size_t fwrt;
+	int nl=1;
+	int need_ts = ts_output + ts_script;
+	double oldtime_ts=time(NULL), newtime_ts, starttime_ts;
+	ssize_t real_cc;
 
 	(void) close(0);
 #ifdef HAVE_LIBUTIL
@@ -302,6 +322,10 @@
 	tvec = time((time_t *)NULL);
 	my_strftime(obuf, sizeof obuf, "%c\n", localtime(&tvec));
 	fprintf(fscript, _("Script started on %s"), obuf);
+	if (need_ts) {
+		gettimeofday(&tv, NULL);
+		starttime_ts = tv.tv_sec + (double) tv.tv_usec / 1000000;
+	}
 
 	if (die == 0 && child && kill(child, 0) == -1 && errno == ESRCH)
 		/*
@@ -336,25 +360,75 @@
 			continue;
 		if (cc <= 0)
 			break;
+		obuf_p = obuf;
+continue_newline:
+		if (need_ts && nl) {
+			char buf_ts[30];
+			gettimeofday(&tv, NULL);
+			newtime_ts = tv.tv_sec + (double) tv.tv_usec / 1000000;
+			sprintf(buf_ts, "%12.6f (%12.6f)| ",
+				newtime_ts - starttime_ts, newtime_ts - oldtime_ts);
+			oldtime_ts = newtime_ts;
+
+			if (ts_output) {
+				wrt = write(1, buf_ts, 29);
+				if (wrt != 29) {
+					int err = errno;
+					fprintf (stderr, _("%s: write error: %s\n"),
+						progname, strerror(err));
+					fail();
+				}
+			}
+			if (ts_script) {
+				fwrt = fwrite(buf_ts, 1, 29, fscript);
+				if (fwrt != 29) {
+					int err = errno;
+					fprintf (stderr, _("%s: cannot write script file, error: %s\n"),
+						progname, strerror(err));
+					fail();
+				}
+			}
+		}
+		if (need_ts) {
+			int i;
+
+			real_cc = cc;
+
+			for (i=0; i<cc; i++) {
+				nl = (obuf_p[i] == '\n');
+				if (nl) {
+					cc = i + 1;
+					break;
+				}
+			}
+		}
 		if (tflg) {
 			newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
-			fprintf(stderr, "%f %zd\n", newtime - oldtime, cc);
+			fprintf(stderr, "%f %zd\n", newtime - oldtime,
+				cc + (ts_script && nl ? 29 : 0));
 			oldtime = newtime;
 		}
-		wrt = write(1, obuf, cc);
+		wrt = write(1, obuf_p, cc);
 		if (wrt < 0) {
 			int err = errno;
 			fprintf (stderr, _("%s: write error: %s\n"),
 				progname, strerror(err));
 			fail();
 		}
-		fwrt = fwrite(obuf, 1, cc, fscript);
+		fwrt = fwrite(obuf_p, 1, cc, fscript);
 		if (fwrt < cc) {
 			int err = errno;
 			fprintf (stderr, _("%s: cannot write script file, error: %s\n"),
 				progname, strerror(err));
 			fail();
 		}
+		if (need_ts) {
+			if (real_cc != cc) {
+				obuf_p = &obuf_p[cc];
+				cc = real_cc - cc;
+				goto continue_newline;
+			}
+		}
 		if (fflg)
 			(void) fflush(fscript);
 	} while(1);
Index: util-linux-ng/misc-utils/script.1
===================================================================
--- util-linux-ng.orig/misc-utils/script.1	2009-07-13 11:02:20.000000000 -0500
+++ util-linux-ng/misc-utils/script.1	2009-07-13 11:02:21.000000000 -0500
@@ -44,6 +44,9 @@
 .Op Fl f
 .Op Fl q
 .Op Fl t
+.Op Fl --ts-output
+.Op Fl --ts-script
+.Op Fl T
 .Op Ar file
 .Sh DESCRIPTION
 .Nm Script
@@ -86,6 +89,15 @@
 the previous output. The second field indicates how many characters were
 output this time. This information can be used to replay typescripts with
 realistic typing and output delays.
+.It Fl -ts-output
+Add a formatted timestamp to stdout for each new line of output.
+The timestamp is composed of two floating point values, the first is
+seconds since the script was started.  The second is seconds since the
+previous line of output.
+.It Fl -ts-script
+Puts the same values as above in the typescript file instead of stdout.
+.It Fl T
+The same as specifying --ts-output --ts-script
 .El
 .Pp
 The script ends when the forked shell exits (a
--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux