Agent/Driver/Datum.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 121 CC 6 SCAR 16.9
CC
6
Cov
0%
intercept
20.0
logwrite
12.0
make
2.0
CRAP 5.4
1 ###########################################################################
2 #
3 # Datum.pm
4 #
5 # Copyright (C) 1999 Raphael Manfredi.
6 # Copyright (C) 2002-2017 Mark Rogaski, mrogaski@cpan.org;
7 # all rights reserved.
8 #
9 # See the README file included with the
10 # distribution for license information.
11 #
12 ##########################################################################
13  
14 use strict;
15 require Log::Agent::Driver;
16  
17 ########################################################################
18 package Log::Agent::Driver::Datum;
19  
20 use vars qw(@ISA);
21  
22 @ISA = qw(Log::Agent::Driver);
23  
24 #
25 # ->make -- defined
26 #
27 # Creation routine.
28 #
29 # Attributes:
30 # driver the underlying driver originally configured
31 #
32 sub make {
33 my $self = bless {}, shift;
34 my ($driver) = @_;
35 $self->_init('', 0); # 0 is the skip Carp penalty
36 $self->{driver} = $driver;
37 $driver->add_penalty(2); # We're intercepting the calls
38 return $self;
39 }
40  
41 #
42 # Attribute access
43 #
44  
45 sub prefix { $_[0]->{driver}->prefix }
46 sub driver { $_[0]->{driver} }
47  
48 #
49 # Cannot-be-called routines.
50 #
51  
52 sub prefix_msg { require Carp; Carp::confess("prefix_msg") }
53 sub emit { require Carp; Carp::confess("emit") }
54  
55 #
56 # ->channel_eq -- defined
57 #
58 # Redirect comparison to driver.
59 #
60 sub channel_eq {
61 my $self = shift;
62 my ($chan1, $chan2) = @_;
63 return $self->driver->channel_eq($chan1, $chan2);
64 }
65  
66 #
67 # ->datum_trace
68 #
69 # Emit a Carp::Datum trace, which will be a logwrite() on the 'debug' channel.
70 #
71 sub datum_trace {
72 my $self = shift;
73 my ($str, $tag) = @_;
74 require Carp::Datum;
75 Carp::Datum::trace($str, $tag);
76 }
77  
78 #
79 # intercept
80 #
81 # Intercept call to driver by calling ->datum_trace() first, then resume
82 # regular operation on the driver, if the channel where message would go
83 # is not the same as the debug channel.
84 #
85 sub intercept {
86 my ($aref, $tag, $op, $chan, $prepend) = @_;
87 my $self = shift @$aref;
88  
89 #
90 # $aref can be [$str] or [$offset, $str]
91 #
92  
93 my $pstr = $aref->[$#$aref]; # String is last argument
94 if (defined $prepend) {
95 $pstr = $pstr->clone; # We're prepending tag on a copy
96 $pstr->prepend("$prepend: ");
97 }
98 $self->datum_trace($pstr, $tag);
99 my $driver = $self->driver;
100 if ($driver->channel_eq('debug', $chan)) {
101 die "$pstr\n" if $prepend eq 'FATAL';
102 } else {
103 $driver->$op(@$aref);
104 }
105 }
106  
107 #
108 # Interface interception.
109 #
110 # The string will be tagged with ">>" to make it clear it comes from Log::Agent,
111 # unless it's a fatal string from logconfess/logcarp/logdie, in wich case
112 # it is tagged with "**".
113 #
114  
115 sub logconfess { intercept(\@_, '**', 'logconfess', 'error', 'FATAL') }
116 sub logxcroak { intercept(\@_, '**', 'logxcroak', 'error', 'FATAL') }
117 sub logdie { intercept(\@_, '**', 'logdie', 'error', 'FATAL') }
118 sub logerr { intercept(\@_, '>>', 'logerr', 'error', 'ERROR') }
119 sub logcluck { intercept(\@_, '>>', 'logcluck', 'error', 'WARNING') }
120 sub logwarn { intercept(\@_, '>>', 'logwarn', 'error', 'WARNING') }
121 sub logxcarp { intercept(\@_, '>>', 'logxcarp', 'error', 'WARNING') }
122 sub logsay { intercept(\@_, '>>', 'logsay', 'output') }
123 sub loginfo { intercept(\@_, '>>', 'loginfo', 'output') }
124 sub logdebug { intercept(\@_, '>>', 'logdebug', 'output') }
125  
126 #
127 # logwrite -- redefined
128 #
129 # Emit the message to the specified channel
130 #
131 sub logwrite {
132 my $self = shift;
133 my ($chan, $prio, $level, $str) = @_;
134  
135 #
136 # Have to be careful not to recurse through ->datum_trace().
137 # Look at who is calling us (immediate caller is Log::Agent).
138 #
139  
140 my $pkg = caller(1);
141 if ($pkg =~ /^Carp::Datum\b/) {
142 my $drv = $self->driver;
143 return unless defined $drv; # Can happen during global destruct
144 $drv->logwrite($chan, $prio, $level, $str);
145 return;
146 }
147  
148 #
149 # The following will recurse back to us, but the above check will
150 # cut the recursion.
151 #
152  
153 intercept([$self, $str], '>>', 'logwrite', $chan);
154 }
155  
156 __END__