#!/usr/bin/awk -f 

# usage : output_filtered_log -v comment="$1" [-v caller=$3] [-v logLevel=$2]

#	external variable definitions
# caller : process calling this logUpdate function
# comment : string to log
# logLevel : optionnal logLevel information [1::3]

# caller should be defined as following 
# caller=`cat /proc/$$/cmdline | tr '\000' '#'`
# tr invokation is needed to separate arguments and script name

BEGIN{
	printTest = 1			# do we need to print out this log message
	if (logLevel != ""){
		LogLevel["debug"]=		1
		LogLevel["warning"]=	2
		LogLevel["normal"]=		3

		HBoxLogLevelCmd = "fw_printenv HBoxLogLevel"
		if ((HBoxLogLevelCmd | getline HBoxLogLevel) >0){
			sub(".*=", "", HBoxLogLevel)			# suppress "HBoxLogLevel=" => result should be debug, warning or normal
			HBoxLogLevel = LogLevel[tolower(HBoxLogLevel)]	# get decimal value of this log level string
			if (logLevel < HBoxLogLevel){
				printTest=0		# we have do filter this log message
			}
		}
	}
	
	if (printTest){
		Initials["CAPGOOD.sh"]=               "[GCG] :"
		Initials["COM_SEL.sh"]=               "[GCS] :"
		Initials["GPIO_common"]=              "[G-C] :"
		Initials["POE_OK.sh"]=                "[GPO] :"
		Initials["modify_environment.sh"]=    "[ME-] :"
		Initials["application"]=              "[APP] :"
		Initials["update_from_directory.sh"]= "[UfD] :"
		Initials["update_survey"]=            "[US-] :"
		Initials["update.sh"]=                "[U--] :"
		Initials["S16BenchTest"]=             "[S16] :"
	
		FS="#"
		$0=caller
		caller=$2
		sub(".*\/", "", caller) # remove directory name from the caller process
		prefix= "[" caller "] :"
		if (caller in Initials)
			prefix = Initials[caller]
		print strftime ("%Y/%m/%d-%H:%M:%S"), prefix, comment
	}
}