Blame subr.rtl/rtl_log.subr

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
#
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() {
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}"	\
e9fa07
		_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
e9fa07
	if rtl_lmatch \$RTLP_LOG_TAGS "${_rlm3_tag}" ","; then
e9fa07
		eval _rlm3_attr='${LOG_TAG_'"${_rlm3_tag}"':-}';
e9fa07
		if [ "${#_rlm3_attr}" -eq 0 ]; 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