tazwok annotate libtazwok/libtazwok-modules/report @ rev 559

tazwok: typos
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Nov 09 11:24:59 2021 +0000 (2021-11-09)
parents d2993eed3ea5
children
rev   line source
pankso@555 1
pankso@555 2 # Usage : use_report functions/commands
pankso@555 3 # 'use_report' execute functions/commands and redirect the output.
pankso@555 4 # You can use 'report' function to display messages, steps, blocs and
pankso@555 5 # status in the terminal and organize the display of the log.
pankso@555 6
pankso@555 7 report_verbosity_opt=all
pankso@555 8
pankso@555 9 report_display()
pankso@555 10 {
pankso@555 11 while read out; do
pankso@555 12 [ "$out" = €øß ] && report_stop_display && return
pankso@555 13 echo -e " ...\n ~~~~~~~~~~~~~~~~~~~~ Messages ~~~~~~~~~~~~~~~~~~~~ "
pankso@555 14 echo "$out"
pankso@555 15 break
pankso@555 16 done
pankso@555 17 while read out; do
pankso@555 18 [ "$out" = €øß ] && report_stop_display && return
pankso@555 19 echo "$out"
pankso@555 20 done
pankso@555 21 }
pankso@555 22
pankso@555 23 report_create_html()
pankso@555 24 {
pankso@555 25 mkdir -p "${log_opt%/*}"
pankso@555 26 echo '<div id="report">' >> "$log_opt"
pankso@555 27
pankso@555 28 echo -e "<strong>$(basename "$log_command")</strong>\n<em>$(date)</em>" >> "$log_opt"
pankso@555 29 }
pankso@555 30
pankso@555 31 report_start()
pankso@555 32 {
pankso@555 33 # Create the temporary directory if needed.
pankso@555 34 ! [ -d /tmp/libtaz/ ] && mkdir -p /tmp/libtaz
pankso@555 35 ! [ -d $SLITAZ_LOG ] && mkdir -p $SLITAZ_LOG
pankso@555 36
pankso@555 37 # Give permissions to all users if user is root.
pankso@555 38 if test $(id -u) = 0 ; then
pankso@555 39 chmod 777 /tmp/libtaz
pankso@555 40 chmod -R 777 $SLITAZ_LOG
pankso@555 41 fi
pankso@555 42
pankso@555 43 # Get a random logfile name.
pankso@555 44 if [ "$report_pid" ]; then
pankso@555 45 log_tmp=/tmp/libtaz/$report_pid
pankso@555 46 embeded_mode=enabled
pankso@555 47 initial_bloc_level=$bloc_level
pankso@555 48 if [ "$open_bloc" ]; then
pankso@555 49 initial_bloc_level=$(($bloc_level+1))
pankso@555 50 embeded_open_bloc=yes
pankso@555 51 fi
pankso@555 52 if [ -p $log_tmp.stdout ] && ! [ "$open_bloc" = yes ]; then
pankso@555 53 report_step_status
pankso@555 54 fi
pankso@555 55 else
pankso@555 56 log_tmp=/tmp/libtaz/$$
pankso@555 57 report_pid=$$
pankso@555 58 fi
pankso@555 59
pankso@555 60 # Use the default logfile if no logfile was specified by the main script.
pankso@555 61 [ ! "$log_opt" ] && [ "$report_log_all" = yes ] && \
pankso@555 62 log_opt="$SLITAZ_LOG/`basename $0`/$(date -Iseconds).html"
pankso@555 63 export report_pid log_opt
pankso@555 64
pankso@555 65 #if [ "$embeded_mode" != enabled ]; then
pankso@555 66
pankso@555 67 # Initialize html logfile if needed, or prepare it to be edited.
pankso@555 68 [ "$log_opt" ] && { [ ! "$embeded_mode" ] || [ ! -f "$log_opt" ]; } && report_create_html
pankso@555 69
pankso@555 70 echo -n "" > $log_tmp
pankso@555 71
pankso@555 72 # Initialize named pipes & set I/O redirections.
pankso@555 73 [ "$embeded_mode" ] || exec 3>&1 4>&2
pankso@555 74
pankso@555 75 # Start debugging if the option is enabled.
pankso@555 76 if [ "$debug_opt" ]; then
pankso@555 77 exec 2>$log_tmp.stdout
pankso@555 78 set -vx
pankso@555 79 echo "[--------------------------/!\ DEBUG MODE ENABLED /!\--------------------------]"
pankso@555 80 fi
pankso@555 81
pankso@555 82 # Theses variable are used to configure the display of steps in a terminal.
pankso@555 83 bloc1_delimiter="================================================================================"
pankso@555 84 bloc1_display=" "
pankso@555 85 bloc2_delimiter="----------------------------------------------------------------------------"
pankso@555 86 bloc2_display=" > "
pankso@555 87 bloc3_delimiter="* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
pankso@555 88 bloc3_display=" * "
pankso@555 89
pankso@555 90 # Use a trap to exit cleanly if app is killed.
pankso@555 91 trap "report_exit $(basename $0) killed by user" SIGINT SIGTERM
pankso@555 92
pankso@555 93 # Use another display function if quiet is enabled.
pankso@555 94 [ "$report_verbosity_opt" = "none" ] && report_display()
pankso@555 95 {
pankso@555 96 while read out; do
pankso@555 97 [ "$out" = €øß ] && report_stop_display && return
pankso@555 98 done
pankso@555 99 }
pankso@555 100 }
pankso@555 101
pankso@555 102 report_stop_display()
pankso@555 103 {
pankso@555 104 rm $log_tmp.stderr $log_tmp.stdout
pankso@555 105 sed '/^€øß$/d' -i $log_tmp
pankso@555 106 touch $log_tmp.end
pankso@555 107 }
pankso@555 108
pankso@555 109 report_listen()
pankso@555 110 {
pankso@555 111 mkfifo $log_tmp.stderr $log_tmp.stdout
pankso@555 112 {
pankso@555 113 { tee -a $log_tmp $log_tmp.error <$log_tmp.stderr | while read line; do echo -e "\\033[1;31m${line}\\033[0m"; done; }&
pankso@555 114 { tee -a $log_tmp <$log_tmp.stdout; }&
pankso@555 115 } | report_display&
pankso@555 116 usleep 10000
pankso@555 117 exec 1>>$log_tmp.stdout 2>>$log_tmp.stderr
pankso@555 118 }
pankso@555 119
pankso@555 120 report_stop_listen()
pankso@555 121 {
pankso@555 122 if [ -p $log_tmp.stdout ]; then
pankso@555 123 [ -f $log_tmp.end ] && rm $log_tmp.end
pankso@555 124 usleep 10000 && echo €øß
pankso@555 125 while ! [ -f $log_tmp.end ]; do
pankso@555 126 usleep 10000
pankso@555 127 done
pankso@555 128 exec 1>&3 2>&4
pankso@555 129 rm $log_tmp.end
pankso@555 130 fi
pankso@555 131 }
pankso@555 132
pankso@555 133 report_stop()
pankso@555 134 {
pankso@555 135 ! [ "$report_pid" ] && exit
pankso@555 136 # End step and close blocs.
pankso@555 137 [ "$step_running" ] && report end-step
pankso@555 138 while [ $(($bloc_level)) -gt $(($initial_bloc_level)) ]; do
pankso@555 139 report close-bloc
pankso@555 140 done
pankso@555 141
pankso@555 142 if [ "$embeded_mode" = enabled ]; then
pankso@555 143 [ "$bloc_level" ] && echo $(eval echo \$bloc${bloc_level}_status) > $log_tmp.status
pankso@555 144 if [ "$embeded_open_bloc" = opened ]; then
pankso@555 145 touch $log_tmp.eob
pankso@555 146 fi
pankso@555 147 else
pankso@555 148 [ "$log_opt" ] && echo '</div>' >> "$log_opt"
pankso@555 149 exec 3>&- 4>&-
pankso@555 150 rm -f $log_tmp*
pankso@555 151 unset report_pid log_opt
pankso@555 152
pankso@555 153 # Stop debugging
pankso@555 154 [ "$debug_opt" ] && set +vx
pankso@555 155 fi
pankso@555 156 }
pankso@555 157
pankso@555 158 # Use this to stop a program with an error.
pankso@555 159 report_exit()
pankso@555 160 {
pankso@555 161 # Log&display error message.
pankso@555 162 echo -e "$@" >&2
pankso@555 163
pankso@555 164 # Close step as failed.
pankso@555 165 if [ "$step_running" ]; then
pankso@555 166 (exit 1)
pankso@555 167 report end-step
pankso@555 168 fi
pankso@555 169
pankso@555 170 # Set blocs as failed, report and exit.
pankso@555 171 bloc1_status="failed"
pankso@555 172 bloc2_status="failed"
pankso@555 173 bloc3_status="failed"
pankso@555 174 report_stop
pankso@555 175 exit 1
pankso@555 176 }
pankso@555 177
pankso@555 178 # Usage : report [command] [message]
pankso@555 179 #
pankso@555 180 # type :message simply display the message.
pankso@555 181 # step name the step and display status when ended.
pankso@555 182 # end-step display status of the step, it's done automatically when
pankso@555 183 # starting a new step, closing a bloc or stopping a report.
pankso@555 184 # open-bloc open the list of steps in the operation.
pankso@555 185 # close-bloc close the current bloc.
pankso@555 186 report()
pankso@555 187 {
pankso@555 188 # First : get status before it is changed by other commands.
pankso@555 189 check_status=$?
pankso@555 190 case "$1" in
pankso@555 191 start)
pankso@555 192 report_start
pankso@555 193 ;;
pankso@555 194 stop)
pankso@555 195 report_stop
pankso@555 196 ;;
pankso@555 197 exit)
pankso@555 198 shift && report_exit "$@"
pankso@555 199 ;;
pankso@555 200 # TODO : Code displaying a message during a step run.
pankso@555 201 message)
pankso@555 202 shift
pankso@555 203 echo -e "${current_bloc_display}$@"
pankso@555 204 report_message="$@"
pankso@555 205 [ "$log_opt" ] && echo -e "<div class=\"message\">$report_message</div>" >> "$log_opt"
pankso@555 206 ;;
pankso@555 207 step)
pankso@555 208 [ "$log_step" ] && echo "$2" > $log_step
pankso@555 209 [ "$open_bloc" ] && report_open_bloc
pankso@555 210 [ "$step_running" ] && report_step_status
pankso@555 211 echo -ne "$current_bloc_display$2\\033[70G[ \\033[1;32mR\\033[33mU\\033[31mN\\033[0;39m ]" >&3
pankso@555 212 [ "$log_opt" ] && echo -e "<div class=\"$2\">\n<strong>$2</strong>" >> "$log_opt"
pankso@555 213 export step_running="$2"
pankso@555 214 report_listen
pankso@555 215 ;;
pankso@555 216 end-step)
pankso@555 217 if ! [ "$step_running" ]; then
pankso@555 218 report_return_code=$check_status
pankso@555 219 else
pankso@555 220 report_step_status
pankso@555 221 fi
pankso@555 222 ;;
pankso@555 223 open-bloc)
pankso@555 224 export open_bloc=yes
pankso@555 225 ;;
pankso@555 226 close-bloc)
pankso@555 227 if [ -f "$log_tmp.eob" ]; then
pankso@555 228 report_open_bloc
pankso@555 229 check_status=$(cat $log_tmp.status)
pankso@555 230 rm $log_tmp.status
pankso@555 231 report_set_bloc_status
pankso@555 232 elif [ "$step_running" ]; then
pankso@555 233 report_step_status
pankso@555 234 fi
pankso@555 235
pankso@555 236 # Then close the bloc and report status if it was open.
pankso@555 237 if ! [ "$open_bloc" ]; then
pankso@555 238 export current_bloc_display="$(eval echo \"\$bloc$((${bloc_level}-1))_display\")"
pankso@555 239 [ "$(eval echo \"\$bloc${bloc_level}_delimiter\")" ] && \
pankso@555 240 echo -e "$current_bloc_display$(eval echo \"\$bloc${bloc_level}_delimiter\")" >&3
pankso@555 241 echo -en "$current_bloc_display... $(eval echo \"\$bloc${bloc_level}_running\")" >&3
pankso@555 242 check_status=$(eval echo \$bloc${bloc_level}_status)
pankso@555 243 report_display_status
pankso@555 244
pankso@555 245 # Set the bloc status in the log file.
pankso@555 246 if [ "$log_opt" ]; then
pankso@555 247 sed "s~<div class=\"bloc_level$bloc_level\">~<div class=\"$check_status\">~" -i "$log_opt"
pankso@555 248 echo "</div>" >> "$log_opt"
pankso@555 249 fi
pankso@555 250 echo "" >&3
pankso@555 251 export bloc_level=$(($bloc_level-1))
pankso@555 252 [ "$bloc_level" = 0 ] && unset bloc_level
pankso@555 253 else
pankso@555 254 unset open_bloc
pankso@555 255 fi
pankso@555 256 ;;
pankso@555 257 sublog)
pankso@555 258 shift
pankso@555 259 ! [ "$log_list" ] && log_list="$log_opt"
pankso@555 260 export log_list="$1 $log_list"
pankso@555 261 export log_opt="$1"
pankso@555 262 report_create_html
pankso@555 263 ;;
pankso@555 264 end-sublog)
pascal@559 265 [ "$log_opt" ] || return
pankso@555 266 export log_list="${log_list#* }"
pankso@555 267 echo '</div>' >> "$log_opt"
pankso@555 268 # Grep the summary of sublog (main action + status) and put
pankso@555 269 # it in the main log, plus a link.
pankso@555 270 [ "$log_list" ] && { sed -n '/<div id="report">/,$p' $log_opt | \
pankso@555 271 grep -A1 -F '<div class=' | sed 1,2!d &&
pankso@555 272 echo '<a class="sublog" href="'$log_opt'">SubLog</a>' && \
pankso@555 273 echo "</div>"
pankso@555 274 } >> ${log_list%% *}
pankso@555 275 export log_opt="${log_list%% *}"
pankso@555 276 report_step_status
pankso@555 277 ;;
pankso@555 278 esac
pankso@555 279 return $report_return_code
pankso@555 280 }
pankso@555 281
pankso@555 282 # Open a bloc of substeps in a terminal and log.
pankso@555 283 report_open_bloc()
pankso@555 284 {
pankso@555 285 unset open_bloc
pankso@555 286 [ "$embeded_open_bloc" = "yes" ] && embeded_open_bloc=opened
pankso@555 287 [ -f "$log_tmp.eob" ] && rm $log_tmp.eob
pankso@555 288 export bloc_level=$(($bloc_level+1))
pankso@555 289 export bloc${bloc_level}_running="$step_running"
pankso@555 290 if [ -p $log_tmp.stdout ]; then
pankso@555 291 if [ -s $log_tmp ]; then
pankso@555 292 prebloc=yes
pankso@555 293 report_step_status
pankso@555 294 prebloc=
pankso@555 295 else
pankso@555 296 report_stop_listen
pankso@555 297 echo ' ...' >&3
pankso@555 298 fi
pankso@555 299 [ "$(eval echo \$bloc${bloc_level}_delimiter)" ] && \
pankso@555 300 echo "$current_bloc_display$(eval echo \"\$bloc${bloc_level}_delimiter\")" >&3
pankso@555 301 [ "$log_opt" ] && sed "s~<div class=\"$step_running\">~<div class=\"bloc_level$bloc_level\">~" -i "$log_opt"
pankso@555 302 else
pankso@555 303 exec 1>&3 2>&4
pankso@555 304 fi
pankso@555 305 export current_bloc_display="$(eval echo \"\$bloc${bloc_level}_display\")"
pankso@555 306 unset bloc${bloc_level}_status
pankso@555 307 unset step_running
pankso@555 308 }
pankso@555 309
pankso@555 310 # These are the two new status functions used by the report tool.
pankso@555 311 # States are : ok - no error since the start of the step
pankso@555 312 # warning - there was an error(s) since the start of the step
pankso@555 313 # failed - the last executed command has exited abnormally
pankso@555 314 report_step_status()
pankso@555 315 {
pankso@555 316 unset report_return_code
pankso@555 317 if [ -p $log_tmp.stdout ]; then
pankso@555 318 report_stop_listen
pankso@555 319
pankso@555 320 # Close message bloc if opened.
pankso@555 321 if [ -s $log_tmp -a "$report_verbosity_opt" = all ] || \
pankso@555 322 [ -s $log_tmp.error -a "$report_verbosity_opt" != none ]; then
pankso@555 323 echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ " >&3
pankso@555 324 ! [ "$prebloc" ] && echo -en "$current_bloc_display... $step_running" >&3 && report_addemptyline="yes"
pankso@555 325 fi
pankso@555 326
pankso@555 327 # Check, display and log the status of the step.
pankso@555 328 if ! [ "$check_status" = "0" ]; then
pankso@555 329 check_status=failed
pankso@555 330 else
pankso@555 331 [ -s "$log_tmp.error" ] && check_status=warning || check_status=ok
pankso@555 332 fi
pankso@555 333 ! [ "$prebloc" ] && report_display_status
pankso@555 334 [ "$report_addemptyline" ] && echo "" >&3 && unset report_addemptyline
pankso@555 335 [ "$log_opt" ] && report_rec_log
pankso@555 336 fi
pankso@555 337
pankso@555 338 [ -f "$log_tmp.eob" ] && rm $log_tmp.eob
pankso@555 339
pankso@555 340 # Set blocs status if needed.
pankso@555 341 [ -f $log_tmp.status ] && check_status=$(cat $log_tmp.status) && rm $log_tmp.status
pankso@555 342 [ "$bloc_level" ] && report_set_bloc_status
pankso@555 343
pankso@555 344 # Clear variables / temporary logs.
pankso@555 345 ! [ "$prebloc" ] && export step_running=""
pankso@555 346 check_status=""
pankso@555 347 echo -n "" > $log_tmp.error
pankso@555 348 echo -n "" > $log_tmp
pankso@555 349 }
pankso@555 350
pankso@555 351 report_display_status()
pankso@555 352 {
pankso@555 353 [ "$report_return_code" = 1 ] && check_status=failed
pankso@555 354 echo -en "\\033[70G " >&3
pankso@555 355 echo -en "\\033[70G[ " >&3
pankso@555 356 [ "$check_status" = ok ] && report_return_code=0 && echo -en "\\033[1;32mOK" >&3
pankso@555 357 [ "$check_status" = warning ] && report_return_code=0 && echo -en "\\033[1;33mWarning" >&3
pankso@555 358 [ "$check_status" = failed ] && report_return_code=1 && echo -en "\\033[1;31mFailed" >&3
pankso@555 359 echo -e "\\033[0;39m ]" >&3
pankso@555 360 }
pankso@555 361
pankso@555 362 report_term_to_html()
pankso@555 363 {
pankso@555 364 report_decolorize | sed -e 's~<~\&lt;~' -e 's~>~\&gt;~' -e 's~STDERR: \(.*\)~<span class="error">\1<\/span>~' -e 's~ ~\&nbsp;\&nbsp;\&nbsp;\&nbsp;~'
pankso@555 365 }
pankso@555 366
pankso@555 367 report_decolorize()
pankso@555 368 {
pankso@555 369 tr -d '\e' | sed -r "s/\[([0-9]{1,3}(;[0-9]{1,3})*)[m|G]//g"
pankso@555 370 }
pankso@555 371
pankso@555 372 report_rec_log()
pankso@555 373 {
pankso@555 374 ! [ "$prebloc" ] && sed "s~<div class=\"$step_running\">~<div class=\"$check_status\">~" -i "$log_opt"
pankso@555 375 if [ -s "$log_tmp" ]; then
pankso@555 376 if [ -s "$log_tmp.error" ]; then
pankso@555 377 cat $log_tmp.error | sort -u | while read line; do
pankso@555 378 # Format line to avoid sed errors :
pankso@555 379 line=$(echo "$line" | sed -e 's~\*~\\\*~g' -e 's~\[~\\\[~g' -e 's~\]~\\\]~g')
pankso@555 380 sed "s~^$line$~STDERR: $line~" -i $log_tmp
pankso@555 381 done
pankso@555 382 fi
pankso@555 383 echo -n "<pre>" >> "$log_opt"
pankso@555 384 cat $log_tmp | report_term_to_html >> "$log_opt"
pankso@555 385 echo "</pre>" >> "$log_opt"
pankso@555 386 fi
pankso@555 387 ! [ "$prebloc" ] && echo "</div>" >> "$log_opt"
pankso@555 388 }
pankso@555 389
pankso@555 390 # These variables keep the status of the blocs.
pankso@555 391 # A bloc is reported as ok if all child steps are ok and has failed
pankso@555 392 # if all child steps are failed (or if a fatal error occurs).
pankso@555 393 # In other cases the bloc is reported as a warning.
pankso@555 394 report_set_bloc_status()
pankso@555 395 {
pankso@555 396 bloc_status=$(eval echo \$bloc${bloc_level}_status)
pankso@555 397 for n in $(seq $bloc_level -1 1); do
pankso@555 398 ! [ "$(eval echo \$bloc${n}_status)" ] && eval bloc${n}_status=$check_status
pankso@555 399 ! [ "$(eval echo \$bloc${n}_status)" = "$check_status" ] && \
pankso@555 400 eval bloc${n}_status=warning
pankso@555 401 done
pankso@555 402 }
pankso@555 403