Blame subr.rtl/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 b1d432
Lucio Andrés Illanes Albornoz b6a9a1
RTLP_LOG_FNAME="";
Lucio Andrés Illanes Albornoz b1d432
RTLP_LOG_NO_ATTR=0;
Lucio Andrés Illanes Albornoz b1d432
RTLP_LOG_TAGS="";
Lucio Andrés Illanes Albornoz ae3ff0
Lucio Andrés Illanes Albornoz b6a9a1
rtlp_log_printf() {
Lucio Andrés Illanes Albornoz dc07ba
	local _attr="${1}" _fmt_pfx="${2}" _fmt="${3#*;}" _fmt_argc="${3%%;*}"; shift 3;
Lucio Andrés Illanes Albornoz dc07ba
	if [ "${#}" -ne "${_fmt_argc}" ]; then
Lucio Andrés Illanes Albornoz dc07ba
		if [ "${_fmt_argc}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz dc07ba
			shift "${#}";
Lucio Andrés Illanes Albornoz dc07ba
		else
Lucio Andrés Illanes Albornoz dc07ba
			rtlp_log_printf "" "" "0;==> ?[4;41;97mFIXME TODO XXX MESSAGE STRING ARGUMENT COUNT MISMATCH?[0m\n";
Lucio Andrés Illanes Albornoz dc07ba
		fi;
Lucio Andrés Illanes Albornoz dc07ba
	fi;
Lucio Andrés Illanes Albornoz dc07ba
	_msg="$(printf "${_fmt_pfx}${_fmt}" "${@}")";
Lucio Andrés Illanes Albornoz b1d432
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 b1d432
	fi; return 0;
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 b1d432
# Public subroutines
Lucio Andrés Illanes Albornoz b6a9a1
#
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz b1d432
rtl_log_clear_tags() {
Lucio Andrés Illanes Albornoz b1d432
	RTLP_LOG_TAGS=""; return 0;
Lucio Andrés Illanes Albornoz b1d432
};
Lucio Andrés Illanes Albornoz b1d432
Lucio Andrés Illanes Albornoz b1d432
rtl_log_enable_tags() {
Lucio Andrés Illanes Albornoz b1d432
	local IFS=","; set -- ${*};
Lucio Andrés Illanes Albornoz b1d432
	while [ "${#}" -gt 0 ]; do
Lucio Andrés Illanes Albornoz b1d432
		if ! rtl_lmatch "${RTLP_LOG_TAGS}" "${1}" ","; then
Lucio Andrés Illanes Albornoz b1d432
			RTLP_LOG_TAGS="${RTLP_LOG_TAGS:+${RTLP_LOG_TAGS},}${1}";
Lucio Andrés Illanes Albornoz b1d432
		fi; shift;
Lucio Andrés Illanes Albornoz b1d432
	done; return 0;
Lucio Andrés Illanes Albornoz b1d432
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz c6d6e0
rtl_log_env_vars() {
Lucio Andrés Illanes Albornoz b1d432
	local _tag="${1}" _type="${2}" _arg_len_max=0; shift 2;
Lucio Andrés Illanes Albornoz b1d432
Lucio Andrés Illanes Albornoz dc07ba
	rtl_log_msg "${_tag}" "${MSG_rtl_log_vars_header}" "${_type}";
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 dc07ba
		rtl_log_msg "${_tag}"					\
Lucio Andrés Illanes Albornoz dc07ba
			"2;%${_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 b1d432
	done; return 0;
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 b1d432
	local _tag="${1}" _fmt="${2}" _attr="" _exitfl=0; shift 2;
Lucio Andrés Illanes Albornoz b1d432
Lucio Andrés Illanes Albornoz b1d432
	[ "x${_tag}" = "xfatalexit" ] && { _tag="fatal"; _exitfl=1; };
Lucio Andrés Illanes Albornoz b1d432
	if rtl_lmatch "${RTLP_LOG_TAGS}" "${_tag}" ","; then
Lucio Andrés Illanes Albornoz b1d432
		eval _attr='${LOG_TAG_'"${_tag}"':-}';
Lucio Andrés Illanes Albornoz b1d432
		if [ "${#_attr}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz dc07ba
			rtlp_log_printf "" "" "0;==> ?[4;41;97mFIXME TODO XXX UNKNOWN TAG \`${_tag}' PASSED TO rtl_log_msg()?[0m\n";
Lucio Andrés Illanes Albornoz b1d432
		fi;
Lucio Andrés Illanes Albornoz dc07ba
		rtlp_log_printf "${_attr}" "==> $(rtl_date) " "${_fmt}" "${@}";
Lucio Andrés Illanes Albornoz b1d432
		if [ "${_exitfl}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz b1d432
			exit 1;
Lucio Andrés Illanes Albornoz b1d432
		fi;
Lucio Andrés Illanes Albornoz b1d432
	fi; return 0;
Lucio Andrés Illanes Albornoz b1d432
};
Lucio Andrés Illanes Albornoz b1d432
Lucio Andrés Illanes Albornoz b1d432
rtl_log_set_fname() {
Lucio Andrés Illanes Albornoz b1d432
	RTLP_LOG_FNAME="${1}"; return 0;
Lucio Andrés Illanes Albornoz b1d432
};
Lucio Andrés Illanes Albornoz b1d432
Lucio Andrés Illanes Albornoz b1d432
rtl_log_set_no_attr() {
Lucio Andrés Illanes Albornoz b1d432
	RTLP_LOG_NO_ATTR="${1}"; return 0;
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