mariadb/Build-tools/logger.pm
unknown baa0238c94 - Heavily reworked the Do-pkg script to use functions from logger.pm
- moved helper functions from Bootstrap script to logger.pm 


Build-tools/Bootstrap:
   - renamed logfile -> LOGFILE
   - moved functions abort() and run_command() to logger.pm for better
     reusability
Build-tools/Do-pkg:
   - heavily reworked: now make use of the logger.pm helper functions
   - added new options: --dry-run, --log, --mail, --verbose
Build-tools/logger.pm:
   - added functions run_command() and abort() from Bootstrap script for
     better code reusability
support-files/MacOSX/postinstall.sh:
   - made script more robust
2003-02-25 15:20:07 +01:00

106 lines
1.9 KiB
Perl

# Helper functions
#
# Create a log entry
#
sub logger
{
my $message=$_[0];
print timestamp() . " " . $message . "\n" if $opt_verbose;
if (defined $opt_log && !$opt_dry_run)
{
open LOG, ">>$LOGFILE" or die "Can't open logfile $LOGFILE!";
print LOG timestamp() . " " . $message . "\n";
close LOG;
}
}
#
# run_command(<command>,<error message>)
# Execute the given command or die with the respective error message
# Just print out the command when doing a dry run
#
sub run_command
{
my $command= $_[0];
my $errormsg= $_[1];
if ($opt_dry_run)
{
print "$command\n";
}
else
{
&logger($command);
$command.= " >> $LOGFILE 2>&1" if defined $opt_log;
$command.= " > /dev/null" if (!$opt_verbose && !$opt_log);
system($command) == 0 or &abort("$errormsg\n");
}
}
#
# abort(<message>)
# Exit with giving out the given error message or by sending
# it via email to the given mail address (including a log file snippet,
# if available)
#
sub abort
{
my $message= $_[0];
my $messagefile;
$message= "ERROR: " . $message;
&logger($message);
if ($opt_mail && !$opt_dry_run)
{
$messagefile= "/tmp/message.$$";
$subject= "Bootstrap of $REPO failed";
open(TMP,">$messagefile");
print TMP "$message\n\n";
close TMP;
if (defined $opt_log)
{
system("tail -n 40 $LOGFILE >> $messagefile");
}
system("mail -s \"$subject\" $opt_mail < $messagefile");
unlink($messagefile);
}
exit 1;
}
# Create a time stamp for logging purposes
sub timestamp
{
return &ymd() . " " . &hms();
}
#
# return the current time as a string (HH:MM:SS)
#
sub hms
{
my @ta= localtime(time());
my $h= $ta[2];
$h= "0" . "$h" if ($h <= 9);
my $m= $ta[1];
$m= "0" . "$m" if ($m <= 9);
my $s= $ta[0];
$s="0" . "$s" if ($s <= 9);
return "$h:$m:$s";
}
#
# return the current date as a string (YYYYMMDD)
#
sub ymd
{
my @ta=localtime(time());
my $d=$ta[3];
$d="0" . "$d" if ($d <= 9);
my $m=$ta[4]+1;
$m="0" . "$m" if ($m <= 9);
my $y=1900+$ta[5];
return "$y$m$d";
}