Debian Codegraph @

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

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # A simple 'tee' implementation, using perl tie.
5 #
6 # Whenever you print to the handle, it gets forwarded to a list of
7 # handles. The list of output filehandles is passed to the constructor.
8 #
9 # This is similar to IO::Tee, but only used for output. Only the PRINT
10 # method is currently implemented; that's all we need. We don't want to
11 # depend on IO::Tee just for this.
12
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
18 package PostgreSQL::Test::SimpleTee;
19 use strict;
20 use warnings FATAL => 'all';
21
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
39 sub TIEHANDLE
40 {
41 my $self = shift;
42 return bless \@_, $self;
43 }
44
45 sub PRINT
46 {
47 my $self = shift;
48 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;
54 for my $fh (@$self)
55 {
56 print $fh ($skip ? "" : $ts), @_ or $ok = 0;
57 $fh->flush or $ok = 0;
58 $skip = 0;
59 }
60 return $ok;
61 }
62
63 1;