hotsanic |
Subversion Repositories: |
Compare with Previous - Blame - Download
#
# $Id: HotSaNIClog.pm,v 1.8 2004/07/12 09:55:10 bernisys Exp $
#
package HotSaNIClog;
my @Weekday=("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
my $MODULE="(unknown)";
my $TIMESTAMP=0;
my $DEBUGLEVEL=0;
my $SECTION="(unknown)";
my %DEBUG_LEVELS=(
# main debugging (rrdtimer - 10 levels usable)
MAIN_HEARTBEAT=>1,
MAIN_SCAN_MODULES=>2,
MAIN_SCAN_MODULES_VERBOSE=>4,
MAIN_SIGNAL=>8,
MAIN_SIGNAL_VERBOSE=>16,
MAIN_CALL_SCRIPT=>32,
MAIN_CONFIG=>64,
MAIN_VERBOSE=>128,
MAIN_USER_2=>256,
MAIN_USER_3=>512,
# module debugging
MODULE_FORK=>1024,
MODULE_SAMPLING=>2048,
MODULE_DB_UPDATE=>4096,
MODULE_VERBOSE=>8192,
MODULE_CONFIG=>16384,
MODULE_USER_1=>32768,
MODULE_USER_2=>65536,
MODULE_USER_3=>131072,
# SNMP stuff
SNMP_VERBOSE=>262144,
SNMP_CONFIG=>524288,
SNMP_RESULTS=>1048576,
SNMP_USER_1=>2097152,
SNMP_USER_2=>4194304,
SNMP_USER_3=>8388608,
# custom levels
MAIN=>1023,
NOSNMP=>262143-1023,
SNMP=>16777215-262143,
MODULE=>16777215-1023,
VERBOSE=>128+8192,
FULL=>16777215
);
my %DEBUG_HELP=(
# main debugging (rrdtimer - 10 levels usable)
MAIN_HEARTBEAT=>"rrdtimer.pl will log the heartbeat interval (\"main loop running\")",
MAIN_SCAN_MODULES=>"rrdtimer.pl will log the calling of the \"call_modules\" subroutine",
MAIN_SCAN_MODULES_VERBOSE=>"verbose reporting of module starts",
MAIN_SIGNAL=>"log module signalling process",
MAIN_SIGNAL_VERBOSE=>"verbose logging of module signalling process",
MAIN_CALL_SCRIPT=>"log calling of helper scripts (makeindex, diagrams, convert)",
MAIN_CONFIG=>"show main configuration hash on startup",
MAIN_VERBOSE=>"verbose rrdtimer reporting",
MAIN_USER_2=>"",
MAIN_USER_3=>"",
# module debugging
MODULE_FORK=>"module's forking process",
MODULE_SAMPLING=>"give a terse overview about what happens while sampling",
MODULE_DB_UPDATE=>"show DB update values",
MODULE_VERBOSE=>"verbose module reporting",
MODULE_CONFIG=>"show module configuration on startup",
MODULE_USER_1=>"",
MODULE_USER_2=>"",
MODULE_USER_3=>"",
# SNMP stuff
SNMP_VERBOSE=>"verbose SNMP debugging",
SNMP_CONFIG=>"show SNMP configuration",
SNMP_RESULTS=>"show results of SNMP query",
SNMP_USER_1=>"",
SNMP_USER_2=>"",
SNMP_USER_3=>"",
# custom levels
MAIN=>"full rrdtimer debugging",
NOSNMP=>"module debugging without SNMP",
SNMP=>"full SNMP debugging",
MODULE=>"full module debugging",
VERBOSE=>"verbose reporting",
FULL=>"full debugging"
);
($VERSION = '$Revision: 1.8 $') =~ s/.*(\d+\.\d+).*/$1/;
sub set_modulename { $MODULE=shift || "(unknown)"; }
sub check_debuglevel {
my $which=shift || 0;
my $method=shift || "and";
my $result=0;
my $mask=16777215;
if ($MODULE =~ /^[A-Z]+$/) { $mask=$DEBUG_LEVELS{MODULE}; }
elsif ($MODULE =~ /^[a-z]+$/) { $mask=$DEBUG_LEVELS{MAIN}; }
my $level=(get_debuglevel($which) & $mask);
my $local=($DEBUGLEVEL & $mask);
return 1 if ( $level > 0) && (($level & $local) == $level);
return 0;
}
sub set_debuglevel {
$DEBUGLEVEL=shift || 0;
if (HotSaNIClog::check_debuglevel("MAIN_CONFIG,MODULE_CONFIG")) {
foreach (sort keys %DEBUG_LEVELS) {
if (HotSaNIClog::check_debuglevel($_)) { HotSaNIClog::info("DEBUG: $_"," $DEBUG_HELP{$_}"); }
}
}
}
sub get_debuglevel {
my $items=shift || 0;
my $level=0;
@ITEMS=split /,/,$items;
foreach my $item (@ITEMS) {
my $method="";
if ($item =~ s/^-(.*)/$1/) { $method="not"; }
if (defined $DEBUG_LEVELS{$item}) { $item=$DEBUG_LEVELS{$item}; }
if ($item =~ /^[0-9]+$/) {
if ($method eq "not") { $level &= (16777215-$item); }
else { $level |= $item; }
}
}
return $level;
}
sub set_timestamping {
$TIMESTAMP=shift;
if (!defined $TIMESTAMP) { $TIMESTAMP=0; }
elsif ($TIMESTAMP eq "") { $TIMESTAMP=0; }
elsif ($TIMESTAMP < 0) { $TIMESTAMP=0; }
elsif ($TIMESTAMP > 2) { $TIMESTAMP=0; }
}
######################################################################
#
sub info {
for $line (@_) {
print_entry(": $line");
}
}
######################################################################
#
sub warn {
$message=shift;
if (defined $message) {
print_entry(" warning: $message");
}
if (@_) { info(@_); }
}
######################################################################
#
sub error {
$message=shift;
if (defined $message) {
print_entry(" error: $message");
}
if (@_) { info(@_); }
}
sub print_entry {
my $line=shift || "";
if ($TIMESTAMP>0) {
if ($TIMESTAMP==1) { print "\[".time."\] - "; }
else {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year+=1900;
$mon++;
if ($mon<10) { $mon="0".$mon; }
if ($mday<10) { $mday="0".$mday; }
if ($hour<10) { $hour="0".$hour; }
if ($min<10) { $min="0".$min; }
if ($sec<10) { $sec="0".$sec; }
print "\[$Weekday[$wday] $year-$mon-$mday $hour:$min:$sec\] - ";
}
}
print "$MODULE " if ($MODULE ne "(unknown)") and ($MODULE ne "");
print "[$$]";
print "($SECTION) " if ($SECTION ne "(unknown)") and ($SECTION ne "");
print "$line\n";
}
1;