#!/bin/sh # @(#) bumsg.fnc ver20140130 # # Description: # Functions for logging messages. # Message text will supply by *.msg file. # # @author Toshiki IGA # @version 1.0 2014-01-29 created newly. ############################################################################## ############################################################################## # bashutil # Copyright (C) 2014 Toshiki IGA # # This library is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # 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 Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this library. If not, see . ############################################################################## ############################################################################## # Copyright 2014 Toshiki IGA and others. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ############################################################################## LOGGER="/usr/bin/logger -t $0 -i" ######################################## # Write message to specified media. # # @param $1 message id # @param $2 argument 1 # @param $3 argument 2 # @param $4 argument 3 # @param $5 argument 4 # @param $6 argument 5 # @param $7 argument 6 # @param $8 argument 7 # @param $9 argument 8 # @param ${10} argument 9 # @param ${11} argument 10 logmsg() { local msgid="$1" if [ ${#msgid} = 0 ]; then echo "[bumsg.fnc#logmsg][error]Illegal argument: message id must be specified." 1>&2 return 101 fi local strmsg="`getmsg \"$1\" \"$2\" \"$3\" \"$4\" \"$5\" \"$6\" \"$7\" \"$8\" \"$9\" \"${10}\" \"${11}\"`" ismsgEnvSysout "${msgid}" local issysout=$? ismsgEnvSyserr "${msgid}" local issyserr=$? ismsgEnvLogger "${msgid}" local islogger=$? if [ ${issysout} = 0 ]; then echo "${strmsg}" fi if [ ${issyserr} = 0 ]; then echo "${strmsg}" 1>&2 fi if [ ${islogger} = 0 ]; then ${LOGGER} "${strmsg}" fi return 0 } ######################################## # Get message from message environment. # # @param $1 message id # @param $2 argument 1 # @param $3 argument 2 # @param $4 argument 3 # @param $5 argument 4 # @param $6 argument 5 # @param $7 argument 6 # @param $8 argument 7 # @param $9 argument 8 # @param ${10} argument 9 # @param ${11} argument 10 # @echo message text getmsg() { local msgid="$1" local arg1="$2" local arg2="$3" local arg3="$4" local arg4="$5" local arg5="$6" local arg6="$7" local arg7="$8" local arg8="$9" local arg9="${10}" local arg10="${11}" if [ ${#msgid} = 0 ]; then echo "[bumsg.fnc#getmsg][error]Illegal argument: message id must be specified." 1>&2 return 101 fi local msgtextkey="`getmsgIdEnvKey ${msgid}`" local msgtextkeyString="`getmsgEnvValue ${msgtextkey}`" if [ ${#msgtextkeyString} = 0 ]; then echo "[bumsg.fnc#getmsg][error]Message not found for ${msgtextkey}" 1>&2 return 102 fi local printfString="" ismsgEnvEmbedid "${msgid}" local isembedid=$? if [ ${isembedid} = 0 ]; then printfString="${printfString}[${msgid}]" fi printfString="${printfString}${msgtextkeyString}" local outMessage="" if [ ${#arg1} = 0 ]; then outMessage="`printf \"${printfString}\"`" elif [ ${#arg2} = 0 ]; then outMessage="`printf \"${printfString}\" \"${arg1}\"`" elif [ ${#arg3} = 0 ]; then outMessage="`printf \"${printfString}\" \"${arg1}\" \"${arg2}\"`" elif [ ${#arg4} = 0 ]; then outMessage="`printf \"${printfString}\" \"${arg1}\" \"${arg2}\" \"${arg3}\"`" elif [ ${#arg5} = 0 ]; then outMessage="`printf \"${printfString}\" \"${arg1}\" \"${arg2}\" \"${arg3}\" \"${arg4}\"`" elif [ ${#arg6} = 0 ]; then outMessage="`printf \"${printfString}\" \"${arg1}\" \"${arg2}\" \"${arg3}\" \"${arg4}\" \"${arg5}\"`" elif [ ${#arg7} = 0 ]; then outMessage="`printf \"${printfString}\" \"${arg1}\" \"${arg2}\" \"${arg3}\" \"${arg4}\" \"${arg5}\" \"${arg6}\"`" elif [ ${#arg8} = 0 ]; then outMessage="`printf \"${printfString}\" \"${arg1}\" \"${arg2}\" \"${arg3}\" \"${arg4}\" \"${arg5}\" \"${arg6}\" \"${arg7}\"`" elif [ ${#arg9} = 0 ]; then outMessage="`printf \"${printfString}\" \"${arg1}\" \"${arg2}\" \"${arg3}\" \"${arg4}\" \"${arg5}\" \"${arg6}\" \"${arg7}\" \"${arg8}\"`" elif [ ${#arg10} = 0 ]; then outMessage="`printf \"${printfString}\" \"${arg1}\" \"${arg2}\" \"${arg3}\" \"${arg4}\" \"${arg5}\" \"${arg6}\" \"${arg7}\" \"${arg8}\" \"${arg9}\"`" else outMessage="`printf \"${printfString}\" \"${arg1}\" \"${arg2}\" \"${arg3}\" \"${arg4}\" \"${arg5}\" \"${arg6}\" \"${arg7}\" \"${arg8}\" \"${arg9}\" \"${arg10}\"`" fi echo "${outMessage}" return 0 } ######################################## # Get message id environment key. # # @param $1 message id # @echo message id environment key getmsgIdEnvKey() { local msgid="$1" local msgtextkey='${MSG_'"${msgid}"'}' echo "${msgtextkey}" return 0 } ######################################## # Get message environment value. # # @param $1 environment key # @echo message environment value getmsgEnvValue() { local envkey="$1" local msgtextkeyString="`eval \"echo ${envkey}\"`" echo "${msgtextkeyString}" return 0 } ######################################## # Get message environment boolean value. # # @param $1 environment key # @return 0:true 1:false 2:unknown getmsgEnvBooleanValue() { local envkey="$1" local envvalue="`getmsgEnvValue \"${envkey}\"`" if [ "${envvalue}" = "true" ]; then return 0 fi if [ "${envvalue}" = "True" ]; then return 0 fi if [ "${envvalue}" = "TRUE" ]; then return 0 fi if [ "${envvalue}" = "false" ]; then return 1 fi if [ "${envvalue}" = "False" ]; then return 1 fi if [ "${envvalue}" = "FALSE" ]; then return 1 fi return 2 } ######################################## # Check write sysout or not. # # @param $1 message id # @return 0:do output 1:none ismsgEnvSysout() { local msgid="$1" local envkey="`getmsgIdEnvKey \"${msgid}\"'_sysout'`" getmsgEnvBooleanValue "${envkey}" local envvalue=$? case "${envvalue}" in "0" ) return 0 ;; "1" ) return 1 ;; "2" ) return 1 ;; * ) return 1 ;; esac } ######################################## # Check write syserr or not. # # @param $1 message id # @return 0:do output 1:none ismsgEnvSyserr() { local msgid="$1" local envkey="`getmsgIdEnvKey \"${msgid}\"'_syserr'`" getmsgEnvBooleanValue "${envkey}" local envvalue=$? case "${envvalue}" in "0" ) return 0 ;; "1" ) return 1 ;; "2" ) return 0 ;; * ) return 0 ;; esac } ######################################## # Check write logger(syslog) or not. # # @param $1 message id # @return 0:do output 1:none ismsgEnvLogger() { local msgid="$1" local envkey="`getmsgIdEnvKey \"${msgid}\"'_logger'`" getmsgEnvBooleanValue "${envkey}" local envvalue=$? case "${envvalue}" in "0" ) return 0 ;; "1" ) return 1 ;; "2" ) return 1 ;; * ) return 1 ;; esac } ######################################## # Check embed message id or not. # # @param $1 message id # @return 0:do output 1:none ismsgEnvEmbedid() { local msgid="$1" local envkey="`getmsgIdEnvKey \"${msgid}\"'_embedid'`" getmsgEnvBooleanValue "${envkey}" local envvalue=$? case "${envvalue}" in "0" ) return 0 ;; "1" ) return 1 ;; "2" ) return 1 ;; * ) return 1 ;; esac }