git: 6c4afed5667a - main - script add -T fmt to print time-stamps
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 09 Mar 2022 21:33:15 UTC
The branch main has been updated by sjg:
URL: https://cgit.FreeBSD.org/src/commit/?id=6c4afed5667a65f3c5bd308a934e7de8c0526954
commit 6c4afed5667a65f3c5bd308a934e7de8c0526954
Author: Simon J. Gerraty <sjg@FreeBSD.org>
AuthorDate: 2022-03-09 21:33:03 +0000
Commit: Simon J. Gerraty <sjg@FreeBSD.org>
CommitDate: 2022-03-09 21:33:03 +0000
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
---
usr.bin/script/script.1 | 16 +++++++++++++++-
usr.bin/script/script.c | 38 +++++++++++++++++++++++++++++---------
2 files changed, 44 insertions(+), 10 deletions(-)
diff --git a/usr.bin/script/script.1 b/usr.bin/script/script.1
index 04fa75fc1612..e6c422b476f5 100644
--- a/usr.bin/script/script.1
+++ b/usr.bin/script/script.1
@@ -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
diff --git a/usr.bin/script/script.c b/usr.bin/script/script.c
index 9c18dc73390f..2e540a935ecd 100644
--- a/usr.bin/script/script.c
+++ b/usr.bin/script/script.c
@@ -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)