PageRenderTime 76ms CodeModel.GetById 63ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/BackupManager/Dialog.pm

http://github.com/sukria/Backup-Manager
Perl | 64 lines | 49 code | 15 blank | 0 comment | 7 complexity | cce5448f7cb157d97988ca1e5ecd765e MD5 | raw file
 1package BackupManager::Dialog;
 2
 3use strict;
 4use warnings;
 5
 6use vars qw(@ISA @EXPORT);
 7@ISA = ('Exporter');
 8@EXPORT = qw(init_dialog print_info print_warning print_error);
 9
10use BackupManager::Logger;
11
12my $dialog_verbose = 0;
13
14sub should_log($) {
15    my ($level) = @_;
16
17    my $level_score = {
18        debug => 0,
19        info => 1,
20        warning => 2,
21        error => 3,
22    };
23
24    my $conf_level = $ENV{BM_LOGGER_LEVEL} || 'warning';
25    return $level_score->{$level} >= $level_score->{$conf_level};
26}
27
28sub init_dialog($)
29{
30    my ($verbose) = @_;
31    $dialog_verbose = $verbose if defined $verbose;
32}
33
34sub print_info
35{
36    my ($message) = @_;
37	$message = "" unless defined $message;
38    chomp $message;
39
40	info ($message) if should_log 'info';
41	print STDOUT $message."\n" if $dialog_verbose;
42}
43
44sub print_warning
45{
46    my ($message) = @_;
47	$message = "" unless defined $message;
48    chomp $message;
49
50	warning ($message) if should_log 'warning';
51	print STDERR $message."\n" if $dialog_verbose;
52}
53
54sub print_error
55{
56    my ($message) = @_;
57	$message = "" unless defined $message;
58    chomp $message;
59
60	error ($message) if should_log 'error';
61	print STDERR $message."\n";
62}
63
641;