Blame subr/rtl_log.subr

Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
#
Lucio Andrés Illanes Albornoz e1d469
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
#
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
Lucio Andrés Illanes Albornoz b6a9a1
#
Lucio Andrés Illanes Albornoz b6a9a1
# Private globals and subroutines
Lucio Andrés Illanes Albornoz b6a9a1
#
Lucio Andrés Illanes Albornoz b6a9a1
RTLP_LOG_NO_ATTR=0;
Lucio Andrés Illanes Albornoz b6a9a1
RTLP_LOG_FNAME="";
Lucio Andrés Illanes Albornoz b6a9a1
RTLP_LOG_LVL="0";
Lucio Andrés Illanes Albornoz b6a9a1
rtl_log_set_fname() { RTLP_LOG_FNAME="${1}"; };
Lucio Andrés Illanes Albornoz b6a9a1
rtl_log_set_lvl() { RTLP_LOG_LVL="${1}"; };
Lucio Andrés Illanes Albornoz b6a9a1
rtl_log_set_no_attr() { RTLP_LOG_NO_ATTR="${1}"; };
Lucio Andrés Illanes Albornoz ae3ff0
Lucio Andrés Illanes Albornoz b6a9a1
rtlp_log_printf() {
Lucio Andrés Illanes Albornoz b6a9a1
	local _attr="${1}" _fmt="${2}"; shift 2; _msg="$(printf "${_fmt}" "${@}")";
Lucio Andrés Illanes Albornoz b6a9a1
	if [ -n "${RTLP_LOG_FNAME}" ]; then
Lucio Andrés Illanes Albornoz b6a9a1
		printf "%s\n" "${_msg}" >> "${RTLP_LOG_FNAME}";
Lucio Andrés Illanes Albornoz b6a9a1
	fi;
Lucio Andrés Illanes Albornoz b6a9a1
	if [ "${RTLP_LOG_NO_ATTR:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz b6a9a1
		printf "\033[0m\033[${_attr}m%s\033[0m\n" "${_msg}";
Lucio Andrés Illanes Albornoz b6a9a1
	else
Lucio Andrés Illanes Albornoz b6a9a1
		printf "%s\n" "${_msg}";
Lucio Andrés Illanes Albornoz (arab, vxp) a43dbf
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) a43dbf
};
Lucio Andrés Illanes Albornoz (arab, vxp) a43dbf
Lucio Andrés Illanes Albornoz b6a9a1
#
Lucio Andrés Illanes Albornoz b6a9a1
# Public globals
Lucio Andrés Illanes Albornoz b6a9a1
#
Lucio Andrés Illanes Albornoz b6a9a1
RTL_LOG_MSG_FATAL_COLOUR=91;		# Bright red
Lucio Andrés Illanes Albornoz b6a9a1
RTL_LOG_MSG_WARNING_COLOUR=31;		# Dark red
Lucio Andrés Illanes Albornoz b6a9a1
RTL_LOG_MSG_SUCCESS_COLOUR=33;		# Dark yellow
Lucio Andrés Illanes Albornoz b6a9a1
RTL_LOG_MSG_SUCCESS_END_COLOUR=32;	# Dark green
Lucio Andrés Illanes Albornoz b6a9a1
RTL_LOG_MSG_INFO_COLOUR=93;		# Bright yellow
Lucio Andrés Illanes Albornoz b6a9a1
RTL_LOG_MSG_INFO_END_COLOUR=92;		# Bright green
Lucio Andrés Illanes Albornoz b6a9a1
RTL_LOG_MSG_NOTICE_COLOUR=96;		# Bright cyan
Lucio Andrés Illanes Albornoz b6a9a1
RTL_LOG_MSG_VERBOSE_COLOUR=90;		# Dark grey
Lucio Andrés Illanes Albornoz b6a9a1
RTL_LOG_MSG_DEBUG_COLOUR=36;		# Dark cyan
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz c6d6e0
rtl_log_env_vars() {
Lucio Andrés Illanes Albornoz b6a9a1
	local _arg_len_max=0;
Lucio Andrés Illanes Albornoz c6d6e0
	rtl_log_msg info "Variables for this ${1:-build}:"; shift;
Lucio Andrés Illanes Albornoz b6a9a1
	_arg_len_max="$(rtl_lmax "${@}")";
Lucio Andrés Illanes Albornoz b6a9a1
	while [ "${#}" -gt 0 ]; do
Lucio Andrés Illanes Albornoz b6a9a1
		rtl_log_msg info				\
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
			"%${_arg_len_max}.${_arg_len_max}s=%s"	\
Lucio Andrés Illanes Albornoz b6a9a1
			"${1%%=*}" "$(rtl_get_var_unsafe "${1#*=}")";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
		shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
};
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
Lucio Andrés Illanes Albornoz c6d6e0
rtl_log_msg() {
Lucio Andrés Illanes Albornoz b6a9a1
	local _lvl="${1}" _fmt="${2}" _attr=""; shift 2;
Lucio Andrés Illanes Albornoz b6a9a1
	case "${RTLP_LOG_LVL:-0}" in
Lucio Andrés Illanes Albornoz b6a9a1
	0)	rtl_lmatch "notice verbose debug" "${_lvl}" && return; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	1)	rtl_lmatch "verbose debug" "${_lvl}" && return; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	2)	rtl_lmatch "debug" "${_lvl}" && return; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	3)	;;
Lucio Andrés Illanes Albornoz b6a9a1
	esac;
Lucio Andrés Illanes Albornoz b6a9a1
	case "${_lvl}" in
Lucio Andrés Illanes Albornoz b6a9a1
	fatal|fatalexit)	_attr="${RTL_LOG_MSG_FATAL_COLOUR}"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	warning)		_attr="${RTL_LOG_MSG_WARNING_COLOUR}"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	success)		_attr="${RTL_LOG_MSG_SUCCESS_COLOUR}"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	success_end)		_attr="${RTL_LOG_MSG_SUCCESS_END_COLOUR}"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	info)			_attr="${RTL_LOG_MSG_INFO_COLOUR}"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	info_end)		_attr="${RTL_LOG_MSG_INFO_END_COLOUR}"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	notice)			_attr="${RTL_LOG_MSG_NOTICE_COLOUR}"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	verbose)		_attr="${RTL_LOG_MSG_VERBOSE_COLOUR}"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	debug)			_attr="${RTL_LOG_MSG_DEBUG_COLOUR}"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
	esac;
Lucio Andrés Illanes Albornoz b6a9a1
	rtlp_log_printf "${_attr}" "==> %s ${_fmt}" "$(rtl_date)" "${@}";
Lucio Andrés Illanes Albornoz b6a9a1
	if [ "x${_lvl}" = "xfatalexit" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) a43dbf
		exit 1;
Lucio Andrés Illanes Albornoz (arab, vxp) a43dbf
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
};
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
# vim:filetype=sh