From: Geert J. <gj...@us...> - 2003-10-03 09:29:07
|
Update of /cvsroot/woc/woc2/source/maint/wocperl In directory sc8-pr-cvs1:/tmp/cvs-serv31184 Added Files: Log.pm Log Message: New WOC Perl modules: Module for logging --- NEW FILE: Log.pm --- ############################################################################### # # PERL MODULE # # Filename: Log.pm # Short description: Module for handling of logging and log files # Version: 1.00 # Last updated: 2003-10-03 # Status: Untested # Perl version: 5.x # Owner: Woordenboek Organische Chemie (WOC) # Created by: GJ: Gee...@da... (Daidalos BV and WOC-team) # Edited by: GJ: Gee...@da... (Daidalos BV and WOC-team) # Copyright (c): 2003 Woordenboek Organische Chemie (WOC) # Nijmegen, The Netherlands # ############################################################################### # # History: # 1.00 2003-10-03 # Initial version based on XMLDoc.pm. # ############################################################################### # # See end of script for Usage and Description as POD documentation. # # Note: use perldoc Log.pm or perldoc Log to read the documentation # ############################################################################### use strict; ##################################################################### package Log; ##################################################################### use Carp; use vars qw ( $VERSION @ISA @EXPORT_OK $AUTOLOAD ); $VERSION = '1.00'; @ISA = qw( Exporter ); @EXPORT_OK = qw( &log &debug &warn &error ); my %deprecation_used; ###################################################################### # CONSTRUCTOR/DESTRUCTOR sub new { my $class = shift; my $self = bless {}, $class; my %args = $self->__parse_args(@_); # the output handles $self->{OUT} = defined $args{outfile} ? $args{outfile} : \*STDOUT; $self->{ERR} = defined $args{errfile} ? $args{errfile} : \*STDERR; if (ref $self->{OUT} != "GLOB") { my $OUTHANDLE; if (open ($OUTHANDLE, $self->{OUT})) { $self->{OUT} = \*{$OUTHANDLE}; } else { carp "Failed to open $self->{OUT} for Output Logging."; } } if (ref $self->{ERR} != "GLOB") { my $ERRHANDLE; if (open ($ERRHANDLE, $self->{ERR})) { $self->{ERR} = \*{ERRHANDLE}; } else { carp "Failed to open $self->{ERR} for Error Logging."; } } $self->{VERBOSE} = defined $args{verbose} ? $args{verbose} : ""; # "" = false $self->{DEBUG} = defined $args{debug} ? $args{debug} : ""; # "" = false $self->{WARNINGS} = defined $args{warnings} ? $args{warnings} : ""; # "" = false $self->{HALTONERROR} = defined $args{haltonerror} ? $args{haltonerror} : ""; # "" = false $self->{OVERLOADWARN} = defined $args{overloadwarn} ? $args{overloadwarn} : 1; # 1 = true $self->{OVERLOADDIE} = defined $args{overloaddie} ? $args{overloaddie} : 1; # 1 = true $self->{INDENT} = defined $args{indent} ? $args{indent} : 0; # starting number $self->{INDENT_INCR} = defined $args{indent_incr} ? $args{indent_incr} : 1; # increment number return $self; } sub DESTROY {} # Cuts out random dies on includes ## Internal function for verifying arguments sub __parse_args { my $self = shift; my %args; if(@_ % 2 == 1) { my $name = [caller(1)]->[3]; carp "Argument syntax of call to $name incorrect. See the documentation for $name" } else { %args = @_; } return %args; } ###################################################################### # PUBLIC DEFINITIONS sub log { my $self = shift; my $OUT = $self->{OUT}; if (fileno($OUT)) { if ($self->{DEBUG}) { print $OUT " "x$self->{INDENT},"@_\n"; } elsif ($self->{VERBOSE}) { print $OUT " "x$self->{INDENT},"@_\n"; } } elsif ($self->{DEBUG} || $self->{VERBOSE}) { $self->warn ("Output log not open.."); print STDOUT @_; } } sub debug { my $self = shift; my $OUT = $self->{OUT}; if (fileno($OUT)) { print $OUT " "x$self->{INDENT},"@_\n" if $self->{DEBUG}; } elsif ($self->{DEBUG}) { $self->warn ("Output log not open.."); print STDOUT @_; } } sub warn { my $self = shift; my $ERR = $self->{ERR}; if (fileno($ERR)) { if ($self->{DEBUG} && ($self->{OUT} == $ERR)) { print $ERR " "x$self->{INDENT},"@_\n"; } elsif ($self->{WARNINGS}) { print $ERR "@_\n"; } } else { carp "Error log not open.."; carp @_; } } sub error { my $self = shift; my $ERR = $self->{ERR}; if (fileno($ERR)) { if ($self->{DEBUG} && ($self->{OUT} == $ERR)) { print $ERR " "x$self->{INDENT},"@_\n"; } else { print $ERR "@_\n"; } } else { $self->warn ("Error log not open.."); croak @_; } } 1; ############################################################################### __END__ =head1 NAME Log.pm - A module for handling of logging and log files =head1 DESCRIPTION This module provides functionality to perform logging into two separate channels, alike STDOUT and STDERR. It also provides functionallity to produce four types of messages: =over 4 =item logging General log messages, progress indication and verbosity =item debug Additional messages for debug and trace purposes =item warnings Warnings that are encountered during the process =item errors Errors that are encountered during the process, may stop at the first =back =head1 SYNOPSIS use Log; my $LogObj = Log->new(%args); =head1 OPTIONS =over 4 =item outfile path and name of Output log file or a reference to a GLOB like \*STDOUT, path must exist =item errfile path and name of Error log file or a reference to a GLOB like \*STDERR, path must exist =item verbose (de)activate logging of log messages in Output log =item debug (de)activate logging of debug messages in Output log. Activating logging of debug messages will activate use of indentation in Output log. See also options indent and indent_incr =item warnings (de)activate logging of warnings in Error log =item haltonerror (de)activate stopping after encountering first error =item overloadwarn (not operational yet) =item overloaddie (not operational yet) =item indent sets starting column for indentation =item indent_incr sets increment number for indentation. One means increment indentation with one blank with each nesting =back =cut ############################################################################### |