# Errors logging library. Write logs into file.
# Using: $result=LogError('Error message');
# Don't set $! to 'Error message', script will make it self.
# Template variables:
# %year% - year,
# %month% - month,
# %year_day% - day of year,
# %month_day% - day of month,
# %week_day% - day of week,
# %hour% - hour,
# %minute% - minute,
# %second% - second,
# %message_text% - text of 'Error message',
# %standart_error_output% - perl error variable ($!),
# %eol% - new line (\n).
$ErrorLog_File='./ErrorLog.dat';
$ErrorLog_Semathor='./ErrorLog.sem';
$ErrorLog_Template='[%year%/%month%/%month_day%, %hour%:%minute%:%second%] %message_text%; $! Returned: %standart_error_output% %eol%';
$ErrorLog_Result=1;
sub LogError {
my $text=shift;
&LogError_LockSemathor;
open(ELF, ">>$ErrorLog_File") || ($ErrorLog_Result=0);
print ELF LogError_MakeOutput($text);
close(ELF);
&LogError_UnlockSemathor;
return $ErrorLog_Result;
undef $text;
}
sub LogError_MakeOutput {
my $text=shift;
my ($sec,$min,$hour,$month_day,$mon,$year,$week_day,$year_day)=LogError_MakeTime();
$_=$ErrorLog_Template;
s/%year%/$year/igs;
s/%month%/$mon/igs;
s/%year_day%/$year_day/igs;
s/%month_day%/$month_day/igs;
s/%week_day%/$week_day/igs;
s/%hour%/$hour/igs;
s/%minute%/$min/igs;
s/%second%/$sec/igs;
s/%message_text%/$text/igs;
s/%standart_error_output%/$!/igs;
s/%eol%/\n/igs;
return $_;
($sec,$min,$hour,$month_day,$mon,$year,$week_day,$year_day,$text,$_)=-1;
}
sub LogError_MakeTime {
my ($sec,$min,$hour,$month_day,$mon,$year,$week_day,$year_day)=(localtime(time))[0..7];
if ($sec<10) {
$sec="0$sec";
}
if ($min<10) {
$min="0$min";
}
if ($hour<10) {
$hour="0$hour";
}
$year_day++;
$mon++;
$year+=1900;
return ($sec,$min,$hour,$month_day,$mon,$year,$week_day,$year_day);
($sec,$min,$hour,$month_day,$mon,$year,$week_day,$year_day)=-1;
}
sub LogError_LockSemathor {
open(LESEM, ">$ErrorLog_Semathor") || ($ErrorLog_Result=0);
flock(LESEM,2);
}
sub LogError_UnlockSemathor {
close(LESEM);
}