From: Stephen L Johnson Date: Mon, 24 Jan 2000 07:11:12 +0000 (+0000) Subject: Initial versions added to repository. X-Git-Tag: spong-2_7-alpha5~110 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7ef55ebd4d672d4bc66bb3c9bc3abd10bf1d9b34;p=spong.git Initial versions added to repository. --- diff --git a/src/lib/Spong/Log.pm b/src/lib/Spong/Log.pm new file mode 100755 index 0000000..6a157bd --- /dev/null +++ b/src/lib/Spong/Log.pm @@ -0,0 +1,99 @@ +# This module encapsulated a standard set of Logging functions that Spong +# programs use as. This module uses Logger objects for handling the actual +# logging procedures. +# +# methods +# error_context_init() - Initialize error output context +# debug_context_init() - Initialize debug output context +# error() - output an error entry +# debug() - output a debug entry + +package Spong::Log; + + +use strict; +use Carp; +use IO::Socket; + +use Spong::Logger; + +require Exporter; +use vars qw(@ISA @EXPORT_OK $VERSION); +@ISA = qw(Exporter); +@EXPORT_OK = qw(error debug); +$VERSION = 0.01; + +my($stdout, $stderr, $syslog, $ident, $facility, $logopt, $priority); +my($debuglevel); +my($filename); +my($errlog); + +# Call the set context functions to initialize default values +&set_debug_context(); +&set_error_context(); + +# This function is used to set the debug logging context + +sub set_debug_context { + my( %arg ) = @_; + + $debuglevel = defined $arg{debuglevel} ? $arg{debuglevel} : 0; +} + +# This function used to set the error logging context + +sub set_error_context { + my( %arg ) = @_; + + $stdout = defined $arg{stdout} ? $arg{stdout} : 0; + $stderr = defined $arg{stderr} ? $arg{stderr} : 1; + $syslog = defined $arg{syslog} ? $arg{syslog} : 0; + $ident = defined $arg{ident} ? $arg{ident} : ""; + $facility = defined $arg{facility} ? $arg{facility} : 'user'; + $logopt = defined $arg{logopt} ? $arg{logopt} : ""; + $priority = defined $arg{priority} ? $arg{priority} : 'info'; + $filename = $arg{filename} if defined $arg{filename}; + +} + +# Function create a logger object with the current error context +sub open_error_log { + + $errlog = new Spong::Logger( stdout => $stdout, + stderr => $stderr, + syslog => $syslog, + ident => $ident, + facility => $facility, + priority => $priority, + filename => $filename, + ); + + if (! defined $errlog ) { + croak "Could not create error logger: $!"; + return 1; + } + +} + +# Function closes the error log by undefineding the error logger object +sub close_error_log { + undef $errlog; +} + +sub error { + my( $message ) = @_; + + # if the error logger is not defined, create it. + if ( ! defined $errlog ) { &open_error_log(); } + + $errlog->log("Error: $message"); +} + +sub debug { + my ($message,$level) = @_; + $level = 1 if (! defined $level); # Default to level 1 + print scalar localtime, " ", $message, "\n" if $debuglevel >= $level; +} + +1; # I'm included perl code, I need this line + diff --git a/src/lib/Spong/Logger.pm b/src/lib/Spong/Logger.pm new file mode 100755 index 0000000..c993ae8 --- /dev/null +++ b/src/lib/Spong/Logger.pm @@ -0,0 +1,94 @@ +# This module is a more generalzed debugging facility. It can be configured +# to log information to serveral different facilities, stdout, stderr, +# syslog and/or a file. +# +# The logger object is initialized with following key-value pairs. +# stdout Log to Stdout +# stderr Log to Stderr +# filename File name to log entries into +# syslog Log to syslog +# ident Identity argument for openlog() +# facility Syslog facility to log to Defaults to 'user' +# priority Default syslog priority to log Defaults to 'notice' +# logopt Log Options parameter for openlog() +# +# Functions +# log(msg [, pri]) Log an entry +# Where +# msg is message to log +# pri is priority to use in syslog + +package Spong::Logger; +use Sys::Syslog qw(:DEFAULT setlogsock); +use FileHandle; +use Carp; + +# Constructor. A simple constructor that just sets all of the instance vars +# according to what was passed in. + +sub new { + my( $proto, %arg ) = @_; + my( $class ) = ref($proto) || $proto; + my $self = {}; + + $self->{'stdout'} = defined $arg{stdout} ? $arg{stdout} : 0; + $self->{'stderr'} = defined $arg{stderr} ? $arg{stderr} : 0; + $self->{'syslog'} = defined $arg{syslog} ? $arg{syslog} : 0; + $self->{'ident'} = defined $arg{ident} ? $arg{ident} : ""; + $self->{'facility'} = defined $arg{facility} ? $arg{facility} : 'user'; + $self->{'logopt'} = defined $arg{logopt} ? $arg{logopt} : ""; + $self->{'priority'} = defined $arg{priority} ? $arg{priority} : 'info'; + $self->{'filename'} = $arg{filename} if defined $arg{filename}; + + # If we are logging to syslog, open a proper channel + if ($self->{'syslog'}) { + setlogsock 'unix'; + } + + # If we are logging to a file, open the file + if ( defined $self->{'filename'} && $self->{'filename'} ne "" ) { + $self->{'fh'} = new FileHandle ">> " . $arg{filename}; + if (! defined $self->{'fh'} ) { + warn "Could not open file $arg{filename}"; + } else { + $self->{'fh'}->autoflush(1); + } + } + + bless ($self,$class); + return $self; +} + +sub DESTROY { + my( $self ) = @_; + + # If were are logging to a file, close it + if (defined $self->{'fh'}) { $self->{'fh'}->close();} + # And if using syslog, closelog for completion + if ( $self->{'syslog'} ) { closelog(); } + +} + +sub log { + my( $self, $message, $pri ) = @_; + $pri = $self->{priority} if ! defined $pri; + + if ($self->{stdout}) { print scalar localtime() , " ", $message,"\n"; } + + if ($self->{stderr}) { print STDERR scalar localtime(), " ",$message,"\n"; } + + if ($self->{syslog}) { + openlog $self->{'ident'},$self->{'logopt'},$self->{'facility'}; + syslog $pri,$message,''; + closelog; + } + + if ($self->{'fh'} ) { + my $fh = $self->{'fh'}; + print $fh scalar(localtime())," ",$message,"\n"; + } + +} + +1; # I'm included perl code, I need this line +