package Err;

use strict;

require SendZ;

BEGIN {@main::Ids = (@main::Ids, '$Id: Err.pm,v 1.6 2001/11/25 06:35:25 ingolia Exp $ ')}

return 1;

BEGIN {
    $Err::error_mailto = 'ingolia@mit.edu';
}

# Report an error condition by mail, zephyr, and warn().
sub error {
    mail_error(@_);

    eval {
      SendZ::zdebug("ERROR\n" . error_msg(@_));
    } ;
}

# Report an error condition by mail and warn().  This is useful for
# reporting errors in zephyr sending, to avoid recursive error
# production.
sub mail_error {
    my $msg = error_msg(@_);

    warn($msg);

    open (MAILTO, "|mail -s \'Error\' $Err::error_mailto")
	or (warn("Cannot mail error to $Err::error_mailto"));

    print MAILTO $msg;
    print MAILTO join("\n", @mail::Ids);

    close (MAILTO)
	or ($! and warn('Error closing mail pipe', $msg, $!))
	    or warn('mail exited with error', $msg, $?);    
}

# Produce an error message from the arguments by joining the arguments
# as lines in an error string and adding the date, the hostname, and
# the process id as seperate lines.
sub error_msg {
    return join("\n", (@_, `date`, `hostname`, $$));
}
