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;