armbian_build/lib/functions/logging/logging.sh
Ricardo Pardini d24f363187
armbian-oleg: logging: new logfile format; SHOW_LOG=yes default; introduce DEBUG=yes
- default `SHOW_LOG=yes` if user on terminal
- shortcut: `DEBUG=yes` defaults both `SHOW_LOG=yes` & `SHOW_DEBUG=yes`
- much simpler logging format for log file
- skip all display_alert()'s more verbose than DEBUG from logfile
  - set `DEBUG=yes` to log everything again
- skip Markdown assets from being included in ANSI log
- use of `ccze`
- introduce internal var for controlling COMMAND-level log
- remove unused `run_on_sdcard()` that somehow was left there
- drop `export_html_logs()` and `EXPORT_HTML_LOG` -- are no more
2023-02-18 07:42:20 -03:00

111 lines
4.3 KiB
Bash

#!/usr/bin/env bash
function logging_init() {
# defaults.
# if stdout is a terminal, then default SHOW_LOG to yes
[[ -t 1 ]] && declare -g SHOW_LOG="${SHOW_LOG:-"yes"}"
# if DEBUG=yes (via env only, at this point) is set then default both log & debug to yes
if [[ "${DEBUG}" == "yes" ]]; then
declare -g SHOW_LOG="${SHOW_LOG:-"yes"}"
declare -g SHOW_DEBUG="${SHOW_DEBUG:-"yes"}"
fi
# globals
declare -g padding="" left_marker="[" right_marker="]"
declare -g normal_color="\x1B[0m" gray_color="\e[1;30m" # "bright black", which is grey
declare -i logging_section_counter=0 # -i: integer
declare -g logging_section_counter
declare -g tool_color="${gray_color}" # default to gray... (should be ok on terminals)
if [[ "${CI}" == "true" ]]; then # ... but that is too dark for Github Actions
declare -g tool_color="${normal_color}"
declare -g SHOW_LOG="${SHOW_LOG:-"yes"}" # if in CI/GHA, default to showing log
fi
if [[ "${ARMBIAN_RUNNING_IN_CONTAINER}" == "yes" ]]; then # if in container, add a cyan "whale emoji" to the left marker wrapped in dark gray brackets
local container_emoji="🐳" # 🐳 or 🐋
declare -g left_marker="${gray_color}[${container_emoji}|${normal_color}"
elif [[ "$(uname -s)" == "Darwin" ]]; then # if on Mac, add a an apple emoji to the left marker wrapped in dark gray brackets
local mac_emoji="🍏" # 🍏 or 🍎
declare -g left_marker="${gray_color}[${mac_emoji}|${normal_color}"
fi
}
function logging_error_show_log() {
local logfile_to_show="${CURRENT_LOGFILE}" # store current logfile in separate var
unset CURRENT_LOGFILE # stop logging, otherwise crazy
[[ "${SHOW_LOG}" == "yes" ]] && return 0 # Do nothing if we're already showing the log on stderr.
if [[ "${CI}" == "true" ]]; then # Close opened CI group, even if there is none; errors would be buried otherwise.
echo "::endgroup::"
fi
if [[ -f "${logfile_to_show}" ]]; then
local prefix_sed_contents="${normal_color}${left_marker}${padding}👉${padding}${right_marker} "
local prefix_sed_cmd="s/^/${prefix_sed_contents}/;"
display_alert " 👇👇👇 Showing logfile below 👇👇👇" "${logfile_to_show}" "err"
# shellcheck disable=SC2002 # my cat is great. thank you, shellcheck.
cat "${logfile_to_show}" | grep -v -e "^$" | sed -e "${prefix_sed_cmd}" 1>&2 # write it to stderr!!
display_alert " 👆👆👆 Showing logfile above 👆👆👆" "${logfile_to_show}" "err"
else
display_alert "✋ Error log not available at this stage of build" "check messages above" "debug"
fi
return 0
}
# usage example:
# declare -a verbose_params=() && if_user_on_terminal_and_not_logging_add verbose_params "--verbose" "--progress"
# echo "here is the verbose params: ${verbose_params[*]}"
function if_user_on_terminal_and_not_logging_add() {
# Is user on a terminal? if not, do nothing.
if [[ ! -t 1 ]]; then
return 0
fi
# are we running under a logging section? if so, do nothing.
if [[ -n "${CURRENT_LOGGING_SECTION}" ]]; then
return 0
fi
# If we're here, we're on a terminal and not logging.
declare -n _add_to="$1" # nameref to an array; can't use '-a' here
shift
_add_to+=("$@")
return 0
}
function if_user_not_on_terminal_or_is_logging_add() {
# Is user on a terminal? if yes, do nothing.
if [[ -t 1 ]]; then
return 0
fi
# are we running under a logging section? if not, do nothing.
if [[ -z "${CURRENT_LOGGING_SECTION}" ]]; then
return 0
fi
declare -n _add_to_inverse="$1" # nameref to an array; can't use '-a' here
shift
_add_to_inverse+=("$@")
return 0
}
# This takes LOG_ASSET, which can and should include an extension.
function do_with_log_asset() {
# @TODO: check that CURRENT_LOGGING_COUNTER is set, otherwise crazy?
local ASSET_LOGFILE="${CURRENT_LOGGING_DIR}/${CURRENT_LOGGING_COUNTER}.${LOG_ASSET}"
display_alert "Logging to asset" "${CURRENT_LOGGING_COUNTER}.${LOG_ASSET}" "debug"
"$@" >> "${ASSET_LOGFILE}"
}
function print_current_asset_log_base_file() {
declare ASSET_LOGFILE_BASE="${CURRENT_LOGGING_DIR}/${CURRENT_LOGGING_COUNTER}."
echo -n "${ASSET_LOGFILE_BASE}"
}
function discard_logs_tmp_dir() {
# Linux allows us to be more careful, but really, those are log files we're talking about.
if [[ "$(uname)" == "Linux" ]]; then
rm -rf --one-file-system "${LOGDIR}"
else
rm -rf "${LOGDIR}"
fi
}