Skip to content

Commit 3b0a42e

Browse files
committed
Add timestamp and elapsed time decorations to TAP test logs
These apply to traces from Test::More functions such as ok(), is(), diag() and note(). Output from other sources (e.g. external programs such a initdb) is not affected. The elapsed time is the time since the last such trace (or the beginning of the test in the first case). Times and timestamps are at millisecond precision. Discussion: https://postgr.es/m/20220401172150.rsycz4lrn7ewruil@alap3.anarazel.de
1 parent 7b735f8 commit 3b0a42e

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/test/perl/PostgreSQL/Test/SimpleTee.pm

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,32 @@
1010
# method is currently implemented; that's all we need. We don't want to
1111
# depend on IO::Tee just for this.
1212

13+
# The package is enhanced to add timestamp and elapsed time decorations to
14+
# the log file traces sent through this interface from Test::More functions
15+
# (ok, is, note, diag etc.). Elapsed time is shown as the time since the last
16+
# log trace.
17+
1318
package PostgreSQL::Test::SimpleTee;
1419
use strict;
1520
use warnings;
1621

22+
use Time::HiRes qw(time);
23+
24+
my $last_time;
25+
26+
BEGIN { $last_time = time; }
27+
28+
sub _time_str
29+
{
30+
my $tm = time;
31+
my $diff = $tm - $last_time;
32+
$last_time = $tm;
33+
my ($sec, $min, $hour) = localtime($tm);
34+
my $msec = int(1000 * ($tm - int($tm)));
35+
return sprintf("[%.2d:%.2d:%.2d.%.3d](%.3fs) ",
36+
$hour, $min, $sec, $msec, $diff);
37+
}
38+
1739
sub TIEHANDLE
1840
{
1941
my $self = shift;
@@ -24,10 +46,16 @@ sub PRINT
2446
{
2547
my $self = shift;
2648
my $ok = 1;
49+
# The first file argument passed to tiehandle in PostgreSQL::Test::Utils is
50+
# the original stdout, which is what PROVE sees. Additional decorations
51+
# confuse it, so only put out the time string on files after the first.
52+
my $skip = 1;
53+
my $ts = _time_str;
2754
for my $fh (@$self)
2855
{
29-
print $fh @_ or $ok = 0;
56+
print $fh ($skip ? "" : $ts), @_ or $ok = 0;
3057
$fh->flush or $ok = 0;
58+
$skip = 0;
3159
}
3260
return $ok;
3361
}

0 commit comments

Comments
 (0)