Jmx4Perl/Agent/Jolokia/Logger.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 93 CC 11 SCAR 23.4
CC
11
Cov
0%
_resolve_color
30.0
new
12.0
debug
6.0
CRAP 10.4
1 #!/usr/bin/perl
2 package JMX::Jmx4Perl::Agent::Jolokia::Logger;
3  
4 use vars qw($HAS_COLOR);
5 use strict;
6  
7 =head1 NAME
8  
9 JMX::Jmx4Perl::Agent::Jolokia::Logger - Simple logging abstraction for the
10 Jolokia agent manager
11  
12 =head1 DESCRIPTION
13  
14 Simple Logger used throughout 'jolokia' and its associated modules for
15 output. It knows about coloring and a quiet mode, where no output is generated
16 at all.
17  
18 =cut
19  
20 BEGIN {
21 $HAS_COLOR = eval "require Term::ANSIColor; Term::ANSIColor->import(qw(:constants)); 1";
22 }
23  
24  
25 =head1 METHODS
26  
27 =over 4
28  
29 =item $logger = JMX::Jmx4Perl::Agent::Jolokia::Logger->new(quiet=>1,color=>1)
30  
31 Creates a logger. Dependening on the options (C<quiet> and C<color>) output can
32 be supressed completely or coloring can be used. Coloring only works, if the
33 Module L<Term::ANSIColor> is available (which is checked during runtime).
34  
35 =cut
36  
37 sub new {
38 my $class = shift;
39 my $self = ref($_[0]) eq "HASH" ? $_[0] : { @_ };
40  
41 my $quiet = delete $self->{quiet};
42 $HAS_COLOR &&= $self->{color};
43
44 # No-op logger
45 return new JMX::Jmx4Perl::Agent::Jolokia::Logger::None
46 if $quiet;
47  
48 bless $self,(ref($class) || $class);
49 }
50  
51 =item $log->debug("....");
52  
53 Debug output
54  
55 =cut
56  
57 sub debug {
58 my $self = shift;
59 if ($self->{debug}) {
60 print "+ ",join("",@_),"\n";
61 }
62 }
63  
64 =item $log->info("....","[em]","....","[/em]",...);
65  
66 Info output. The tag "C<[em]>" can be used to higlight a portion of the
67 output. The tag must be provided in an extra element in the given list.
68  
69 =cut
70  
71  
72 sub info {
73 my $self = shift;
74 my $text = $self->_resolve_color(@_);
75 my ($cs,$ce) = $HAS_COLOR ? (DARK . CYAN,RESET) : ("","");
76 print $cs . "*" . $ce . " " . $text . "\n";
77 }
78  
79 =item $log->warn(...)
80  
81 Warning output (printed in yellow)
82  
83 =cut
84  
85  
86 sub warn {
87 my $self = shift;
88 my $text = join "",@_;
89 my ($cs,$ce) = $HAS_COLOR ? (YELLOW,RESET) : ("","");
90 print $cs. "! " . $text . $ce ."\n";
91 }
92  
93 =item $log->warn(...)
94  
95 Error output (printed in red)
96  
97 =cut
98  
99  
100 sub error {
101 my $self = shift;
102 my $text = join "",@_;
103 my ($cs,$ce) = $HAS_COLOR ? (RED,RESET) : ("","");
104 print $cs . $text . $ce . "\n";
105 }
106  
107 sub _resolve_color {
108 my $self = shift;
109 return join "",map {
110 if (lc($_) eq "[em]") {
111 $HAS_COLOR ? GREEN : ""
112 } elsif (lc($_) eq "[/em]") {
113 $HAS_COLOR ? RESET : ""
114 } else {
115 $_
116 }} @_;
117 }
118  
119 =back
120  
121 =head1 LICENSE
122  
123 This file is part of jmx4perl.
124 Jmx4perl is free software: you can redistribute it and/or modify
125 it under the terms of the GNU General Public License as published by
126 The Free Software Foundation, either version 2 of the License, or
127 (at your option) any later version.
128
129 jmx4perl is distributed in the hope that it will be useful,
130 but WITHOUT ANY WARRANTY; without even the implied warranty of
131 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
132 GNU General Public License for more details.
133
134 You should have received a copy of the GNU General Public License
135 along with jmx4perl. If not, see <http://www.gnu.org/licenses/>.
136  
137 A commercial license is available as well. Please contact roland@cpan.org for
138 further details.
139  
140 =head1 AUTHOR
141  
142 roland@cpan.org
143  
144 =cut
145  
146  
147 package JMX::Jmx4Perl::Agent::Jolokia::Logger::None;
148 use base qw(JMX::Jmx4Perl::Agent::Jolokia::Logger);
149  
150 =head1 NAME
151  
152 JMX::Jmx4Perl::Agent::Jolokia::Logger::None - No-op logger
153  
154 =head1 DESCRIPTION
155  
156 No-op logger used when quiet mode is switched on. Doesn't print
157 out anything.
158  
159 =cut
160  
161 sub info { }
162 sub warn { }
163 sub error { }
164 sub debug { }
165  
166  
167 1;