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
#
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
4243a1
#
4243a1
# rtlp_log_msg_get_attr() - get attributes e.g. ANSI escape sequences for single log tag
4243a1
# @_rattr:	out reference to attributes e.g. ANSI escape sequences
4243a1
# @_tag:	tag to log message at
4243a1
#
4243a1
# Returns:	zero (0) on success, non-zero (>0) on failure
4243a1
#
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
4243a1
#
4243a1
# rtlp_log_printfV() - print single message to log file
4243a1
# @_attr:	attributes e.g. ANSI escape sequences to log message with
4243a1
# @_fmt_pfx:	printf(1) format string prefix
4243a1
# @_fmt:	printf(1) format string
4243a1
# @...:		parameters to printf(1) w/ @_fmt
4243a1
#
4243a1
# Returns:	zero (0) on success, non-zero (>0) on failure
4243a1
#
4243a1
rtlp_log_printfV() {
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
4243a1
			rtlp_log_printfV "" "" "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
4243a1
#
4243a1
# rtl_log_clear_tags() - clear all log tags
4243a1
#
4243a1
# Returns:	zero (0) on success, non-zero (>0) on failure
4243a1
#
Lucio Andrés Illanes Albornoz b1d432
rtl_log_clear_tags() {
4243a1
	RTLP_LOG_TAGS="";
4243a1
	return 0;
Lucio Andrés Illanes Albornoz b1d432
};
Lucio Andrés Illanes Albornoz b1d432
4243a1
#
4243a1
# rtl_log_enable_tagsV() - enable log tags
4243a1
# @...:		list of tags to enable as positional parameters
4243a1
#
4243a1
# Returns:	zero (0) on success, non-zero (>0) on failure
4243a1
#
4243a1
rtl_log_enable_tagsV() {
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
4243a1
#
4243a1
# rtl_log_env_vars() - log all environment variables to log file
4243a1
# @_tag:	tag to log environment variables at
4243a1
# @_type:	type string printed in log message header
4243a1
#
4243a1
# Returns:	zero (0) on success, non-zero (>0) on failure
4243a1
#
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
4243a1
	rtl_log_msgV "${_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_ "" " ";
4243a1
		rtl_log_msgV "${_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
4243a1
#
4243a1
# rtl_log_msgV() - log message to log file
4243a1
# @_tag:	tag to log message at
4243a1
# @_fmt:	printf(1) format string
4243a1
# @...:		parameters to printf(1) w/ @_fmt
4243a1
#
4243a1
# Returns:	zero (0) on success, non-zero (>0) on failure
4243a1
# N.B.:		Optional attributes e.g. ANSI escape sequences w/ which the the log message is to
4243a1
#		be printed are taken from the variable corresponding to @_tag named "LOG_TAG_<@_tag>",
4243a1
#		if present/set.
4243a1
#
4243a1
#		@_tag must have previously been enabled w/ rtl_log_enable_tagsV() to be logged at all.
4243a1
#
4243a1
rtl_log_msgV() {
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}";
e9fa07
		rtl_date \$_rlm3_date_now;
4243a1
		rtlp_log_printfV "${_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
4243a1
#
4243a1
# rtl_log_set_fname() - set log file pathname
4243a1
# @_fname:	pathname to new log file
4243a1
#
4243a1
# Returns:	zero (0) on success, non-zero (>0) on failure
4243a1
#
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
4243a1
#
4243a1
# rtl_log_set_no_attr() - set or clear no ANSI escape sequences in log file flag
4243a1
# @_flag:	either 0 to set or 1 to clear no ANSI escape sequences in log file flag
4243a1
#
4243a1
# Returns:	zero (0) on success, non-zero (>0) on failure
4243a1
#
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