################################################################################
#
# routine: printmsg.pl
#
# purpose: Prints a datetime-stamped message with a severity-level indicator
#          to stdout, and a trailer file if open.
#          The message looks like:
#
#          2001025193251-I-my message goes here.
#
#          The date/time is yyyydddhhmmss.  
# 
# Input:   level - message-level indicator (e.g. D,I,W,E,F for 
#                                              diag,info,warning,error,fatal)
#          msg   - text of the message
#          modulename - optional name of module text string to print
#          GLOBAL variable $trl is a file-handle to a trailer file
#
# Returns: none
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 06/03/00 43165  MSwam     first version
# 04/04/01 43165_12 MSwam   add one to dayofyear
# 11/12/04 52348  Sherbert  Set a consistent level indicator
# 
################################################################################
sub PrintMsg
{
    my ($level,$msg,$module_name) = @_;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
    my $date;

    # get the current date/time
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $year   += 1900;
    $yday   += 1;

    # read the environment setting for the PrintMsg report level
    # or default if not defined
    if (exists($ENV{"MSG_REPORT_LEVEL"})) {
       $report_level_val  = ($ENV{"MSG_REPORT_LEVEL"} );
    } else {
       $report_level_val = "MSG_INFO";
    }
#   print STDERR "# # # report_level_val is $report_level_val \n";

    # define the active levels for message reporting
    if ( $report_level_val      =~ /MSG_NONE/ ) {
        @active_levels = ("") ;
    } elsif ( $report_level_val =~ /MSG_FATAL/ ) {
        @active_levels = ("F") ;
    } elsif ( $report_level_val =~ /MSG_ERR/ ) {
        @active_levels = ("F","E") ;
    } elsif ( $report_level_val =~ /MSG_WARN/ ) {
        @active_levels = ("F","E","W") ;
    } elsif ( $report_level_val =~ /MSG_DIAG/  ||
              $report_level_val =~ /MSG_ALL/ ) {
        @active_levels = ("F","E","W","I","D") ;
    } else { # default is           MSG_INFO
        @active_levels = ("F","E","W","I") ;
    }
#   print STDERR "  # # active_levels are ", join (", ", @active_levels), "\n";


    ## Now printmsg.pl will only print the message if it is in MSG_REPORT_LEVEL
    my @MATCHING = ();
    @MATCHING = grep { $_ eq $level} @active_levels;
    foreach $MATCH ( @MATCHING ) {
#       print STDERR "    # MATCH is $MATCH; level is $level \n";

        # Set consistent level indicator
        if    ($level eq "W") { $leveltxt = "WARNING" }
        elsif ($level eq "E") { $leveltxt = "ERROR"   }
        elsif ($level eq "F") { $leveltxt = "FATAL"   }
        elsif ($level eq "D") { $leveltxt = "DIAG"    }
        else                  { $leveltxt = "INFO"    }


        if ( $module_name ) {
            $module_name = $module_name . '-';
        } 
        else {
            $module_name = "";
        }

        # format the message
        $date = sprintf("%4d%03d%02d%02d%02d",$year,$yday,$hour,$min,$sec);
        print "$date-$level-$leveltxt-" . $module_name . $msg . "\n";
     
        # print to trailer, if it is open
        if (defined($trl)) {
           print $trl "$date-$level-$leveltxt-" . $module_name . $msg . "\n";
        }

    }   ## End foreach MATCH in @active_levels 
}
1;
