logging.incsh 1.9 KB
Newer Older
1 2 3 4
readonly ERROR=1
readonly WARNING=2
readonly INFO=4
readonly DEBUG=8
5
readonly ALL=15
6

7
VERBOSE_FLAGS=${ALL}
8

Lucas Sulzbach's avatar
Lucas Sulzbach committed
9 10 11 12 13 14 15 16
mapType() {
	case "${1}" in
		e | error    ) echo "${ERROR}" ;;
		w | warning  ) echo "${WARNING}" ;;
		i | info     ) echo "${INFO}" ;;
		d | debug    ) echo "${DEBUG}" ;;
                *            ) createIsoLogger -m "Invalid type '${1}'" -T "LOGGER"; return 1 ;;
	esac
17 18
}

Lucas Sulzbach's avatar
Lucas Sulzbach committed
19
createIsoLogger() {
20

Lucas Sulzbach's avatar
Lucas Sulzbach committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	TYPE="${ERROR}"
	MSG=""
	SCRIPT="$(basename "${BASH_SOURCE[1]}")"
	LINE="${BASH_LINENO[0]}"

	while true; do
    	case "${1}" in
                -t | --type    ) TYPE="$(mapType "${2}")" || return; shift; shift ;;
                -T | --tag     ) TAG="${2}"; shift; shift ;;
                -m | --message ) MSG="${2}"; shift; shift ;;
                -s | --script  ) SCRIPT="${2}"; shift; shift ;;
        	-l | --line    ) LINE="${2}"; shift; shift ;;
                -*             ) createIsoLogger -m "Unrecognized option '${1}'" -T "LOGGER"; return ;;
 	        *              ) break ;;
    	esac
	done

	if [ "$((${VERBOSE_FLAGS} & ${TYPE}))" -eq "${TYPE}" ]; then
                if [ -z "${TAG}" ]; then
                        LOG_LINE="[${SCRIPT}]: line ${LINE}: ${MSG}"
                else
                        LOG_LINE="[${SCRIPT}]: line ${LINE}: [${TAG}]: ${MSG}"
                fi
		case "${TYPE}" in
			"${ERROR}"    ) LOG_LINE="\033[38;5;9m[ERROR]: ${LOG_LINE}\033[0m" ;;
			"${WARNING}"  ) LOG_LINE="\033[38;5;11m[WARNING]: ${LOG_LINE}\033[0m" ;;
			"${INFO}"     ) LOG_LINE="\033[38;5;10m[INFO]: ${LOG_LINE}\033[0m" ;;
			"${DEBUG}"    ) LOG_LINE="\033[38;5;6m[DEBUG]: ${LOG_LINE}\033[0m" ;;
			*             ) createIsoLogger -m "Invalid type '${TYPE}'" -T "LOGGER"; return ;;
		esac
		echo -e "${LOG_LINE}" 1>&2
		echo -e "${LOG_LINE}" >> "${LOG_FILE}"
	else
		createIsoLogger -m "Tried to use disabled type '${TYPE}'" -T "LOGGER"
		return
56
	fi
57

Lucas Sulzbach's avatar
Lucas Sulzbach committed
58 59
	if [ "${TYPE}" -eq "${ERROR}" ]; then
		/bin/bash
60 61 62 63
	fi
}

true