1#!/usr/bin/perl
2package JMX::Jmx4Perl::Agent::Jolokia::Logger;
3
4use vars qw($HAS_COLOR);
5use strict;
6
7=head1 NAME
8
9JMX::Jmx4Perl::Agent::Jolokia::Logger - Simple logging abstraction for the
10Jolokia agent manager
11
12=head1 DESCRIPTION
13
14Simple Logger used throughout 'jolokia' and its associated modules for
15output. It knows about coloring and a quiet mode, where no output is generated
16at all.
17
18=cut
19
20BEGIN {
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
31Creates a logger. Dependening on the options (C<quiet> and C<color>) output can
32be supressed completely or coloring can be used. Coloring only works, if the
33Module L<Term::ANSIColor> is available (which is checked during runtime).
34
35=cut
36
37sub 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
53Debug output
54
55=cut 
56
57sub debug {
58    my $self = shift;
59    if ($self->{debug}) {
60        print "+ ",join("",@_),"\n";
61    }
62}
63
64=item $log->info("....","[em]","....","[/em]",...);
65
66Info output. The tag "C<[em]>" can be used to higlight a portion of the
67output. The tag must be provided in an extra element in the given list.
68
69=cut 
70
71
72sub 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
81Warning output (printed in yellow)
82
83=cut 
84
85
86sub 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
95Error output (printed in red)
96
97=cut 
98
99
100sub 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
107sub _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
123This file is part of jmx4perl.
124Jmx4perl is free software: you can redistribute it and/or modify
125it under the terms of the GNU General Public License as published by
126The Free Software Foundation, either version 2 of the License, or
127(at your option) any later version.
128
129jmx4perl is distributed in the hope that it will be useful,
130but WITHOUT ANY WARRANTY; without even the implied warranty of
131MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132GNU General Public License for more details.
133
134You should have received a copy of the GNU General Public License
135along with jmx4perl.  If not, see <http://www.gnu.org/licenses/>.
136
137A commercial license is available as well. Please contact roland@cpan.org for
138further details.
139
140=head1 AUTHOR
141
142roland@cpan.org
143
144=cut
145
146
147package JMX::Jmx4Perl::Agent::Jolokia::Logger::None;
148use base qw(JMX::Jmx4Perl::Agent::Jolokia::Logger);
149
150=head1 NAME
151
152JMX::Jmx4Perl::Agent::Jolokia::Logger::None - No-op logger
153
154=head1 DESCRIPTION
155
156No-op logger used when quiet mode is switched on. Doesn't print
157out anything.
158
159=cut
160
161sub info { }
162sub warn { }
163sub error { }
164sub debug { }
165
166
1671;
168