aboutsummaryrefslogtreecommitdiff
path: root/modules/renrot_msg.pm
blob: 2b16f88a103392b5e6bdada67cbb2332b7b537f5 (plain)
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
use strict;

use Term::ANSIColor;

package msg;

sub new {
    my $obj = shift;
    my $use_color = shift;
    my $quiet = shift;
    my $verbose = shift;
    my %colors = (
	debug => {value => 'green'},
	error => {value => 'magenta'},
	fatal => {value => 'red'},
	info => {value => 'bold'},
	process => {value => 'white'},
	warning => {value => 'cyan'},
	);
    return (bless {use_color => $use_color,
		   quiet => $quiet,
		   verbose => $verbose,
		   colors => \%colors,
	    }, $obj);
    #$obj->Dbg(4,"msg class has been created.\n");
}
# destructor
sub DESTROY {
    my $obj = shift;
    #$obj->Dbg(4,"msg class has been destroied.\n");
}
# Prints colored message to STDERR
sub coloredprn {
    my $obj = shift;
    my $facility = shift;
    if ($obj->{use_color} != 0) {
	if (defined $facility and defined $obj->{colors}{$facility}) {
	    print STDERR Term::ANSIColor::colored [$obj->{colors}{$facility}{value}], @_;
	    return;
	}
    }
    print STDERR @_;	# fallback to normal print
}
# processing message
sub Proc {
    my $obj = shift;
    return if ($obj->{quiet} != 0);
    
    if ($obj->{use_color} != 0) {
	if (defined $obj->{colors}{'process'}) {
	    print Term::ANSIColor::colored [$obj->{colors}{'process'}{value}], @_;
	    return;
	}
  }
    print @_;		# fallback to normal print
}
# information message
sub Info {
    my $obj = shift;
    $obj->coloredprn('info', @_);
}
# warning message
sub Warn {
    my $obj = shift;
    $obj->coloredprn('warning', "Warning: ", @_);
}
# error message
sub Err {
    my $obj = shift;
    $obj->coloredprn('error', "ERROR: ", @_);
}
# fatal message
sub Fatal {
    my $obj = shift;
    $obj->coloredprn('fatal', "FATAL: ", @_);
}
# debug message
sub Dbg {
    my $obj = shift;
    my $level = shift;
    if ($obj->{verbose} >= $level) {
	$obj->coloredprn('debug', "DEBUG[$level]: ", @_);
    }
}

1;

Return to:

Send suggestions and report system problems to the System administrator.