Skip to content

Instantly share code, notes, and snippets.

@thinkhy
Created January 10, 2011 03:29
Show Gist options
  • Save thinkhy/772306 to your computer and use it in GitHub Desktop.
Save thinkhy/772306 to your computer and use it in GitHub Desktop.
Write_Log
sub getTime
{
my $time = shift || time();
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
$year += 1900;
$mon ++;
$min = '0'.$min if length($min) < 2;
$sec = '0'.$sec if length($sec) < 2;
$mon = '0'.$mon if length($mon) < 2;
$mday = '0'.$mday if length($mday) < 2;
$hour = '0'.$hour if length($hour) < 2;
my $weekday = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[$wday];
return { 'second' => $sec,
'minute' => $min,
'hour' => $hour,
'day' => $mday,
'month' => $mon,
'year' => $year,
'weekNo' => $wday,
'wday' => $weekday,
'yday' => $yday,
'date' => "$year-$mon-$mday"
};
}
sub writelog
{
my $str = shift(@_);
open LOG, ">>$logfile" or die "Could not open $logfile: $!";;
my $tt = getTime();
my $t = $tt->{'year'}."\/".$tt->{'month'}."\/".$tt->{'day'}." $tt->{'hour'}:$tt->{'minute'}:$tt->{'second'} ";
print LOG $t.": ".$str;
close LOG;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment