#
#  Copyright Red Hat Inc., 2002
#  Copyright Mission Critical Linux, 2000
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; see the file COPYING.  If not, write to the
#  Free Software Foundation, Inc.,  675 Mass Ave, Cambridge, 
#  MA 02139, USA.
#

# $Revision: 1.4 $
#
# Author: Gregory P. Myrdal <Myrdal@MissionCriticalLinux.Com>

#
# Shell logging library functions.
#

#-----------------------------------------------------------------------------
# Globals
#-----------------------------------------------------------------------------

#
# Logging states
#
# (Note: LOG_DEFAULT should be the same as svcmgr.h)
#
LOG_EMERG=0			# system is unusable
LOG_ALERT=1			# action must be taken immediately
LOG_CRIT=2			# critical conditions
LOG_ERR=3			# error conditions
LOG_WARNING=4			# warning conditions
LOG_NOTICE=5			# normal but significant condition
LOG_INFO=6			# informational
LOG_DEBUG=7			# debug-level messages
LOG_DEFAULT=$LOG_NOTICE		# default log level of Service Manager

#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------

#
# Given a level to log, determine if we should log based on the current
# svcmgr log level setting.  Argument $2 will be filled in with the 
# log level string.
#
# Returns:
#	$YES, $2 contains log level string
#	$NO
#	$FAIL
#
logLevelCheck()
{
	typeset svcmgr_log_level
	typeset MYNAME=$(basename $0)

	if [ $# -ne 2 ]; then
	  echo "$MYNAME: Usage logLevelCheck log_level \"log_level_string_variable\"" >&2
	  return $FAIL
	fi

	typeset log_level=$1
	
	case "$log_level" in
	  $LOG_EMERG)	eval $2='"emergency"' ;;
	  $LOG_ALERT)	eval $2='"alert"' ;;
	  $LOG_CRIT)	eval $2='"critcal"' ;;
	  $LOG_ERR)	eval $2='"error"' ;;
	  $LOG_WARNING)	eval $2='"warning"' ;;
	  $LOG_NOTICE)	eval $2='"notice"' ;;
	  $LOG_INFO)	eval $2='"info"' ;;
	  $LOG_DEBUG)	eval $2='"debug"' ;;
	  *)
	    echo "$MYNAME: Unknown log level '$log_level'" >&2
	    eval $2='"Unknown"'
	    ;;
	esac

	svcmgr_log_level=$(getSvcMgrLogLevel $DB)
	retVal=$?
	case $retVal in
	0)  ;;
	2)  svcmgr_log_level=$LOG_DEFAULT;;
	*)  svcmgr_log_level=$LOG_DEFAULT
	    echo "$MYNAME: Cannot get Service Manager log level; error=$retVal" >&2
	    ;;
	esac

	if [ -n "$log_level" -a -n "$svcmgr_log_level" \
	     -a $log_level -le $svcmgr_log_level ]; then
	  return $YES			# yes, log it
	fi

	return $NO			# no, do not log it
}

logAndPrint() {

	typeset logline
	typeset log_level_str

	if [ $# -lt 1 ]; then
	  echo "Usage logAndPrint log_level string" >&2
	  return $FAIL
	fi

	typeset MYNAME=$(basename $0)
	typeset log_level=$1
	shift
	
	logLevelCheck $log_level "log_level_str"
	case $? in
	  $YES)		: ;;	# log msg
	  $NO)		return $SUCCESS ;;
	  $FAIL)	return $FAIL ;;
	esac

	logline="$MYNAME $log_level_str: $*"
	$CLULOG -p $$ -n $SVCMGR_STR -s $log_level -- "$logline"

	echo "$log_level_str: $*"		# print to stdout
}

logAndPrintFile ()
{
	typeset log_level_str
	typeset MYNAME=$(basename $0)

	if [ $# -ne 2 ]; then
	  echo "Usage logAndPrintFile log_level file" >&2
	  return $FAIL
	fi

	typeset log_level=$1
	typeset file=$2

	if [ ! -s "$file" ]; then
	  return $SUCCESS
	fi

	logLevelCheck $log_level "log_level_str"
	case $? in
	  $YES)		: ;;	# log msg
	  $NO)		return $SUCCESS ;;
	  $FAIL)	return $FAIL ;;
	esac

	while read line
	do
	  logline="$MYNAME $log_level_str: $line"
	  $CLULOG -p $$ -n $SVCMGR_STR -s $log_level "$logline"

	  echo "$log_level_str: $line"		# print to stdout
	done < "$file"
}
