Dispatchouli/Global.pm

filter: Statementstmt 79.3% 46 / 58 Branchbran 30.0% 3 / 10 Conditioncond 53.3% 8 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 87.5% 14 / 16 Podpod 83.3% 5 / 6 totaltotal 68.4% 78 / 114 CC 11 SCAR 15.9
CC
11
Cov
69%
_equiv
12.0
_build_logger
9.0
current_logger
6.0
CRAP 4.9
« Fmt.pm ↑ Summary Dispatchouli/Proxy.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Log::Dispatchouli::Global 3.101;
4 # ABSTRACT: a system for sharing a global, dynamically-scoped logger
5  
6 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Log::Dispatchouli;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Sub::Exporter::GlobExporter 0.002 qw(glob_exporter); # pass-through args
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 collectors => {
13 '$Logger' => glob_exporter(Logger => \'_build_logger'),
14 },
15 2 };
16  
17 #pod =head1 DESCRIPTION
18 #pod
19 #pod B<Warning>: This interface is still experimental.
20 #pod
21 #pod Log::Dispatchouli::Global is a framework for a global logger object. In your
22 #pod top-level programs that are actually executed, you'd add something like this:
23 #pod
24 #pod use Log::Dispatchouli::Global '$Logger' => {
25 #pod init => {
26 #pod ident => 'My::Daemon',
27 #pod facility => 'local2',
28 #pod to_stdout => 1,
29 #pod },
30 #pod };
31 #pod
32 #pod This will import a C<$Logger> into your program, and more importantly will
33 #pod initialize it with a new L<Log::Dispatchouli> object created by passing the
34 #pod value for the C<init> parameter to Log::Dispatchouli's C<new> method.
35 #pod
36 #pod Much of the rest of your program, across various libraries, can then just use
37 #pod this:
38 #pod
39 #pod use Log::Dispatchouli::Global '$Logger';
40 #pod
41 #pod sub whatever {
42 #pod ...
43 #pod
44 #pod $Logger->log("about to do something");
45 #pod
46 #pod local $Logger = $Logger->proxy({ proxy_prefix => "whatever: " });
47 #pod
48 #pod for (@things) {
49 #pod $Logger->log([ "doing thing %s", $_ ]);
50 #pod ...
51 #pod }
52 #pod }
53 #pod
54 #pod This eliminates the need to pass around what is effectively a global, while
55 #pod still allowing it to be specialized within certain contexts of your program.
56 #pod
57 #pod B<Warning!> Although you I<could> just use Log::Dispatchouli::Global as your
58 #pod shared logging library, you almost I<certainly> want to write a subclass that
59 #pod will only be shared amongst your application's classes.
60 #pod Log::Dispatchouli::Global is meant to be subclassed and shared only within
61 #pod controlled systems. Remember, I<sharing your state with code you don't
62 #pod control is dangerous>.
63 #pod
64 #pod =head1 USING
65 #pod
66 #pod In general, you will either be using a Log::Dispatchouli::Global class to get
67 #pod a C<$Logger> or to initialize it (and then get C<$Logger>). These are both
68 #pod demonstrated above. Also, when importing C<$Logger> you may request it be
69 #pod imported under a different name:
70 #pod
71 #pod use Log::Dispatchouli::Global '$Logger' => { -as => 'L' };
72 #pod
73 #pod $L->log( ... );
74 #pod
75 #pod There is only one class method that you are likely to use: C<current_logger>.
76 #pod This provides the value of the shared logger from the caller's context,
77 #pod initializing it to a default if needed. Even this method is unlikely to be
78 #pod required frequently, but it I<does> allow users to I<see> C<$Logger> without
79 #pod importing it.
80 #pod
81 #pod =head1 SUBCLASSING
82 #pod
83 #pod Before using Log::Dispatchouli::Global in your application, you should subclass
84 #pod it. When you subclass it, you should provide the following methods:
85 #pod
86 #pod =head2 logger_globref
87 #pod
88 #pod This method should return a globref in which the shared logger will be stored.
89 #pod Subclasses will be in their own package, so barring any need for cleverness,
90 #pod every implementation of this method can look like the following:
91 #pod
92 #pod sub logger_globref { no warnings 'once'; return \*Logger }
93 #pod
94 #pod =cut
95  
96 6 ▶sub logger_globref {
Subroutine logger_globref: called CC=1 SCAR=0.0
Pod documented
97 2 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 6 \*Logger;
99 }
100  
101 0 ▶sub current_logger {
Subroutine current_logger: not called CC=2 SCAR=17.9
Pod undocumented
102 0 my ($self) = @_;
103  
104 0 my $globref = $self->logger_globref;
105  
106 0 ▶ unless (defined $$$globref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$$globref) 0 0 0
107 0 $$$globref = $self->default_logger;
108 }
109  
110 0 return $$$globref;
111 }
112  
113 #pod =head2 default_logger
114 #pod
115 #pod If no logger has been initialized, but something tries to log, it gets the
116 #pod default logger, created by calling this method.
117 #pod
118 #pod The default implementation calls C<new> on the C<default_logger_class> with the
119 #pod result of C<default_logger_args> as the arguments.
120 #pod
121 #pod =cut
122  
123 7 ▶sub default_logger {
Subroutine default_logger: called CC=2 SCAR=7.0
Pod documented
124 7 my ($self) = @_;
125  
126 7 my $ref = $self->default_logger_ref;
127  
128 7 ▶ $$ref ||= $self->default_logger_class->new(
Condition66%2/3
$$ref ||= $self->default_logger_class->new($self->default_logger_args)
l!l&&r!l&&!r
430
MC/DC0%0/2
$$ref ||= $self->default_logger_class->new($self->default_logger_args)
$$ref $self->default_logger_class->new($self->default_logger_args)
Truth table66%2/3
A ||= B
A= $$refB= $self->default_logger_class->new($self->default_logger_args)
ABresult
1X1
011
000
129 $self->default_logger_args
130 );
131 }
132  
133 #pod =head2 default_logger_class
134 #pod
135 #pod This returns the class on which C<new> will be called when initializing a
136 #pod logger, either from the C<init> argument when importing or the default logger.
137 #pod
138 #pod Its default value is Log::Dispatchouli.
139 #pod
140 #pod =cut
141  
142 4 ▶sub default_logger_class { 'Log::Dispatchouli' }
Subroutine default_logger_class: called CC=1 SCAR=0.0
Pod documented
143  
144 #pod =head2 default_logger_args
145 #pod
146 #pod If no logger has been initialized, but something tries to log, it gets the
147 #pod default logger, created by calling C<new> on the C<default_logger_class> and
148 #pod passing the results of calling this method.
149 #pod
150 #pod Its default return value creates a sink, so that anything logged without an
151 #pod initialized logger is lost.
152 #pod
153 #pod =cut
154  
155 1 ▶sub default_logger_args {
Subroutine default_logger_args: called CC=1 SCAR=0.0
Pod documented
156 return {
157 1 ident => "default/$0",
158 facility => undef,
159 }
160 }
161  
162 #pod =head2 default_logger_ref
163 #pod
164 #pod This method returns a scalar reference in which the cached default value is
165 #pod stored for comparison. This is used when someone tries to C<init> the global.
166 #pod When someone tries to initialize the global logger, and it's already set, then:
167 #pod
168 #pod =for :list
169 #pod * if the current value is the same as the default, the new value is set
170 #pod * if the current value is I<not> the same as the default, we die
171 #pod
172 #pod Since you want the default to be isolated to your application's logger, the
173 #pod default behavior is default loggers are associated with the glob reference to
174 #pod which the default might be assigned. It is unlikely that you will need to
175 #pod interact with this method.
176 #pod
177 #pod =cut
178  
179 2 my %default_logger_for_glob;
180  
181 5 ▶sub default_logger_ref {
Subroutine default_logger_ref: called CC=1 SCAR=0.0
Pod documented
182 5 my ($self) = @_;
183  
184 5 my $glob = $self->logger_globref;
185 5 my $addr = Scalar::Util::refaddr($glob);
186 5 return \$default_logger_for_glob{ $addr };
187 }
188  
189 0 ▶sub _equiv {
Subroutine _equiv: not called CC=3 SCAR=24.8
190 0 my ($self, $x, $y) = @_;
191  
192 0 ▶ return 1 if Scalar::Util::refaddr($x) == Scalar::Util::refaddr($y);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($x) == builtin::refaddr($y) 0 0 0
193 0 ▶ return 1 if $x->config_id eq $y->config_id;
Branch: 1 branch
BranchTrueFalseTotal
if $x->config_id eq $y->config_id 0 0 0
194 return
195 0 }
196  
197 7 ▶sub _build_logger {
Subroutine _build_logger: called CC=7 SCAR=22.0
198 7 my ($self, $arg) = @_;
199  
200 7 my $globref = $self->logger_globref;
201 7 my $default = $self->default_logger;
202  
203 7 my $Logger = $$$globref;
204  
205 7 ▶ if ($arg and $arg->{init}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg and $arg->{'init'}) { } 1 6 7
Condition100%3/3
$arg and $arg->{"init"}
!ll&&!rl&&r
511
MC/DC100%2/2
$arg and $arg->{"init"}
$arg $arg->{"init"}
Truth table100%3/3
A and B
A= $argB= $arg->{"init"}
ABresult
0X0
100
111
206 1 my $new_logger = $self->default_logger_class->new($arg->{init});
207  
208 1 ▶ if ($Logger
Branch: 1 branch
BranchTrueFalseTotal
if ($Logger and not $self->_equiv($Logger, $new_logger) || $self->_equiv($Logger, $default)) 0 1 1
Condition16%1/6
$Logger and not $self->_equiv($Logger, $new_logger) || $self->_equiv($Logger, $default)
!ll&&!rl&&r
100
$self->_equiv($Logger, $new_logger) || $self->_equiv($Logger, $default)
l!l&&r!l&&!r
000
MC/DC0%0/3
$Logger and not $self->_equiv($Logger, $new_logger) || $self->_equiv($Logger, $default)
$Logger $self->_equiv($Logger, $new_logger) $self->_equiv($Logger, $default)
Truth table25%1/4
A and not(B || C)
A= $LoggerB= $self->_equiv($Logger, $new_logger)C= $self->_equiv($Logger, $default)
ABCresult
0XX0
11X0
1010
1001
209 and not(
210 $self->_equiv($Logger, $new_logger)
211 or
212 $self->_equiv($Logger, $default)
213 )
214 ) {
215 # We already set up a logger, so we'll check that our new one is
216 # equivalent to the old. If so, we'll keep the old, since it's good
217 # enough. If not, we'll raise an exception: you can't configure the
218 # logger twice, with different configurations, in one program!
219 # -- rjbs, 2011-01-21
220 0 my $old = $Logger->config_id;
221 0 my $new = $new_logger->config_id;
222  
223 0 Carp::confess(sprintf(
224 "attempted to initialize %s logger twice; old config %s, new config %s",
225 $self,
226 $old,
227 $new,
228 ));
229 }
230  
231 1 $$$globref = $new_logger;
232 } else {
233 6 ▶ $$$globref ||= $default;
Condition66%2/3
$$$globref ||= $default
l!l&&r!l&&!r
420
MC/DC0%0/2
$$$globref ||= $default
$$$globref $default
Truth table66%2/3
A ||= B
A= $$$globrefB= $default
ABresult
1X1
011
000
234 }
235  
236 7 return $globref;
237 }
238  
239 #pod =head1 COOKBOOK
240 #pod
241 #pod =head2 Common Logger Recipes
242 #pod
243 #pod Say you often use the same configuration for one kind of program, like
244 #pod automated tests. You've already written your own subclass to get your own
245 #pod storage and defaults, maybe C<MyApp::Logger>.
246 #pod
247 #pod You can't just write a subclass with a different default, because if another
248 #pod class using the same global has set the global with I<its> default, yours won't
249 #pod be honored. You don't just want this new value to be the default, you want it
250 #pod to be I<the> logger. What you want to do in this case is to initialize your
251 #pod logger normally, then reexport it, like this:
252 #pod
253 #pod package MyApp::Logger::Test;
254 #pod use parent 'MyApp::Logger';
255 #pod
256 #pod use MyApp::Logger '$Logger' => {
257 #pod init => {
258 #pod ident => "Tester($0)",
259 #pod to_self => 1,
260 #pod facility => undef,
261 #pod },
262 #pod };
263 #pod
264 #pod This will set up the logger and re-export it, and will properly die if anything
265 #pod else attempts to initialize the logger to something else.
266 #pod
267 #pod =cut
268  
269 2 1;
270  
271 __END__
« Fmt.pm ↑ Summary Dispatchouli/Proxy.pm »