package Modules::ErrDbg;

########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

BEGIN {
	use Exporter ();
	@ISA = qw(Exporter);
	@EXPORT_OK = qw(&MySQLEvent &hard_error &soft_error &error);
}

use strict "vars";
# Log errors in db
#

sub MySQLEvent{
	my ($code, $message, $anbieter, $kunde) = @_;
	if($main::scriptdebug){
		my $subname = (caller(0))[3];
		print "SUB: $subname ($message)\n";
	}
	my $timestamp = time();
	if(!(defined $main::anbieter)){
	$anbieter = "";
	$kunde = "";
	}
	elsif(!(defined $main::kunde)){
	$kunde = "";
	}
	#$message = &charCruncher($message, 30);
	$main::dbh->do("INSERT INTO ereignisse (timestamp, nachricht, code, script, anbieter, kunde) VALUES (?, ?, ?, 'Update-Script', ?, ?)", undef, $timestamp, $message, $code, $anbieter, $kunde) 
		or die("error: $DBI::errstr");
	$main::ereignisse = 1;
}

sub hard_error{
	if($main::scriptdebug){
		my $subname = (caller(0))[3];
		print "SUB: $subname\n";
	}
	my ($message) = @_;
	&MySQLEvent(1, $message);
	die($message);
}


sub soft_error{
	if($main::scriptdebug){
		my $subname = (caller(0))[3];
		print "SUB: $subname\n";
	}
	my ($message, $anbieter, $kunde) = @_;
	if(!(defined $anbieter)){
		$anbieter = "";
		$kunde = "";
	}
	elsif(!(defined $kunde)){
		$kunde = "";
	}
	&MySQLEvent(2, $message, $anbieter, $kunde);
}

sub error {
	my ($severity, $code, $desc) = @_;
	warn "Sev:$severity, Code:$code, $desc\n";
	return 0;
}

return 1;

END {}
