#-------------------------------------
# PRAGMAS
use lib 'f:/projects/site/bin/config/';
use strict;
use utf8;
#-------------------------------------
# IN HOUSE MODULES
use csConfig;
use csLogsConfig qw( csErrorLog csLogMaster );
use csLogs::Error;
use csLogs::Event;
use csDB qw( QueryDB SafeQuote );
use csMail;
#=====================================================================================
my $entries = 0;
my( %severities,%classes ) = ();
if (-f &csErrorLog)
# file exists and needs to be processed
{
my $tmp_logfile = csPath('logs').'/errors'.time().'.log';
my $LOG = undef;
#---------------------------------
# renaming the file assures that the processing
# this script does doesn't interfere with normal logging.
... rename and open the renamed file ...
#---------------------------------
# process the entries
$entries = process_errorlog($LOG,\%severities,\%classes);
... close and unlink temporary file, note that process ran successfully ...
}
else
# file doesn't exist and no processing was necessary
{
... note that process ran but had nothing to do ...
}
... email summary of results and end ...
#================================================================================================
sub process_errorlog
{
my ($LOG,$count_severities,$count_classes) = @_;
my $entries = 0; # track num of entries processed.
my $state = 'none'; #state of the parser;
my ( $time, $class, $severity, $message, $debug ) = ();
#---------------------------------
# walk the file
while( <$LOG> )
{
/\[Error\]/ and do { if( $time ) # time gets set later in the parsing...
{
#add to db;
THIS LINE> QueryDB("insert into csEvents (Time_Logged,Class,SubClass,Importance,Message,Info)".
" values ('$time',1,$class,$severity,".SafeQuote($message).",".SafeQuote($debug).")"
);
$entries++;
#reset variables;
( $time, $class, $severity, $message, $debug ) = ();
}
$state = 'error';
next;
};
... more wholesome parsing goodness ...
}
# flush out last record
if( $time )
{
#add to db;
THIS LINE> QueryDB("insert into csEvents (Time_Logged,Class,SubClass,Importance,Message,Info)".
" values ('$time',1,$class,$severity,".SafeQuote($message).",".SafeQuote($debug).")"
);
$entries++;
}
return $entries;
}
... other subs ...
####
#-------------------------------------
# PACKAGE INFO
package csDB;
$VERSION = .6;
use Exporter ();
@ISA = qw(Exporter);
@EXPORT_OK = qw(QueryDB SafeQuote SetLongLen GetLongLen $dbh);
#-------------------------------------
# PRAGMAS
use lib 'f:/projects/site/bin/config/';
use strict;
use utf8;
#-------------------------------------
# 3RD PARTY MODULES
use DBI;
#-------------------------------------
# IN HOUSE MODULES
use csConfig;
use csLogs::Error;
our $dbh;
#==========================================================================================================
# Database Connection
BEGIN {
$dbh = DBI->connect(csDBAuth()) or
&csDie( class=>&csErrorClass('SERVER_DATABASE'),severity=>&csErrorScale('CRITICAL'),
message=>'Could not connect to database server',
debug=>$DBI::errstr
);
}
END {
$dbh->disconnect();
}
#==========================================================================================================
# Basic query, returns a statement handle
sub QueryDB
{
$dbh->{RaiseError}=0;
$dbh->{PrintError}=0;
my $qstring = $_[0];
my $dbquery= $dbh->prepare("$qstring") or
&csDie( class=>&csErrorClass('CODE_DATABASE'),severity=>&csErrorScale('SEVERE'),
message=>'Database Query Preparation Error',
debug=>$DBI::errstr."\n$qstring"
);
$dbquery->execute or
&csDie( class=>&csErrorClass('CODE_DATABASE'),severity=>&csErrorScale('SEVERE'),
message=>'Database Query Execution Error',
debug=>$DBI::errstr."\n$qstring"
);
$dbh->{RaiseError}=1;
$dbh->{PrintError}=1;
return ($dbquery);
}
... more useful subs ...
#----
1;
####
#-------------------------------------
# PACKAGE INFO
package Error;
$VERSION = .3;
use Exporter ();
@ISA = qw(Exporter);
@EXPORT = qw( csWarn csDie csErrorClass csErrorScale ErrorScaleName ErrorClassName);
#-------------------------------------
# PRAGMAS
use lib 'f:/projects/site/bin/config/';
use strict;
use utf8;
#-------------------------------------
# IN HOUSE MODULES
use csLogsConfig qw( csErrorLog );
#==================================================
#Error Constants
my %ErrorClasses = ( ... );
my %ErrorScale = ( ... );
#--------------------------------------------------
# Accessors
sub csErrorClass { return $ErrorClasses{$_[0]}; }
sub ErrorClassName
{
foreach my $key ( keys %ErrorClasses )
{
return $key if $ErrorClasses{$key} == $_[0];
}
return 'UNKNOWN';
}
sub csErrorScale { return $ErrorScale{$_[0]}; }
sub ErrorScaleName
{
foreach my $key ( keys %ErrorScale )
{
return $key if $ErrorScale{$key} == $_[0];
}
return 'UNKNOWN';
}
#==================================================
# Replacements for User-called warn and die
...where we define csWarn and csDie...
#==================================================
# Private Functions
... more yummy subs ...
#==================================================
#Overload SIGWARN and SIGDIE
... to behave like csWarn and csDie with default values, wrapped in BEGIN ...
#----
1;