A
download DXLog.pm
Language: Perl
LOC: 140
Project Info
DXSpider DX Cluster System(dxspider)
Server: SourceForge
Type: cvs
...pider\dxspider\spider\perl\
   AGWConnect.pm
   AGWMsg.pm
   AnnTalk.pm
   BadWords.pm
   Bands.pm
   BBS.pm
   Buck.pm
   call.pl
   callbot.pl
   Chain.pm
   cluster.pl
   CmdAlias.pm
   connect.pl
   console.pl
   Console.pm
   convert_users.pl
   convkeps.pl
   create_prefix.pl
   create_qsl.pl
   create_sysop.pl
   create_usdb.pl
   DB0SDX.pm
   DXBearing.pm
   dxcc.pl
   DXChannel.pm
   DXCommandmode.pm
   DXConnect.pm
   DXCron.pm
   DXDb.pm
   DXDebug.pm
   DXDupe.pm
   DXHash.pm
   DXLog.pm
   DXLogPrint.pm
   DXM.pm
   DXMsg.pm
   dxoldtonew.pl
   DXProt.pm
   DXProtout.pm
   DXProtVars.pm
   DXSql.pm
   DXUser.pm
   DXUtil.pm
   DXVars.pm.issue
   DXXml.pm
   Editable.pm
   export_opernam.pl
   ExtMsg.pm
   Filter.pm
   ForkingServer.pm
   gen_usdb_data.pl
   Geomag.pm
   hlptohtml.pl
   importkeps.pl
   importwwv.pl
   Internet.pm
   IntMsg.pm
   Investigate.pm
   IsoTime.pm
   Julian.pm
   K4UTE.pm
   Keps.pm
   Listeners.pm
   Local.pm
   lock_nodes.pl
   log2csv.pl
   LRU.pm
   Minimuf.pm
   MiscLog.pm
   Mrtg.pm
   Msg.pm
   PC.pm
   Prefix.pm
   process_ursa.pl
   Prot.pm
   proto.html
   QRZ.pm
   QSL.pm
   RingBuf.pm
   Route.pm
   RouteDB.pm
   Script.pm
   Spot.pm
   spot2csv.pl
   Sun.pm
   talias.pl
   Thingy.pm
   Timer.pm
   UDPMsg.pm
   update_sysop.pl
   USDB.pm
   VE7CC.pm
   Verify.pm
   WCY.pm
   winclient.pl
   y2k.sh

#
# the general purpose logging machine
#
# This module is designed to allow you to log stuff in specific places
# and will rotate logs on a monthly, weekly or daily basis. 
#
# The idea is that you give it a prefix which is a directory and then 
# the system will log stuff to a directory structure which looks like:-
#
# daily:-
#   spots/1998/<julian day no>[.<optional suffix>]
#
# weekly :-
#   log/1998/<week no>[.<optional suffix>]
#
# monthly
#   wwv/1998/<month>[.<optional suffix>]
#
# Routines are provided to read these files in and to append to them
# 
# Copyright (c) - 1998 Dirk Koopman G1TLH
#
# $Id: DXLog.pm,v 1.22 2006/01/07 16:53:03 minima Exp $
#

package DXLog;

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(Log LogDbg Logclose);

use IO::File;
use DXVars;
use DXUtil;
use Julian;

use Carp;

use strict;

use vars qw($VERSION $BRANCH);
$VERSION = sprintf( "%d.%03d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/ );
$BRANCH = sprintf( "%d.%03d", q$Revision: 1.22 $ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
$main::build += $VERSION;
$main::branch += $BRANCH;

use vars qw($log);

$log = new('log', 'dat', 'm');

# create a log object that contains all the useful info needed
# prefix is the main directory off of the data directory
# sort is 'm' for monthly, 'd' for daily 
sub new
{
	my ($prefix, $suffix, $sort) = @_;
	my $ref = {};
	$ref->{prefix} = "$main::data/$prefix";
	$ref->{suffix} = $suffix if $suffix;
	$ref->{sort} = $sort;
	
	# make sure the directory exists
	mkdir($ref->{prefix}, 0777) unless -e $ref->{prefix};
	return bless $ref;
}

sub _genfn
{
	my ($self, $jdate) = @_;
	my $year = $jdate->year;
	my $thing = $jdate->thing;
	
	my $fn = sprintf "$self->{prefix}/$year/%02d", $thing if $jdate->isa('Julian::Month');
	$fn = sprintf "$self->{prefix}/$year/%03d", $thing if $jdate->isa('Julian::Day');
	$fn .= ".$self->{suffix}" if $self->{suffix};
	return $fn;
}

# open the appropriate data file
sub open
{
	my ($self, $jdate, $mode) = @_;
	
	# if we are writing, check that the directory exists
	if (defined $mode) {
		my $year = $jdate->year;
		my $dir = "$self->{prefix}/$year";
		mkdir($dir, 0777) if ! -e $dir;
	}

	$self->{fn} = $self->_genfn($jdate);
	
	$mode = 'r' if !$mode;
	$self->{mode} = $mode;
	$self->{jdate} = $jdate;
	
	my $fh = new IO::File $self->{fn}, $mode, 0666;
	return undef if !$fh;
	$fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
	$self->{fh} = $fh;

#	print "opening $self->{fn}\n";
	
	return $self->{fh};
}

sub delete($$)
{
	my ($self, $jdate) = @_;
	my $fn = $self->_genfn($jdate);
	unlink $fn;
}

sub mtime($$)
{
	my ($self, $jdate) = @_;
	
	my $fn = $self->_genfn($jdate);
	return (stat $fn)[9];
}

# open the previous log file in sequence
sub openprev($$)
{
	my $self = shift;
	my $jdate = $self->{jdate}->sub(1);
	return $self->open($jdate, @_);
}

# open the next log file in sequence
sub opennext($$)
{
	my $self = shift;
	my $jdate = $self->{jdate}->add(1);
	return $self->open($jdate, @_);
}

# convert a date into the correct format from a unix date depending on its sort
sub unixtoj($$)
{
	my $self = shift;
	
	if ($self->{'sort'} eq 'm') {
		return Julian::Month->new(shift);
	} elsif ($self->{'sort'} eq 'd') {
		return Julian::Day->new(shift);
	}
	confess "shouldn't get here";
}

# write (actually append) to a file, opening new files as required
sub write($$$)
{
	my ($self, $jdate, $line) = @_;
	if (!$self->{fh} || 
		$self->{mode} ne ">>" || 
		$jdate->year != $self->{jdate}->year || 
		$jdate->thing != $self->{jdate}->thing) {
		$self->open($jdate, ">>") or confess "can't open $self->{fn} $!";
	}

	return $self->{fh}->print("$line\n");
}

# write (actually append) using the current date to a file, opening new files as required
sub writenow($$)
{
	my ($self, $line) = @_;
	my $t = time;
	my $date = $self->unixtoj($t);
	return $self->write($date, $line);
}

# write (actually append) using a unix time to a file, opening new files as required
sub writeunix($$$)
{
	my ($self, $t, $line) = @_;
	my $date = $self->unixtoj($t);
	return $self->write($date, $line);
}

# close the log file handle
sub close
{
	my $self = shift;
	undef $self->{fh};			# close the filehandle
	delete $self->{fh};	
}

sub DESTROY
{
	my $self = shift;
	undef $self->{fh};			# close the filehandle
	delete $self->{fh} if $self->{fh};
}

# log something in the system log 
# this routine is exported to any module that declares DXLog
# it takes all its args and joins them together with the unixtime writes them out as one line
# The user is responsible for making sense of this!
sub Log
{
	my $t = time;
	$log->writeunix($t, join('^', $t, @_) );
}

sub LogDbg
{
	DXDebug::dbg($_[$#_]);
	Log(@_);
}

sub Logclose
{
	$log->close();
}
1;

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us