################################################################################
# #
# ############################################################################ #
# # Title : Data::Logger.pm # #
# # Description : A super simple logger, you don't actually need to tell it # #
# # a single thing. # #
# # Coded by : Justin Lee # #
# # Version : 0.3.3-RC4 # #
# # Date : Tuesday, February 19, 2008 # #
# # License : GNU General Public License v.2 # #
# # Copyright (C) 2008 Justin Lee <kool.name at gmail.com> # #
# ############################################################################ #
# #
# ############################################################################ #
# # This program is free software; you can redistribute it and/or # #
# # modify it under the terms of the GNU General Public License version 2 # #
# # as published by the Free Software Foundation. # #
# # # #
# # This program is distributed in the hope that it will be useful, # #
# # but WITHOUT ANY WARRANTY; without even the implied warranty of # #
# # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # #
# # GNU General Public License for more details. # #
# # # #
# # You should have received a copy of the GNU General Public License # #
# # along with this program; if not, write to the Free Software # #
# # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # #
# # 02110-1301, USA. Or, see: <http://www.gnu.org/licenses/> # #
# ############################################################################ #
# #
################################################################################
package Data::Logger;
use strict;
use warnings;
use Carp;
use subs qw(die warn log);
use FileHandle;
use File::Basename;
use Class::Date qw(now);
sub new {
my $class = shift;
my $self = shift; # comes in as a hash or nothing, else fails vv
unless(defined $self) { $self = { 'title' => basename($main::0) }; }
unless(ref $self eq 'HASH') { croak '(Data::Logger) Improper usage!'; }
unless(exists $self->{'dir'} and
defined $self->{'dir'}) { $self->{'dir'} = 'logs'; }
unless(exists $self->{'name'} and
defined $self->{'name'}) { $self->{'name'} = '%Y-%M-%D %h;%m;%s.log'; }
unless(exists $self->{'tstamp'} and
defined $self->{'tstamp'}) { $self->{'tstamp'} = '%Y-%M-%D %h:%m:%s'; }
unless(-d $self->{'dir'}) { mkdir($self->{'dir'},0777) or
croak '(Data::Logger) Unable to create logging directory.'; }
$self->{'fh'} = new FileHandle; ## vv Ahahaha! Fool! I laugh at your attempts to comprehend!
$self->{'fh'}->open(_parse_dt(join('/',split(/\/|\\/,$self->{'dir'}),$self->{'name'})),'>');
croak '(Data::Logger) Filehandle was not properly created.' unless defined $self->{'fh'};
$self->{'fh'}->print( # a quick header
tstamp($self) .
" - Data::Logger ready for input from `$self->{'title'}'!\n"
);
return bless $self, $class;
}
sub _decode { # to be used with all future decoding (see &_parse_dt for how they'll look)
my $href = shift;
my $style = shift;
while ($style =~ /\%(\S)/o) {
my $tag = $1;
if (exists $href->{$tag}) {
$style =~ s/\%$tag/$href->{$tag}/g;
} else {
$style =~ s/\%$tag//g;
}
}
return $style;
}
sub _parse_dt { # ISO-style date time strings
my ($date,$time) = split(/\s+/,now);
my ($year,$month,$day) = split(/-/,$date);
my ($hour,$minute,$second) = split(/:/,$time);
my %datetime = (
'Y' => $year,
'M' => $month,
'D' => $day,
'h' => $hour,
'm' => $minute,
's' => $second
);
return scalar _decode(\%datetime,@_);
}
sub tstamp {
my $self = shift;
return scalar _parse_dt($self->{'tstamp'});
}
sub log {
my $self = shift;
$self->{'fh'}->print(
tstamp($self) .
" - (DEBUG) \u@{[ join(' ',@_) ]}.\n"
);
}
sub warn {
my $self = shift;
$self->{'fh'}->print(
tstamp($self) .
" - (WARNING) \u@{[ join(' ',@_) ]}.\n"
);
carp join(' ',@_);
}
sub die {
my $self = shift;
$self->{'fh'}->print(
tstamp($self) .
" - (FATAL) \u@{[ join(' ',@_) ]}.\n"
);
$self->{'fh'}->close;
croak join(' ',@_);
}
sub close {
my $self = shift;
$self->{'fh'}->close;
return 1;
}
1;