script add -T fmt to print time-stamps

script -r is useful for recording time-stamps of when output
happened.  With -T, rather than playback the script in real-time
we simply print the time-stamps to show when the output happened.

This is very useful for example, for analyzing boot time activity.

If the fmt provided contains no % characters the default
%n@ %s [%Y-%m-%d %T]
is used, which lends itself to analysis by tools as well as humans.

Sponsored by:	Juniper Networks, Inc.

Reviewed by:	allanjude
Differential Revision:	https://reviews.freebsd.org/D34511
This commit is contained in:
Simon J. Gerraty 2022-03-09 13:33:03 -08:00
parent 7ecd99fa42
commit 6c4afed566
2 changed files with 44 additions and 10 deletions

View File

@ -28,7 +28,7 @@
.\" @(#)script.1 8.1 (Berkeley) 6/6/93
.\" $FreeBSD$
.\"
.Dd January 5, 2021
.Dd March 9, 2022
.Dt SCRIPT 1
.Os
.Sh NAME
@ -43,6 +43,7 @@
.Nm
.Fl p
.Op Fl deq
.Op Fl T Ar fmt
.Op Ar file
.Sh DESCRIPTION
The
@ -119,6 +120,19 @@ causes
to flush after every character I/O event.
The default interval is
30 seconds.
.It Fl T Ar fmt
Implies
.Fl p ,
but just reports the time-stamp of each output.
This is very useful for assessing the timing of events.
.Pp
If
.Ar fmt
does not contain any
.Ql %
characters, it indicates the default format:
.Ql %n@ %s [%Y-%m-%d %T]\ ,
which is useful for both tools and humans to read, should be used.
.El
.Pp
The script ends when the forked shell (or command) exits (a

View File

@ -89,6 +89,13 @@ static TAILQ_HEAD(, buf_elm) obuf_list = TAILQ_HEAD_INITIALIZER(obuf_list);
static struct termios tt;
#ifndef TSTAMP_FMT
/* useful for tool and human reading */
# define TSTAMP_FMT "%n@ %s [%Y-%m-%d %T] "
#endif
static const char *tstamp_fmt = TSTAMP_FMT;
static int tflg;
static void done(int) __dead2;
static void doshell(char **);
static void finish(void);
@ -121,7 +128,7 @@ main(int argc, char *argv[])
warning. (not needed w/clang) */
showexit = 0;
while ((ch = getopt(argc, argv, "adeFfkpqrt:")) != -1)
while ((ch = getopt(argc, argv, "adeFfkpqrT:t:")) != -1)
switch(ch) {
case 'a':
aflg = 1;
@ -154,6 +161,11 @@ main(int argc, char *argv[])
if (flushtime < 0)
err(1, "invalid flush time %d", flushtime);
break;
case 'T':
tflg = pflg = 1;
if (strchr(optarg, '%'))
tstamp_fmt = optarg;
break;
case '?':
default:
usage();
@ -559,15 +571,23 @@ playback(FILE *fp)
(void)consume(fp, stamp.scr_len, buf, reg);
break;
case 'o':
tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
if (tsi.tv_nsec < 0) {
tsi.tv_sec -= 1;
tsi.tv_nsec += 1000000000;
if (tflg) {
if (stamp.scr_len == 0)
continue;
l = strftime(buf, sizeof buf, tstamp_fmt,
localtime(&tclock));
(void)write(STDOUT_FILENO, buf, l);
} else {
tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
if (tsi.tv_nsec < 0) {
tsi.tv_sec -= 1;
tsi.tv_nsec += 1000000000;
}
if (usesleep)
(void)nanosleep(&tsi, NULL);
tsi = tso;
}
if (usesleep)
(void)nanosleep(&tsi, NULL);
tsi = tso;
while (stamp.scr_len > 0) {
l = MIN(DEF_BUF, stamp.scr_len);
if (fread(buf, sizeof(char), l, fp) != l)