ARCv2

 view release on metacpan or  search on metacpan

lib/Arc.pm  view on Meta::CPAN

{
	my $this = shift;
	my (%values) = @_;
	my $members = $this->members;

	while (my ($key,$val) = each(%$members)) {
		$this->{$key} = exists($values{$key}) ? $values{$key} : $val;
		delete $values{$key};
	}

	croak("Ignored values at object-creation (this is probably not what you want): ",join(" ",keys (%values))) if keys %values;
	
	# loglevel
	$this->{loglevel} = 4 if not defined $this->{loglevel};

	$this->{_syslog} = ! (defined $this->{logdestination} && $this->{logdestination} eq "stderr");

	openlog("arcv2","cons,pid","user") if $this->{_syslog};
	
	1;
}

## Debug function.
## Logs messages with "DEBUG" 
##in> ... (message)
##out> always false
##eg> $this->_Debug("hello","world"); # message will be "hello world"
sub _Debug
{
	my $this = shift;
	$this->Log(LOG_DEBUG,@_);
}

## Log function.
## Logs messages to 'logdestination' if 'loglevel' is is set appropriatly.
## loglevel behaviour has changed in the 1.0 release of ARCv2, the "Arc"-class can export
## LOG_AUTH (authentication information), LOG_USER (connection information), LOG_ERR (errors), 
## LOG_CMD (ARCv2 addition internal command information), LOG_SIDE (verbose client/server-specific
## information), LOG_DEBUG (verbose debug information). It possible to combine the 
## levels with or (resp. +) to allow a message to appear when not all loglevels are 
## requested by the user.
## Commonly used for logging errors from application level.
##in> $facility, ... (message)
##out> always false
##eg> return $arc->Log(LOG_ERR,"Message");
sub Log
{
	my $this = shift;
	my $pr = shift;
	my $ll = $this->{loglevel};
	my $lev = 1;
	my @syslog_arr = ('err','info','debug');
	
	$lev = 0 if $pr & LOG_ERR;
	$lev = 2 if $pr & LOG_DEBUG;

	if ($pr & $this->{loglevel}) {
		if ($this->{_syslog}) {
			syslog $syslog_arr[$lev], $this->{logfileprefix}." ".join(" ",@_);
		} else {
			print STDERR "[",$syslog_arr[$lev],"]: (",$this->{logfileprefix},") ",join(" ",@_),"\n";
		}
	}
	return;
}

## SetError function.
## This function prepends the error message (@_) to an existing error message (if any) and
## logs the message with LOG_ERR facility.
## Use this function for setting an error from class level. Users should use IsError 
## to get the message if a function failed.
##in> ... (message) 
##out> always false
##eg> return $this->_SetError("User is not allowed to do this."); # breaks when an error occured
sub _SetError
{
	my $this = shift;
	$this->Log(LOG_ERR,@_);
	
	my $errstr = "";
	if ($this->{_error}) {
		$errstr = ' maybe caused by: '.$this->{_error};
	}
	unless (@_) {
		$errstr .= 'Error, but no message.';
	} else {
		$errstr = join(" ",@_).$errstr ;
	}
	$errstr =~ s/\r//g;
	$errstr =~ s/\n/ /g;
	$this->{_error} = $errstr;
	return;
}

## User function to get the error msg.
##out> the error message if any otherwise undef
##eg> unless (my $err = $arc->IsError()) { .. } else { print STDERR $err; }
sub IsError
{
	my $this = shift;
	my $ret = $this->{_error};
	
	$this->{_error} = undef;
	
	return $ret;
}

## Destructor
sub DESTROY {
	my $this = shift;
	closelog() if $this->{_syslog};
}

1;



( run in 0.588 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )