head	1.1;
access;
symbols;
locks; strict;
comment	@# @;


1.1
date	2001.04.25.17.14.58;	author kmaples;	state Exp;
branches;
next	;


desc
@@


1.1
log
@Added a filter and some shared logging routine to quiet the output of the
scripts in production.  Lines which do not explicitly use the routines are
interpreted as debug.

Changed the arguments (unimplemented) from 'is_private' to 'is_public' to
conform with helm.
@
text
@# ================================================================
# Copyright (c) 2001 Collab.Net.  All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 
# 3. The end-user documentation included with the redistribution, if
# any, must include the following acknowlegement: "This product includes
# software developed by Collab.Net (http://www.Collab.Net/)."
# Alternately, this acknowlegement may appear in the software itself, if
# and wherever such third-party acknowlegements normally appear.
# 
# 4. The hosted project names must not be used to endorse or promote
# products derived from this software without prior written
# permission. For written permission, please contact info@@collab.net.
# 
# 5. Products derived from this software may not use the "Tigris" name
# nor may "Tigris" appear in their names without prior written
# permission of Collab.Net.
# 
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ====================================================================
# 
# This software consists of voluntary contributions made by many
# individuals on behalf of Collab.Net.
#

# Shared logging library for parent/child scripts

# In order of verbosity:
# 
#   debug
#   info
#   warn
#   error

# Globals for this library:

# logging identifiers for each of the log levels:
my %TOKEN = (
    'debug' => '[debug]: ',
    'info'  => '[info]:  ', 
    'warn'  => '[warn]:  ', 
    'error' => '[error]: ', 
    'log'   => '[log]:   ', 
);

# Numeric weight of the respective levels: 
my %VALUE = (
    'debug' => 4,
    'info'  => 3,
    'warn'  => 2,
    'error' => 1,
    'log'   => 1,
);

# The default token to give a line, if it doesn't have one:
my $DEFAULT_LEVEL = 'debug'; 
my $DEFAULT_TOKEN = $TOKEN{$DEFAULT_LEVEL};

#---------------------
sub set_default_level {
#---------------------
    my $line = shift || '';
    return($DEFAULT_TOKEN . $line);
}
#---------------------
sub line_is_loggable {
#---------------------
    my ($level, $line) = @@_;

    # Get numeric for loglevel: 
    $level = $VALUE{$level};

    if($line =~ m/^\[(debug|info|warn|error|log)\]:/){
        my $linelevel = $VALUE{$1};
        if($linelevel <= $level){
            return(1);
        } else {
            return(0);
        }  
    } else {
        # In the event that we got a line without a default,
        # set one here:    
        return(line_is_loggable(set_default_level($line)));
    }
    return(0);
}

# Report if the current line has a log level identifier:
#---------------------
sub line_has_level {
#---------------------
    my $line = shift || '';
    if($line =~ m/^\[(debug|info|warn|error|log)\]:/){
        return(1);
    } else {
        return(0);
    }
}
## the purpose of the 'put' routines is to append the 
## appropriate token to the line coming in:  
#---------------------
sub put_debug {
#---------------------
	return(print_log_formatted($TOKEN{'debug'},@@_));	
}
#---------------------
sub put_info {
#---------------------
	return(print_log_formatted($TOKEN{'info'},@@_));	
}
#---------------------
sub put_warn {
#---------------------
	return(print_log_formatted($TOKEN{'warn'},@@_));	
}
#---------------------
sub put_error {
#---------------------
	return(print_log_formatted($TOKEN{'error'},@@_));	
}
#---------------------
sub put_log {
#---------------------
    return(print_log_formatted($TOKEN{'log'},@@_));
}
#---------------------
sub print_log_formatted {
#---------------------
    my $token = shift;
    foreach(@@_){ 
        # Account for the possibility of multi-line
        # records here: 
        my @@lines = split('\n', $_); 
        foreach(@@lines){ 
            print $token . $_; 
            print "\n" unless m/\n$/; 
        }
    }
}


1;
__END__





@
