Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
#
8d7a8a
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 LucĂ­a Andrea Illanes Albornoz <lucia@luciaillanes.de>
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
#
82f78c
# Private globals
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="";
82f78c
RTLP_LOG_TERMINAL_COLOURS="$(tput colors 2>/dev/null)";
82f78c
82f78c
#
82f78c
# Private subroutines
82f78c
#
82f78c
82f78c
rtlp_log_msg_get_attr() {
82f78c
	local	_rplmga_rattr="${1#\$}" _rplmga_tag="${2}"	\
82f78c
		_rplmga_attr="" _rplmga_attr_state=""		\
82f78c
		_rplmga_term_colour=0;
82f78c
82f78c
	if [ "${RTLP_LOG_TERMINAL_COLOURS}" = 256 ]; then
82f78c
		set -- 256 "";
82f78c
	else
82f78c
		set -- "";
82f78c
	fi;
82f78c
82f78c
	for _rplmga_term_colour in "${@}"; do
82f78c
		eval _rplmga_attr='${LOG_TAG_'"${_rplmga_tag}${_rplmga_term_colour:+_${_rplmga_term_colour}}"':-}';
82f78c
82f78c
		if [ "${_rplmga_attr:+1}" = 1 ]; then
82f78c
			break;
82f78c
		else
82f78c
			eval _rplmga_attr_state='${RTLP_ATTR_STATE_'"${_rplmga_tag}"':-}';
82f78c
			if [ "${_rplmga_attr_state:+1}" != 1 ]; then
82f78c
				_rplmga_attr_state=0;
82f78c
			else
82f78c
				: $((_rplmga_attr_state += 1));
82f78c
			fi;
82f78c
			eval "RTLP_ATTR_STATE_${_rplmga_tag}=${_rplmga_attr_state}";
82f78c
82f78c
			if [ "$((${_rplmga_attr_state} % 2))" -eq 0 ]; then
82f78c
				eval _rplmga_attr='${LOG_TAG_'"${_rplmga_tag}"'_even'"${_rplmga_term_colour:+_${_rplmga_term_colour}}"':-}';
82f78c
			else
82f78c
				eval _rplmga_attr='${LOG_TAG_'"${_rplmga_tag}"'_odd'"${_rplmga_term_colour:+_${_rplmga_term_colour}}"':-}';
82f78c
			fi;
82f78c
82f78c
			if [ "${_rplmga_attr:+1}" = 1 ]; then
82f78c
				break;
82f78c
			fi;
82f78c
		fi;
82f78c
	done;
82f78c
82f78c
	eval ${_rplmga_rattr}='${_rplmga_attr}';
82f78c
	return 0;
82f78c
};
Lucio Andrés Illanes Albornoz ae3ff0
Lucio Andrés Illanes Albornoz b6a9a1
rtlp_log_printf() {
e9fa07
	local	_rplp_attr="${1}" _rplp_fmt_pfx="${2}"	\
e9fa07
		_rplp_fmt="${3#*;}" _rplp_fmt_argc="${3%%;*}";
e9fa07
	shift 3;
e9fa07
e9fa07
	if [ "${#}" -ne "${_rplp_fmt_argc}" ]; then
e9fa07
		if [ "${_rplp_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;
e9fa07
	_rplp_msg="$(printf "${_rplp_fmt_pfx}${_rplp_fmt}" "${@}")";
Lucio Andrés Illanes Albornoz b1d432
e9fa07
	if [ "${RTLP_LOG_FNAME:+1}" = 1 ]; then
e9fa07
		printf "%s\n" "${_rplp_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
e9fa07
		printf "\033[0m\033[${_rplp_attr}m%s\033[0m\n" "${_rplp_msg}";
Lucio Andrés Illanes Albornoz b6a9a1
	else
e9fa07
		printf "%s\n" "${_rplp_msg}";
e9fa07
	fi;
e9fa07
	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 -- ${*};
e9fa07
Lucio Andrés Illanes Albornoz b1d432
	while [ "${#}" -gt 0 ]; do
e9fa07
		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;
e9fa07
	done;
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz b1d432
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz c6d6e0
rtl_log_env_vars() {
e9fa07
	local	_rlev_tag="${1}" _rlev_type="${2}"	\
e9fa07
		_rlev_arg_len_max=0 _rlev_list="" _rlev_msg="" _rlev_msg_="";
e9fa07
	shift 2;
Lucio Andrés Illanes Albornoz b1d432
e9fa07
	rtl_log_msg "${_rlev_tag}" "${MSG_rtl_log_vars_header}" "${_rlev_type}";
e9fa07
	_rlev_list="${@}"; rtl_lmax \$_rlev_list \$_rlev_arg_len_max;
Lucio Andrés Illanes Albornoz b6a9a1
	while [ "${#}" -gt 0 ]; do
e9fa07
		rtl_get_var_unsafe \$_rlev_msg "${1#*=}";
e9fa07
		rtl_llift2 \$_rlev_msg \$_rlev_msg_ "" " ";
e9fa07
		rtl_log_msg "${_rlev_tag}"					\
e9fa07
			"2;%${_rlev_arg_len_max}.${_rlev_arg_len_max}s=%s"	\
e9fa07
			"${1%%=*}" "${_rlev_msg_}";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
		shift;
e9fa07
	done;
e9fa07
	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() {
e9fa07
	local	_rlm3_tag="${1}" _rlm3_fmt="${2}"	\
82f78c
		_rlm3_attr="" _rlm3_date_now=0 _rlm3_exitfl=0
e9fa07
	shift 2;
Lucio Andrés Illanes Albornoz b1d432
e9fa07
	if [ "x${_rlm3_tag}" = "xfatalexit" ]; then
e9fa07
		_rlm3_tag="fatal"; _rlm3_exitfl=1;
e9fa07
	fi;
e9fa07
989546
	if [ "${_rlm3_tag}" = "fatal" ]\
989546
	|| rtl_lmatch \$RTLP_LOG_TAGS "${_rlm3_tag}" ",";
989546
	then
82f78c
		rtlp_log_msg_get_attr \$_rlm3_attr "${_rlm3_tag}";
82f78c
		if [ "${_rlm3_attr:+1}" != 1 ]; then
e9fa07
			rtlp_log_printf "" "" "0;==> ?[4;41;97mFIXME TODO XXX UNKNOWN TAG \`${_rlm3_tag}' PASSED TO rtl_log_msg()?[0m\n";
Lucio Andrés Illanes Albornoz b1d432
		fi;
e9fa07
		rtl_date \$_rlm3_date_now;
e9fa07
		rtlp_log_printf "${_rlm3_attr}" "==> ${_rlm3_date_now} " "${_rlm3_fmt}" "${@}";
e9fa07
		if [ "${_rlm3_exitfl}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz b1d432
			exit 1;
Lucio Andrés Illanes Albornoz b1d432
		fi;
e9fa07
	fi;
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz b1d432
};
Lucio Andrés Illanes Albornoz b1d432
Lucio Andrés Illanes Albornoz b1d432
rtl_log_set_fname() {
e9fa07
	RTLP_LOG_FNAME="${1}";
e9fa07
	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() {
e9fa07
	RTLP_LOG_NO_ATTR="${1}";
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
};
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
64844b
# vim:filetype=sh textwidth=0