line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test2::Plugin::IOMuxer::Layer; |
2
|
3
|
|
|
3
|
|
178025
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
171
|
|
3
|
3
|
|
|
3
|
|
21
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
462
|
|
4
|
|
|
|
|
|
|
|
5
|
0
|
|
|
|
|
0
|
BEGIN { |
6
|
3
|
|
|
3
|
|
14
|
local $@ = undef; |
7
|
3
|
|
|
|
|
11
|
my $ok = eval { |
8
|
3
|
|
|
|
|
1477
|
require JSON::MaybeXS; |
9
|
3
|
|
|
|
|
15428
|
JSON::MaybeXS->import('JSON'); |
10
|
3
|
|
|
|
|
12
|
1; |
11
|
|
|
|
|
|
|
}; |
12
|
|
|
|
|
|
|
|
13
|
3
|
50
|
|
|
|
20
|
unless($ok) { |
14
|
0
|
|
|
|
|
0
|
require JSON::PP; |
15
|
0
|
|
|
|
|
0
|
*JSON = sub() { 'JSON::PP' }; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
3
|
|
|
|
|
14
|
my $json = JSON()->new->utf8(1); |
19
|
|
|
|
|
|
|
|
20
|
14
|
|
|
14
|
0
|
177
|
sub encode_json { $json->encode(@_) } |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
3
|
|
|
3
|
|
1465
|
use Time::HiRes qw/time/; |
|
3
|
|
|
|
|
3316
|
|
|
3
|
|
|
|
|
15
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our $VERSION = '0.000009'; |
26
|
|
|
|
|
|
|
|
27
|
3
|
|
|
3
|
|
1253
|
use Test2::Plugin::OpenFixPerlIO; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
74
|
|
28
|
3
|
|
|
3
|
|
1536
|
use IO::Handle; |
|
3
|
|
|
|
|
15024
|
|
|
3
|
|
|
|
|
2106
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
our %MUXED; |
31
|
|
|
|
|
|
|
our %MUX_FILES; |
32
|
|
|
|
|
|
|
|
33
|
7
|
|
|
7
|
0
|
27
|
sub name { 'other' } |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub PUSHED { |
36
|
4
|
|
|
4
|
0
|
3690
|
my ($class, $mode, $handle) = @_; |
37
|
4
|
|
|
|
|
27
|
$handle->autoflush(1); |
38
|
4
|
|
|
|
|
288
|
bless {buffer => [], handle => $handle, count => 0}, $class; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub FLUSH { |
42
|
7
|
|
|
7
|
0
|
46
|
my $self = shift; |
43
|
7
|
|
|
|
|
15
|
my ($handle) = @_; |
44
|
7
|
|
|
|
|
69
|
$handle->flush; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $DEPTH = 0; |
48
|
|
|
|
|
|
|
sub WRITE { |
49
|
16
|
|
|
16
|
|
127
|
my ($self, $buffer, $handle) = @_; |
50
|
|
|
|
|
|
|
|
51
|
16
|
|
|
|
|
43
|
my $count = ++$self->{count}; |
52
|
|
|
|
|
|
|
|
53
|
16
|
50
|
33
|
|
|
78
|
if ($self->{DIED} || $DEPTH) { |
54
|
0
|
|
|
|
|
0
|
print $handle $buffer; |
55
|
0
|
|
|
|
|
0
|
return length($buffer); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
16
|
|
|
|
|
28
|
$DEPTH++; |
59
|
|
|
|
|
|
|
|
60
|
16
|
|
|
|
|
30
|
my $ok1 = eval { |
61
|
16
|
|
|
|
|
45
|
my $time = time; |
62
|
16
|
|
|
|
|
32
|
my $fileno = fileno($handle); |
63
|
|
|
|
|
|
|
|
64
|
16
|
|
|
|
|
72
|
my @parts = split /(\n)/, $buffer; |
65
|
16
|
50
|
66
|
|
|
61
|
unshift @parts => '' if @parts == 1 && $parts[0] eq "\n"; |
66
|
16
|
100
|
|
|
|
46
|
push @parts => undef if @parts % 2; |
67
|
16
|
|
|
|
|
45
|
my %parts = @parts; |
68
|
16
|
|
|
|
|
34
|
for my $part (@parts) { |
69
|
36
|
100
|
|
|
|
118
|
next unless defined $part; |
70
|
32
|
100
|
|
|
|
84
|
next if $part eq "\n"; |
71
|
|
|
|
|
|
|
|
72
|
18
|
|
|
|
|
59
|
my $about = {stamp => $time, fileno => $fileno, name => $self->name, buffer => $part, write_no => $count}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Time to flush |
75
|
18
|
100
|
|
|
|
49
|
if ($parts{$part}) { |
76
|
14
|
|
|
|
|
45
|
$about->{buffer} .= $parts{$part}; # Put the \n back |
77
|
|
|
|
|
|
|
|
78
|
14
|
|
|
|
|
25
|
my $out; |
79
|
14
|
100
|
|
|
|
21
|
if (@{$self->{buffer}}) { |
|
14
|
|
|
|
|
45
|
|
80
|
2
|
|
|
|
|
5
|
push @{$self->{buffer}} => $about; |
|
2
|
|
|
|
|
6
|
|
81
|
|
|
|
|
|
|
$out = { |
82
|
|
|
|
|
|
|
parts => $self->{buffer}, |
83
|
|
|
|
|
|
|
%$about, |
84
|
2
|
|
|
|
|
9
|
buffer => join '' => map { $_->{buffer} } @{$self->{buffer}}, |
|
6
|
|
|
|
|
21
|
|
|
2
|
|
|
|
|
6
|
|
85
|
|
|
|
|
|
|
}; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Reset the buffer |
88
|
2
|
|
|
|
|
6
|
$self->{buffer} = []; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
else { # Easy |
91
|
12
|
|
|
|
|
18
|
$out = $about; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
14
|
|
|
|
|
35
|
my $json = encode_json($out); |
95
|
14
|
|
|
|
|
35
|
my $mh = $MUX_FILES{$MUXED{$fileno}}; |
96
|
14
|
|
|
|
|
126
|
print $mh $json, "\n"; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
else { |
99
|
4
|
|
|
|
|
9
|
push @{$self->{buffer}} => $about; |
|
4
|
|
|
|
|
13
|
|
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
16
|
|
|
|
|
44
|
1; |
104
|
|
|
|
|
|
|
}; |
105
|
16
|
|
|
|
|
33
|
my $err1 = $@; |
106
|
|
|
|
|
|
|
|
107
|
16
|
|
|
|
|
30
|
my $ok2 = eval { print $handle $buffer }; |
|
16
|
|
|
|
|
241
|
|
108
|
16
|
|
|
|
|
37
|
my $err2 = $@; |
109
|
|
|
|
|
|
|
|
110
|
16
|
|
|
|
|
25
|
$DEPTH--; |
111
|
|
|
|
|
|
|
|
112
|
16
|
50
|
33
|
|
|
75
|
unless ($ok1 && $ok2) { |
113
|
0
|
|
|
|
|
0
|
$self->{DIED}++; |
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
0
|
die $err2 if $ok1; |
116
|
0
|
0
|
|
|
|
0
|
die $err1 if $ok2; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
0
|
warn $err2; |
119
|
0
|
|
|
|
|
0
|
die $err1; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
16
|
|
|
|
|
72
|
return length($buffer); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub DESTROY { |
126
|
4
|
|
|
4
|
|
9
|
my $self = shift; |
127
|
4
|
50
|
|
|
|
14
|
my $handle = $self->{handle} or return; |
128
|
4
|
50
|
|
|
|
13
|
my $buffer = $self->{buffer} or return; |
129
|
4
|
50
|
|
|
|
25
|
return unless @$buffer; |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
my $fileno = fileno($handle); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
my $out = { |
134
|
|
|
|
|
|
|
parts => $self->{buffer}, |
135
|
|
|
|
|
|
|
stamp => time, |
136
|
|
|
|
|
|
|
fileno => $fileno, |
137
|
|
|
|
|
|
|
name => $self->name, |
138
|
|
|
|
|
|
|
write_no => ++$self->{count}, |
139
|
|
|
|
|
|
|
DESTROY => 1, |
140
|
0
|
|
|
|
|
|
buffer => join '' => map { $_->{buffer} } @$buffer, |
|
0
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
}; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
my $json = encode_json($out); |
144
|
0
|
|
|
|
|
|
my $mh = $MUX_FILES{$MUXED{$fileno}}; |
145
|
0
|
|
|
|
|
|
print $mh $json, "\n"; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
__END__ |