Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

#==============================================================================
# **** Source Information ****
#
# Program Name        : DeployUtils::Logger.pm
#
# Program Type        : Perl Module (.pm)
#
# Original Author(s)  : G Christidis (gchristi)
#
# Description / Purpose:
#       A Logging Package for the deploy utilities.
#
#==============================================================================

#------------------------------------------------------------------------------
# Package definition
#------------------------------------------------------------------------------
package DeployUtils::Logger;

use strict;
use IO::Handle;

BEGIN
{
    # automatically export what we need into namespace of caller.
    use Exporter();
    our (@ISA, @EXPORT);
    @ISA         = qw(Exporter);
    @EXPORT      = qw(setLogLevel getLogLevel LogNorm LogError LogWarn LogInfo LogDebug LogRaw $LOG_LEVEL_ERROR $LOG_LEVEL_WARN $LOG_LEVEL_NORM $LOG_LEVEL_INFO $LOG_LEVEL_DEBUG );
    autoflush STDERR;
    autoflush STDOUT;
}

#------------------------------------------------------------------------------
# Constants global to this package and exported
#------------------------------------------------------------------------------
our ($LOG_LEVEL_ERROR)        = 1;
our ($LOG_LEVEL_WARN)         = 2;
our ($LOG_LEVEL_NORM)         = 3;
our ($LOG_LEVEL_INFO)         = 4;
our ($LOG_LEVEL_DEBUG)        = 5;

#------------------------------------------------------------------------------
# Constants private to this package
#------------------------------------------------------------------------------
my ($_LOG_FORMAT)             = "%-7s %s";
my ($_LOG_LEVEL)              = $LOG_LEVEL_NORM;

#------------------------------------------------------------------------------
sub setLogLevel
#
# Description:
#       This sub-routine is used to set the log level from the calling script.
#
#------------------------------------------------------------------------------
{
    # correct number of parameters?
    if ( ($#_+1) != 1 )
    {
        LogError("Incorrect number of params passed to " .
                  "setLogLevel() function. " .
                  "Check your config.");
    }

    my ($logLevel) = shift;

    if ( $logLevel >= $LOG_LEVEL_ERROR && $logLevel <= $LOG_LEVEL_DEBUG )
    {
        $_LOG_LEVEL = "$logLevel";
    }
    else
    {
        $_LOG_LEVEL = $LOG_LEVEL_DEBUG;
        LogDebug("Setting log level to max level [$LOG_LEVEL_DEBUG] (Debug).");
    }
    return 1;
}


#------------------------------------------------------------------------------
sub getLogLevel
#
# Description:
#       This sub-routine is used to get the current log level
#
#------------------------------------------------------------------------------
{
    return $_LOG_LEVEL;
}


#------------------------------------------------------------------------------
sub LogNorm
#
# Description:
#       This sub-routine is used to generate a consistent log message format.
#       If 1st param is -n then no \n is printed, the message should be next param
#------------------------------------------------------------------------------
{
    if ( $_LOG_LEVEL >= $LOG_LEVEL_NORM )
    {
        my ($m_arg) = shift;
        my ($term) = "\n";
    
        if ( $m_arg eq "-n" )
        {
            $term = "";
            $m_arg = shift;
        }

        printf(STDERR "$_LOG_FORMAT$term", "[NORM]", $m_arg);
    }
    return 1;
}


#------------------------------------------------------------------------------
sub LogError
#
# Description:
#       This sub-routine is used to generate a consistent log message format.
#       if param is -n then no \n is printed
#       if param is -x then will not exit
#------------------------------------------------------------------------------
{
    my $exitFlag = 1;

    if ( $_LOG_LEVEL >= $LOG_LEVEL_ERROR )
    {
        my $term = "\n";
        my ($m_arg) = shift;

        while ( $m_arg eq "-n" || $m_arg eq "-x" )
        {
            $term = "" if ( $m_arg eq "-n" );
            $exitFlag = 0 if ( $m_arg eq "-x" );
            $m_arg = shift;
        }
        printf(STDERR "$_LOG_FORMAT$term", "[ERROR]", $m_arg);
        printf(STDERR "$_LOG_FORMAT$term", "[ERROR]", "Terminating.") if ( $exitFlag );
    }

    # here we must exit unless -x is supplied
    exit(1) if ( $exitFlag );
    return 1;
}


#------------------------------------------------------------------------------
sub LogWarn
#
# Description:
#       This sub-routine is used to generate a consistent log message format.
#       If 1st param is -n then no \n is printed, the message should be next param
#------------------------------------------------------------------------------
{
    if ( $_LOG_LEVEL >= $LOG_LEVEL_WARN )
    {
        my ($m_arg) = shift;
        my ($term) = "\n";
    
        if ( $m_arg eq "-n" )
        {
            $term = "";
            $m_arg = shift;
        }

        printf(STDERR "$_LOG_FORMAT$term", "[WARN]", $m_arg);
    }
    return 1;
}


#------------------------------------------------------------------------------
sub LogInfo
#
# Description:
#       This sub-routine is used to generate a consistent informational log 
#       message with a predefined format.
#       If 1st param is -n then no \n is printed, the message should be next param
#------------------------------------------------------------------------------
{
    if ( $_LOG_LEVEL >= $LOG_LEVEL_INFO )
    {
        my ($m_arg) = shift;
        my ($term) = "\n";
    
        if ( $m_arg eq "-n" )
        {
            $term = "";
            $m_arg = shift;
        }
        printf(STDERR "$_LOG_FORMAT$term", "[INFO]", $m_arg);
    }
    return 1;
}


#------------------------------------------------------------------------------
sub LogDebug
#
# Description:
#       This sub-routine is used to generate a consistent debug log 
#       message with a predefined format.
#       If 1st param is -n then no \n is printed, the message should be next param
#------------------------------------------------------------------------------
{
    if ( $_LOG_LEVEL >= $LOG_LEVEL_DEBUG )
    {
        my ($m_arg) = shift;
        my ($term) = "\n";
    
        if ( $m_arg eq "-n" )
        {
            $term = "";
            $m_arg = shift;
        }
        printf(STDERR "$_LOG_FORMAT$term", "[DEBUG]", $m_arg);
    }
    return 1;
}


#==============================================================================
#   LogRaw
#
# Description
#       This sub-routine is used to output raw data passed to logger
#==============================================================================
sub LogRaw
{
    printf(STDERR @_);
}   # LogRaw
1;