Blob Blame History Raw
#
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 LucĂ­a Andrea Illanes Albornoz <lucia@luciaillanes.de>
# set +o errexit -o noglob -o nounset is assumed.
#

#
# Private globals and subroutines
#

RTLP_LOG_FNAME="";
RTLP_LOG_NO_ATTR=0;
RTLP_LOG_TAGS="";

rtlp_log_printf() {
	local	_rplp_attr="${1}" _rplp_fmt_pfx="${2}"	\
		_rplp_fmt="${3#*;}" _rplp_fmt_argc="${3%%;*}";
	shift 3;

	if [ "${#}" -ne "${_rplp_fmt_argc}" ]; then
		if [ "${_rplp_fmt_argc}" -eq 0 ]; then
			shift "${#}";
		else
			rtlp_log_printf "" "" "0;==> FIXME TODO XXX MESSAGE STRING ARGUMENT COUNT MISMATCH\n";
		fi;
	fi;
	_rplp_msg="$(printf "${_rplp_fmt_pfx}${_rplp_fmt}" "${@}")";

	if [ "${RTLP_LOG_FNAME:+1}" = 1 ]; then
		printf "%s\n" "${_rplp_msg}" >> "${RTLP_LOG_FNAME}";
	fi;
	if [ "${RTLP_LOG_NO_ATTR:-0}" -eq 0 ]; then
		printf "\033[0m\033[${_rplp_attr}m%s\033[0m\n" "${_rplp_msg}";
	else
		printf "%s\n" "${_rplp_msg}";
	fi;
	return 0;
};

#
# Public subroutines
#

rtl_log_clear_tags() {
	RTLP_LOG_TAGS=""; return 0;
};

rtl_log_enable_tags() {
	local IFS=","; set -- ${*};

	while [ "${#}" -gt 0 ]; do
		if ! rtl_lmatch \$RTLP_LOG_TAGS "${1}" ","; then
			RTLP_LOG_TAGS="${RTLP_LOG_TAGS:+${RTLP_LOG_TAGS},}${1}";
		fi; shift;
	done;
	return 0;
};

rtl_log_env_vars() {
	local	_rlev_tag="${1}" _rlev_type="${2}"	\
		_rlev_arg_len_max=0 _rlev_list="" _rlev_msg="" _rlev_msg_="";
	shift 2;

	rtl_log_msg "${_rlev_tag}" "${MSG_rtl_log_vars_header}" "${_rlev_type}";
	_rlev_list="${@}"; rtl_lmax \$_rlev_list \$_rlev_arg_len_max;
	while [ "${#}" -gt 0 ]; do
		rtl_get_var_unsafe \$_rlev_msg "${1#*=}";
		rtl_llift2 \$_rlev_msg \$_rlev_msg_ "" " ";
		rtl_log_msg "${_rlev_tag}"					\
			"2;%${_rlev_arg_len_max}.${_rlev_arg_len_max}s=%s"	\
			"${1%%=*}" "${_rlev_msg_}";
		shift;
	done;
	return 0;
};

rtl_log_msg() {
	local	_rlm3_tag="${1}" _rlm3_fmt="${2}"	\
		_rlm3_attr="" _rlm3_date_now=0 _rlm3_exitfl=0;
	shift 2;

	if [ "x${_rlm3_tag}" = "xfatalexit" ]; then
		_rlm3_tag="fatal"; _rlm3_exitfl=1;
	fi;

	if [ "${_rlm3_tag}" = "fatal" ]\
	|| rtl_lmatch \$RTLP_LOG_TAGS "${_rlm3_tag}" ",";
	then
		eval _rlm3_attr='${LOG_TAG_'"${_rlm3_tag}"':-}';
		if [ "${#_rlm3_attr}" -eq 0 ]; then
			rtlp_log_printf "" "" "0;==> FIXME TODO XXX UNKNOWN TAG \`${_rlm3_tag}' PASSED TO rtl_log_msg()\n";
		fi;
		rtl_date \$_rlm3_date_now;
		rtlp_log_printf "${_rlm3_attr}" "==> ${_rlm3_date_now} " "${_rlm3_fmt}" "${@}";
		if [ "${_rlm3_exitfl}" -eq 1 ]; then
			exit 1;
		fi;
	fi;
	return 0;
};

rtl_log_set_fname() {
	RTLP_LOG_FNAME="${1}";
	return 0;
};

rtl_log_set_no_attr() {
	RTLP_LOG_NO_ATTR="${1}";
	return 0;
};

# vim:filetype=sh textwidth=0