File: Logger.pm

package info (click to toggle)
msva-perl 0.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 360 kB
  • sloc: perl: 2,220; sh: 110; makefile: 19
file content (102 lines) | stat: -rw-r--r-- 2,901 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#----------------------------------------------------------------------
# Monkeysphere Validation Agent, Perl version
# Marginal User Interface for reasonable prompting
# Copyright © 2010 Daniel Kahn Gillmor <dkg@fifthhorseman.net>,
#                  Matthew James Goins <mjgoins@openflows.com>,
#                  Jameson Graef Rollins <jrollins@finestructure.net>,
#                  Elliot Winard <enw@caveteen.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#----------------------------------------------------------------------

{ package Crypt::Monkeysphere::Logger;

  use strict;
  use warnings;

  # Net::Server log_level goes from 0 to 4
  # this is scaled to match.
  my %loglevels = (
                   'silent' => 0,
                   'quiet' => 0.25,
                   'fatal' => 0.5,
                   'error' => 1,
                   'info' => 2,
                   'verbose' => 3,
                   'debug' => 4,
                   'debug1' => 4,
                   'debug2' => 5,
                   'debug3' => 6,
                  );

  sub log {
    my $self = shift;
    my $msglevel = shift;

    $msglevel = 'error'
      if (! defined($msglevel));
    if ($loglevels{lc($msglevel)} <= $self->{loglevel}) {
      printf STDERR @_;
    }
  };

  sub get_log_level {
    my $self = shift;

    return $self->{loglevel};
  }
  sub set_log_level {
    my $self = shift;
    my $loglevel = shift;
    my $logval = $loglevels{lc($loglevel)};

    if (defined($logval)) {
      $self->{loglevel} = $logval;
    } else {
      $self->log('error', "Invalid log level: '%s' (log level not changed)\n", $loglevel);
    }
  }
  sub more_verbose {
    my $self = shift;
    my $increment = shift;

    $increment = 1
      if (!defined $increment);
    $self->{loglevel} += $increment;
  }

  # let the user test to see if we're noisier than this level
  # directly:
  sub is_logging_at {
    my $self = shift;
    my $qlevel = shift;

    return ($loglevels{lc($qlevel)} <= $self->{loglevel});
  }

  sub new {
    my $class = shift;
    my $loglevel = shift;

    my $self = {loglevel => $loglevels{defined($loglevel) ? lc($loglevel) : 'error'}};
    $self->{loglevel} = $loglevels{error}
      if (!defined $self->{loglevel});

    bless ($self, $class);
    return $self;
  }

  1;
}