#!/bin/bash 
function usage {
echo "
   errlog prints messages into the error log. That is, it can be called by
   a script to put error or informational messages into the error log so
   that they appear in the cmlogviewer, and can be retrieved later for
   diagnostics.
  
   Usage: errlog [-hq][-f facility] message text 

   Options:

      -h (optional) Prints help

      -q (optional) Quiet. Don't echo the message text to stderr 
         (your terminal or log file). The default operation is that 
         errlog will print both to cmlog, and to the stderr of its 
         own process.

      -f (optional) The name of a program or subsystem that originated 
         this message text. If given, it will be printed in the 'facility'
         column of cmlogviewer. If ommitted, 'errlog' will be written
         into the facility name, but please avoid that.

      -b (optional) Block until the connection to the error log has
         been comopleted! This is an option for use by system administrators,
         to verify the Error logging network. The default is to connect
         and issue the message in the background, so that errlog takes
         no time from the calling process flow.

   Examples:
     
      1) errlog -h  
           
         Prints this help
        
      2) errlog -f Oracle Uploading oracle failed
  
         Prints 'Uploading oracle failed' to cmlog's text column
         and facility column 'Oracle'. It also prints a message like
         'Thu Sep 16 19:22:32 PDT 2010: Uploading oracle failed'
         to the screen of the terminal from which errlog was run.

      3) errlog -q -f Oracle Uploading oracle failed

         Like 2 above, but without the printing to local screen.

      4) errlog -b periodic test

         Issues the message blocking until completion. Useful for debugging.
         Don't use this unless you know what you're doing.
"
}
#========================================================
# Auth: 04-Oct-2010, Greg White (greg)
# Rev:  none
#========================================================

# Initialization
# Note this relies also on the default classpath in the ENVS.bash environment.
export CLASSPATH=$PHYSICS_TOP/misc/lib/misc.jar:$CLASSPATH

# Process given options
facility=`basename $0`
quiet=0
background=0
while getopts hqbf: opt
do
   case "$opt" in
       h) usage; exit 0 ;;
       q) quiet=1;;
       f) facility="$OPTARG";;
       b) background=1;;
       *) echo "Unknown option, see errlog -h"; exit 1;;
   esac
done
shift $((OPTIND-1))

# Check for insufficient arguments
if [ $# -lt 1 ]; then
   echo "Too few arguments, see errlog -h for help."
   exit 1;
fi

# Normally this program would run errlog in the background, both
# for speed, and also so it doesn't block the caller should errlog
# fail. But if -b is given, we deliberatly don't do that, so errlog
# can be run to check if it *can* connect to the errolog network.
# If it can't (errlog would hang for 5 minutes) then the watchdog
# system will restart both the processes in the error logging system.
# Note. The "wait" statements are to stop the effect of & making a
# script appear to hang (athough it isn't).
# 
if [ $background -eq 1 ]; then
   if [ $quiet -eq 0 ]; then
      eval java Errlog "$facility" $@ &
      wait
   else
      eval java Errlog "$facility" $@ 2> /dev/null &
      wait
   fi
else
   if [ $quiet -eq 0 ]; then
      eval java Errlog "$facility" $@
   else
      eval java Errlog "$facility" $@ 2> /dev/null
   fi
fi

exit $?

