Project

General

Profile

« Previous | Next » 

Revision 12824

lib/sh/util.sh: log+(): use easier-to-understand log_local instead of prefix-assignments to limit assignments to the invoked command

View differences:

trunk/lib/sh/util.sh
444 444
# for unfiltered messages, the log_level is 0 (i.e. still output at verbosity=0)
445 445
# to view a message's log_level, count the # of + signs before it in the output
446 446

  
447
alias log_local='declare PS4="$PS4" log_level="$log_level"'
448

  
447 449
fi # load new aliases
448 450
if self_being_included; then
449 451

  
......
451 453
#        outside func: log++; ...; log--
452 454
#        before cmd:   clog++ cmd  OR  clog+ num cmd  OR  clog++ clog++... cmd
453 455
# with a cmd, assignments are applied just to it, so log_local is not needed
454
# without a cmd, "$@" expands to nothing and assignments are applied to caller
456
# without a cmd, assignments are applied to caller
455 457
# "${@:2}" expands to all of $@ after *1st* arg, not 2nd ($@ indexes start at 1)
456 458
log+()
457 459
{
460
	if test $# -gt 1; then log_local; fi # if cmd, only apply assignments to it
458 461
	# no local vars because w/o cmd, assignments should be applied to caller
459
	PS4="$(str="${PS4:0:1}" n=$((log_level+$1-1)) repeat)${PS4: -2}" \
460
	log_level=$((log_level+$1)) \
462
	PS4="$(str="${PS4:0:1}" n=$((log_level+$1-1)) repeat)${PS4: -2}"
463
	log_level=$((log_level+$1))
461 464
	"${@:2}"
462 465
}
463 466
log-() { log+ "-$1" "${@:2}"; }
464 467
log++() { log+ 1 "$@"; }
465 468
log--() { log- 1 "$@"; }
466 469
log!() { log- "$log_level" "$@"; } # force-displays next log message
467
alias log_local='declare PS4="$PS4" log_level="$log_level"'
468 470
alias log+='log_local; "log+"' # don't expand next word because it's not a cmd
469 471
alias log++='log_local; "log++" ' # last space alias-expands next word
470 472
alias log--='log_local; "log--" ' # last space alias-expands next word

Also available in: Unified diff