return 100 from msva-query-agent if unable to contact msva
[monkeysphere-validation-agent.git] / Crypt / Monkeysphere / Logger.pm
1 #----------------------------------------------------------------------
2 # Monkeysphere Validation Agent, Perl version
3 # Marginal User Interface for reasonable prompting
4 # Copyright © 2010 Daniel Kahn Gillmor <dkg@fifthhorseman.net>,
5 #                  Matthew James Goins <mjgoins@openflows.com>,
6 #                  Jameson Graef Rollins <jrollins@finestructure.net>,
7 #                  Elliot Winard <enw@caveteen.com>
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22 #----------------------------------------------------------------------
23
24 { package Crypt::Monkeysphere::MSVA::Logger;
25
26   use strict;
27   use warnings;
28
29   # Net::Server log_level goes from 0 to 4
30   # this is scaled to match.
31   my %loglevels = (
32                    'silent' => 0,
33                    'quiet' => 0.25,
34                    'fatal' => 0.5,
35                    'error' => 1,
36                    'info' => 2,
37                    'verbose' => 3,
38                    'debug' => 4,
39                    'debug1' => 4,
40                    'debug2' => 5,
41                    'debug3' => 6,
42                   );
43
44   sub log {
45     my $self = shift;
46     my $msglevel = shift;
47
48     if ($loglevels{lc($msglevel)} <= $self->{loglevel}) {
49       printf STDERR @_;
50     }
51   };
52
53   sub get_log_level {
54     my $self = shift;
55
56     return $self->{loglevel};
57   }
58   sub set_log_level {
59     my $self = shift;
60     my $loglevel = shift;
61     my $logval = $loglevels{lc($loglevel)};
62
63     if (defined($logval)) {
64       $self->{loglevel} = $logval;
65     } else {
66       $self->log('error', "Invalid log level: '%s' (log level not changed)\n", $loglevel);
67     }
68   }
69   sub more_verbose {
70     my $self = shift;
71     my $increment = shift;
72
73     $increment = 1
74       if (!defined $increment);
75     $self->{loglevel} += $increment;
76   }
77
78   # let the user test to see if we're noisier than this level
79   # directly:
80   sub is_logging_at {
81     my $self = shift;
82     my $qlevel = shift;
83
84     return ($loglevels{lc($qlevel)} <= $self->{loglevel});
85   }
86
87   sub new {
88     my $class = shift;
89     my $loglevel = shift;
90
91     my $self = {loglevel => $loglevels{lc($loglevel)}};
92     $self->{loglevel} = $loglevels{error}
93       if (!defined $self->{loglevel});
94
95     bless ($self, $class);
96     return $self;
97   }
98
99   1;
100 }