@@ -453,11 +453,12 @@ test_run_as_target_user() {
453453 # # root_output would catch xtrace and hence variable root_output would be non-empty.
454454 # # Therefore disabling xtrace.
455455 disable_xtrace
456- xtrace_force_no_re_enable=true root_output= " $( run_as_target_user timeout --kill-after 5 5 test -d /usr 2>&1 ) "
457- re_enable_xtrace_maybe
456+ trap ' xtrace_reenable_maybe ' RETURN
457+ root_output= " $( run_as_target_user timeout --kill-after 5 5 test -d /usr 2>&1 ) "
458458 if test -n " ${root_output} " ; then
459- die 1 " ${underline} run_as_target_user :${nounderline} 'sudo -u ${target_user} ' test produced unexpected output: '${root_output} '"
459+ die 1 " ${underline} test_run_as_target_user :${nounderline} 'sudo -u ${target_user} ' test produced unexpected output: '${root_output} '"
460460 fi
461+ log info " test_run_as_target_user: OK."
461462}
462463
463464
@@ -1008,38 +1009,58 @@ fi
10081009
10091010disable_xtrace () {
10101011 if test " ${xtrace:- } " = " 1" ; then
1012+ xtrace_was_on=true
10111013 set +o xtrace
10121014 else
1013- case " ${-} " in
1014- * x* )
1015- xtrace=1
1016- set +o xtrace
1017- ;;
1018- esac
1015+ xtrace_was_on=false
10191016 fi
10201017}
10211018
1022- re_enable_xtrace_maybe () {
1023- if [ " ${xtrace_force_no_re_enable:- } " = " true" ]; then
1024- return 0
1025- fi
1026- if test " ${xtrace:- } " = " 1" ; then
1019+ xtrace_reenable_maybe () {
1020+ trap ' ' RETURN
1021+ if [ " ${xtrace_was_on:- } " = " true" ]; then
10271022 set -o xtrace
10281023 fi
10291024}
10301025
1026+ __log_level_num () {
1027+ case " ${1:- notice} " in
1028+ bug|error) printf ' %s' 0 ;;
1029+ warn) printf ' %s' 1 ;;
1030+ notice) printf ' %s' 2 ;;
1031+ info) printf ' %s' 3 ;;
1032+ debug) printf ' %s' 4 ;;
1033+ echo) printf ' %s' 5 ;;
1034+ null) printf ' %s' 6 ;;
1035+ * ) printf ' %s' 2 ;; # # unknown -> treat as notice
1036+ esac
1037+ return 0
1038+ }
1039+
10311040# # Logging mechanism with easy customization of message format as well as
10321041# # standardization on how the messages are delivered.
1033- # # usage: log [info|notice|warn|error] "X occurred."
1042+ # # usage: log [info|notice|warn|error|debug|bug|echo|null ] "X occurred."
10341043# # Variable to define by calling script: log_level
1035- log (){
1036- : " ${log_level:= " notice" } "
1044+ log () {
1045+ : " ${log_level:= notice} "
1046+
10371047 # # Avoid clogging output if log() is working alright.
10381048 disable_xtrace
1049+ trap ' xtrace_reenable_maybe' RETURN
1050+
1051+ local log_type log_type_up log_color log_source_script log_level_colorized log_content log_full
1052+ local should_print=0
1053+ local caller_level_num msg_level_num
1054+
10391055 log_type=" ${1:- notice} "
1056+
1057+ if (( $# > 0 )) ; then
1058+ shift || true
1059+ fi
1060+
10401061 # # capitalize log level
1041- log_type_up=" $( printf ' %s ' " $ {log_type} " | tr " [:lower:] " " [:upper:] " ) "
1042- shift 1
1062+ log_type_up=" ${log_type^^} "
1063+
10431064 # # set formatting based on log level
10441065 case " ${log_type} " in
10451066 bug)
@@ -1054,21 +1075,25 @@ log(){
10541075 info)
10551076 log_color=" ${cyan} "
10561077 ;;
1078+ debug)
1079+ log_color=" ${cyan} "
1080+ ;;
10571081 notice)
10581082 log_color=" ${green} "
10591083 ;;
10601084 echo)
10611085 log_color=" "
1062- true
10631086 ;;
10641087 null)
10651088 log_color=" "
1066- true
10671089 ;;
10681090 * )
1069- log bug " Unsupported log type specified: '${log_type} '"
1070- re_enable_xtrace_maybe
1091+ # # Avoid recursion into log() on unsupported type; print directly.
1092+ # log bug "Unsupported log type specified: '${log_type}'"
1093+ stecho " ${0##*/ } : [${red} BUG${nocolor} ]: Unsupported log type specified: '${log_type} '" >&2
10711094 die 1 " Please report this bug."
1095+ return 0
1096+ ;;
10721097 esac
10731098 # # uniform log format
10741099 log_color=" ${bold}${log_color} "
@@ -1082,32 +1107,25 @@ log(){
10821107 case " ${log_type} " in
10831108 bug|error)
10841109 stecho " ${log_full} " >&2
1085- re_enable_xtrace_maybe
10861110 return 0
10871111 ;;
10881112 null)
1089- true
1113+ return 0
10901114 ;;
10911115 esac
1092- # # reverse importance order is required, excluding 'error'
1093- all_log_levels=" warn notice info debug echo null"
1094- # shellcheck disable=SC2154
1095- if printf ' %s' " ${all_log_levels} " | grep -o " .* ${log_level} " \
1096- | grep " ${log_type} " & > /dev/null
1097- then
1098- case " ${log_type} " in
1099- null)
1100- true
1101- ;;
1102- * )
1103- stecho " ${log_full} " >&2
1104- ;;
1105- esac
1116+
1117+ caller_level_num=" $( __log_level_num " ${log_level} " ) "
1118+ msg_level_num=" $( __log_level_num " ${log_type} " ) "
1119+
1120+ if (( msg_level_num <= caller_level_num )) ; then
1121+ should_print=1
11061122 fi
11071123
1108- # sleep 0.1
1124+ if (( should_print == 1 )) ; then
1125+ stecho " ${log_full} " >&2
1126+ fi
11091127
1110- re_enable_xtrace_maybe
1128+ return 0
11111129}
11121130
11131131
@@ -1116,7 +1134,7 @@ log(){
11161134# # to log consecutive errors on multiple lines, making die more suitable
11171135# # usage: die # "msg"
11181136# # where '#' is the exit code.
1119- die (){
1137+ die () {
11201138 log error " ${2} "
11211139 if test " ${allow_errors:- } " = " 1" ; then
11221140 log warn " Skipping termination because of with code '${1} ' due to 'allow_errors' setting."
@@ -1135,13 +1153,11 @@ die(){
11351153
11361154
11371155# # Wrapper to log command before running to avoid duplication of code
1138- log_run (){
1156+ log_run () {
11391157 local level command_without_extraneous_spaces_temp command_without_extraneous_spaces
11401158 level=" ${1} "
1141- shift
1142- # # Extra spaces appearing when breaking log_run on multiple lines.
1143- # # bug: removes all spaces
1144- # command_without_extraneous_spaces="$(printf '%s' "${@}" | tr -s " ")"
1159+ shift || true
1160+
11451161 printf -v command_without_extraneous_spaces_temp ' %q ' " ${@ } "
11461162 command_without_extraneous_spaces=" $( printf " %s\n" " ${command_without_extraneous_spaces_temp} " ) "
11471163 if test " ${dry_run:- } " = " 1" ; then
@@ -1155,7 +1171,7 @@ log_run(){
11551171 log " ${level} " " Background command starting: $ ${command_without_extraneous_spaces} &"
11561172 " ${@ } " &
11571173 background_pid=" $! "
1158- disown " $background_pid "
1174+ disown " ${ background_pid} " || true
11591175 else
11601176 log " ${level} " " Command executing: $ ${command_without_extraneous_spaces} "
11611177 " ${@ } " || return 1
@@ -1166,13 +1182,13 @@ log_run(){
11661182# # Useful to get runtime mid run to log easily
11671183# # Variable to define outside: start_time
11681184# shellcheck disable=SC2154
1169- get_elapsed_time (){
1185+ get_elapsed_time () {
11701186 printf ' %s\n' " $(( $(date +% s) - start_time)) "
11711187}
11721188
11731189
11741190# # Log elapsed time, the name explains itself.
1175- log_time (){
1191+ log_time () {
11761192 log info " Time elapsed: $( get_elapsed_time) s."
11771193}
11781194# #### END pasted by build-dist-installer-cli from file /usr/libexec/helper-scripts/log_run_die.sh
@@ -1557,7 +1573,7 @@ update_sources() {
15571573 update_cmd=( " ${pkg_mngr_update[@]} " )
15581574 fi
15591575
1560- log info " update_cmd: '${update_cmd[*]} '"
1576+ log info " update_cmd: '${update_cmd[*]} ' | console_write_command: ' ${console_write_command[*]} ' "
15611577
15621578 if update_output=$( root_cmd_loglevel=notice root_cmd " ${update_cmd[@]} " 2>&1 | " ${console_write_command[@]} " ) ; then
15631579 log info " Package List Update: exit code '0' (OK)"
@@ -5234,19 +5250,19 @@ log_term_and_file() {
52345250 tty_output=' '
52355251 # # global console_write_command
52365252 if tty_output=" $( tty 2>&1 ) " ; then
5237- console_write_command=( ' tee' ' --' ' /dev/tty ' )
5253+ console_write_command=( ' tee' ' --' " ${tty_output} " )
52385254 log info " Terminal: 'tty' connected: 'yes' (console_write_command: '${console_write_command[*]} ')"
52395255 else
52405256 # # github actions CI issue:
52415257 # # tee: /dev/tty: No such device or address
52425258 # # https://github.com/actions/runner/issues/241
52435259 console_write_command=( ' cat' ' --' )
5244- log info " Terminal: 'tty' connected: 'no' (tty_output: '${tty_output} ' | console_write_command: '${console_write_command[*]} ')"
5260+ log notice " Terminal: 'tty' connected: 'no' (tty_output: '${tty_output} ' | console_write_command: '${console_write_command[*]} ')"
52455261 fi
52465262
52475263 # # Discover if terminal is attached to 'stdout'.
52485264 if [ ! -t 1 ]; then
5249- log info " Terminal: 'stdout' connected: 'no' - Output is not being sent to the terminal because terminal is not connected to 'stdout'."
5265+ log notice " Terminal: 'stdout' connected: 'no' - Output is not being sent to the terminal because terminal is not connected to 'stdout'."
52505266 return 0
52515267 fi
52525268 log info " Terminal: 'stdout' connected: 'yes'"
0 commit comments