################################################################################
# #
# ############################################################################ #
# # Title : Logger::ByDate.pm # #
# # Description : A logger coded for the camman script. # #
# # Coded by : Justin Lee # #
# # Version : 0.0.1 # #
# # Date : Saturday, February 9, 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 Logger::ByDate;
use strict;
use warnings;
use subs qw(log);
use FileHandle;
use Class::Date qw(now);
sub new {
my $type = shift;
my $self = {};
my %arg = @_;
$self->{'dir'} = $arg{'dir'};
$self->{'name'} = decode_date(now)->($self->{'dir'} . '/[date] [time].log');
$self->{'log'} = new FileHandle $self->{'name'}, '>';
return unless defined $self->{'log'};
$self->{'log'}->print(decode_date(now)->('[time]')," - Logger active!\n");
return bless $self, $type;
}
sub decode_date {
my ($date,$time) = split(/\s+/,shift);
my %datetime = (
'date' => sprintf('%04d-%02d-%02d',split(/-/,$date)),
'time' => sprintf('%02d;%02d;%02d',split(/:/,$time))
);
return sub { # This return is only like this for readability when it's called.
# I was also thinking of changing this into a routine that
# creates directories if passed a string like '~/[date]/[time].log'.
my $log = shift;
$log =~ s/\[(\w+)\]/$datetime{$1}/g;
return $log;
}
}
# who needs log() anyway?
sub log {
my $self = shift;
my @to_say = @_;
$self->{'log'}->print(decode_date(now)->('[time]'),' - ',join(' ',@to_say),"\n");
}
# ONE!!!!!
1;