Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write "die" output to a logfile 2

Status
Not open for further replies.

netman4u

Technical User
Mar 16, 2005
176
US
Hello all,

My question is say I have die code like:

Code:
open (LOG, ">$log_file") or die ("Error opening log file $1");

or

die $sth->errstr if $sth->err;

or 

$scp->cwd("/tmp") or die $scp->{errstr};

[/code}

How do I write the "die" output to a file handle instead of STDERR or both?

Thanks,

Nick

If at first you don't succeed, don't try skydiving.
 
I'd suggest writing a subroutine to handle your error reporting instead of rerouting die to a file handle. That way you can add in other functions such as debug mode, e-mailing of errors when they occur, etc. very easily.

Code:
sub keelover {
  my $error = shift;
  open (FILE, ">/path/to/error.log");
  print FILE $error;
  close (FILE);
}

open (LOG, ">$log_file") or keelover("Error opening log file $1");

or

keelover($sth->errstr) if $sth->err;

or

$scp->cwd("/tmp") or keelover($scp->{errstr});

- George
 
Thanks for the reply Rieekan but it does not seem to be working:

example:
Code:
sub ScpFile
{
	##########################################################################################
	#
	#  This subroutine sftps the output file.
	#
	##########################################################################################
	my $file = shift(@_);
	print "$file\n";
	my $scp;
	my $source = "/opt/app/CLI_DAE/log";
	my $destination = "/tmp";
	$scp = Net::SCP->new("90.168.72.193");
	$scp->login("fhtw5x8") or [blue]ErrorOut[/blue] $scp->{errstr};
	$scp->cwd("/tmp") or ErrorOut $scp->{errstr};
	#$scp->size("file");
	$scp->put("/opt/app/CLI_DAE/log/CLI_log.txt") or ErrorOut $scp->{errstr};
	$scp->quit or [blue]ErrorOut[/blue] $scp->{errstr};
} # End sub SftpFile


sub VistaQuery 
{
	##########################################################################################
	#
	#  This subroutine Queries the InfoVista DB and builds an ip to device type to vendor HoH 
	#  for all the devices we will be querying.
	#
	##########################################################################################
	my $sub_vendor;
	my $sub_ip;
	my $sub_dev_type;
	my %sub_db_results;
	my $data_source = "dbi:Oracle:binvdp01";
	my $dbh = DBI->connect($data_source, $orauser, $orapass)
		  or [blue]ErrorOut[/blue] "Can't connect to $data_source: $DBI::errstr";
	my $sth = $dbh->prepare( q{
		select T1.SVALUE, T2.SVALUE, T3.SVALUE from twtpropvalues T1, twtpropvalues T2, 
		twtpropvalues T3 where T1.PROPID=(select ID from twtproperties 
		where WID='00000000000000000000000000000001') and T2.PROPID=
		(select ID from twtproperties where WID='72B1577C795AB24888F4462B18D8F42D') 
		and T3.PROPID=(select ID from twtproperties where WID='069003638BAFDA11BCB000166F0D3E5D') 
		and T1.INSID=T2.INSID and T2.INSID=T3.INSID
	}
	) or [blue]ErrorOut[/blue] "Can't prepare statement: $DBI::errstr";
	my $rc = $sth->execute
		or [blue]ErrorOut[/blue] "Can't execute statement: $DBI::errstr";
	print LOG "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
	print LOG "Field names: @{ $sth->{NAME} }\n";
	# 
	# Load query results into a HoH.
	#
	if ($debug ==1) {print LOG "Query results returned:\n\n";}
	while (($sub_ip, $sub_vendor, $sub_dev_type) = $sth->fetchrow_array) {
		if ($debug ==1) {print LOG "$sub_ip\t$sub_vendor\t$sub_dev_type\n";}
		$sub_db_results{$sub_vendor}->{$sub_dev_type} = $sub_ip; 
	}
	#
	# search for problems which may have terminated the fetch early, disconnect and return the HoH.
	#
	ErrorOut $sth->errstr if $sth->err;
	$dbh->disconnect;
	print LOG "\nInfoVista query completed successfully.\n\n"; 
	return (%sub_db_results);
} # End sub VistaQuery

Here is the output:

Code:
String found where operator expected at /opt/app/CLI_DAE/bin/bpBellSouthCoyote_Beta_10_11.pl line 243, near "ErrorOut "Can't connect to $data_source: $DBI::errstr""
	(Do you need to predeclare ErrorOut?)
String found where operator expected at /opt/app/CLI_DAE/bin/bpBellSouthCoyote_Beta_10_11.pl line 252, near "ErrorOut "Can't prepare statement: $DBI::errstr""
	(Do you need to predeclare ErrorOut?)
String found where operator expected at /opt/app/CLI_DAE/bin/bpBellSouthCoyote_Beta_10_11.pl line 254, near "ErrorOut "Can't execute statement: $DBI::errstr""
	(Do you need to predeclare ErrorOut?)
syntax error at /opt/app/CLI_DAE/bin/bpBellSouthCoyote_Beta_10_11.pl line 243, near "ErrorOut "Can't connect to $data_source: $DBI::errstr""
syntax error at /opt/app/CLI_DAE/bin/bpBellSouthCoyote_Beta_10_11.pl line 252, near "ErrorOut "Can't prepare statement: $DBI::errstr""
syntax error at /opt/app/CLI_DAE/bin/bpBellSouthCoyote_Beta_10_11.pl line 254, near "ErrorOut "Can't execute statement: $DBI::errstr""
Execution of /opt/app/CLI_DAE/bin/bpBellSouthCoyote_Beta_10_11.pl aborted due to compilation errors.

If at first you don't succeed, don't try skydiving.
 
Here's my sub:

Code:
sub ErrorOut 
{
	##########################################################################################
	#
	#  This subroutine write all STDERR to the log file.
	#
	##########################################################################################
	my $error = shift;
	print LOG "$error\n";
} # End sub ErrorOut

If at first you don't succeed, don't try skydiving.
 
Ok, figured it out. Just needed to add () after the ErrorOut.

If at first you don't succeed, don't try skydiving.
 
Rieekan don't you need an exit in your sub to simulate the die command?

Code:
sub keelover {
  my $error = shift;
  open (FILE, ">/path/to/error.log");
  print FILE $error;
  close (FILE);
[red]  exit 1;[/red]
}

If at first you don't succeed, don't try skydiving.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top