#!/bin/ksh
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2019,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/utils/cl_log.sh 1.11 
#    
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/events/utils/cl_log.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
#   COMPONENT_NAME: EVENTUTILS
#
#   FUNCTIONS: none
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1990,1994
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
###############################################################################
#
#  Name:  cl_log
#
#  This routine logs messages to syslog and standard error. If the string
#  specified by message_id can't be found, default_message will be displayed.
#
#  Arguments:    message default_message
#  Usage:        cl_log message_id default_message 
#  Returns:      0 - success
#                2 - bad number of arguments
#  Environment:  VERBOSE_LOGGING,PATH,NLSPATH
#
###############################################################################

PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"

[[ $VERBOSE_LOGGING == high ]] && set +x

set -u 

# capture current time
TIME="$(LC_ALL=C date +"%h %e %Y %H:%M:%S")"

# Get syslog configuration file
typeset SYSLOG_CONF=""
SYSLOG_CONF=$(clgetsyslog)
# Use default configuration file for any kind of failures
if (( $? != 0 ))
then
    SYSLOG_CONF="/etc/syslog.conf"
fi
# check for required parameters
if (( $# < 2 )) ; then
   print -n -u2 "$TIME"
   echo "usage: $PROGNAME message_id default_message"
   return 2
fi

MSG_ID=$1
DEFAULT_MSG=$2

# this is the banner that surrounds error messages
MSG1=`dspmsg scripts.cat 3010 "\n!!!!!!!!!!   ERROR   !!!!!!!!!!\n"`

# only do the catalog lookup once. 
# be careful with quoting of $@, so that it gets passed as a single argument.
if (( $# > 2 )) ; then
    shift 2
    MSG=$(dspmsg scripts.cat $MSG_ID "$DEFAULT_MSG" "$@")
else
    MSG=$(dspmsg scripts.cat $MSG_ID "$DEFAULT_MSG")
fi
   
if [[ -z "$MSG" ]] ; then
	MSG="Can not find message!"
fi

# add to errlog
# note that logger adds its own timestamp on the dspmsg
logger -t "PowerHA SystemMirror for AIX" $MSG

# now print to stdout with the remaining banner
print -u2 "***************************"
print -u2 $TIME $MSG1 
print -u2 "***************************" 
print -u2 $TIME $MSG 

# special format for consumption by log analytics tools
print -u2 -- "<LAT>"\|"$(/usr/bin/date '+%Y-%m-%dT%H:%M:%S')"\|ERROR\|"$MSG"\|"</LAT>\n"

#	Find the file to which syslog goes, if any
SYSLOG_FILE=`awk '/user.notice/ { print $2 }' ${SYSLOG_CONF}`

if [[ -n "$SYSLOG_FILE" ]]
then
   # create if it doesn't exist
   [[ ! -f "$SYSLOG_FILE" ]] && touch $SYSLOG_FILE

   #
   #   Finally, synchronize the syslog file
   #
   clsynclog $SYSLOG_FILE

fi

return 0
