git: 9ba17e2d1c2f - stable/13 - script add -T fmt to print time-stamps
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 02 Nov 2022 12:35:53 UTC
The branch stable/13 has been updated by des:
URL: https://cgit.FreeBSD.org/src/commit/?id=9ba17e2d1c2ffc9d00ce020d53523365a0d85f1b
commit 9ba17e2d1c2ffc9d00ce020d53523365a0d85f1b
Author: Simon J. Gerraty <sjg@FreeBSD.org>
AuthorDate: 2022-03-09 21:33:03 +0000
Commit: Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2022-11-02 09:44:46 +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
(cherry picked from commit 6c4afed5667a65f3c5bd308a934e7de8c0526954)
script -T skip timstamps for same second
The result is much more readable if we only output the time-stamp
when it is at least 1s since last one.
(cherry picked from commit 7b45ad3f89cc4d65a23f7d034329dd3f8dd3105f)
script: use %n at the end of default tstamp_fmt
Since we are only outputting time-stamps when they differ
ending it with a newline, interferes with the output less.
(cherry picked from commit 31fde973577d0e09caccf0d762135bfa6b14f1f3)
---
usr.bin/script/script.1 | 18 +++++++++++++++++-
usr.bin/script/script.c | 43 ++++++++++++++++++++++++++++++++++---------
2 files changed, 51 insertions(+), 10 deletions(-)
diff --git a/usr.bin/script/script.1 b/usr.bin/script/script.1
index 04fa75fc1612..36523148fa90 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,21 @@ 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]%n ,
+which is useful for both tools and humans to read, should be used.
+Note that time-stamps will only be output when different from the
+previous one.
.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..430f48ad63df 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]%n"
+#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();
@@ -511,12 +523,14 @@ playback(FILE *fp)
off_t nread, save_len;
size_t l;
time_t tclock;
+ time_t lclock;
int reg;
if (fstat(fileno(fp), &pst) == -1)
err(1, "fstat failed");
reg = S_ISREG(pst.st_mode);
+ lclock = 0;
for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
@@ -559,15 +573,26 @@ 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;
+ if (tclock - lclock > 0) {
+ l = strftime(buf, sizeof buf, tstamp_fmt,
+ localtime(&tclock));
+ (void)write(STDOUT_FILENO, buf, l);
+ }
+ lclock = tclock;
+ } 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)