On Wed, Oct 22, 2025 at 06:00:05PM -0700, Bobby Eshleman wrote:
> From: Bobby Eshleman <[email protected]>
> 
> Improve usability of logging functions. Remove the test name prefix from
> logging functions so that logging calls can be made deeper into the call
> stack without passing down the test name or setting some global. Teach
> log function to accept a LOG_PREFIX variable to avoid unnecessary
> argument shifting.
> 
> Remove log_setup() and instead use log_host(). The host/guest prefixes
> are useful to show whether a failure happened on the guest or host side,
> but "setup" doesn't really give additional useful information. Since all
> log_setup() calls happen on the host, lets just use log_host() instead.
> 
> Signed-off-by: Bobby Eshleman <[email protected]>

...

>  log() {
> -     local prefix="$1"
> +     local redirect
> +     local prefix
>  
> -     shift
> -     local redirect=
>       if [[ ${VERBOSE} -eq 0 ]]; then
>               redirect=/dev/null
>       else
>               redirect=/dev/stdout
>       fi
>  
> +     prefix="${LOG_PREFIX:-}"
> +
>       if [[ "$#" -eq 0 ]]; then
> -             __log_stdin | tee -a "${LOG}" > ${redirect}
> +             if [[ -n "${prefix}" ]]; then
> +                     cat | awk -v prefix="${prefix}" '{printf "%s: %s\n", 
> prefix, $0}'

FIWIIW, I would drop cat from this line.

> +             else
> +                     cat
> +             fi
>       else
> -             __log_args "$@" | tee -a "${LOG}" > ${redirect}
> -     fi
> -}
> -
> -log_setup() {
> -     log "setup" "$@"
> +             if [[ -n "${prefix}" ]]; then
> +                     echo "${prefix}: " "$@"
> +             else
> +                     echo "$@"
> +             fi
> +     fi | tee -a "${LOG}" > ${redirect}
>  }
>  
>  log_host() {
> -     local testname=$1
> -
> -     shift
> -     log "test:${testname}:host" "$@"
> +     LOG_PREFIX=host log $@

shellcheck suggests keeping the quoting of $@.
This seems reasonable to me. Although in practice I don't think
it will change the behaviour of this script.

>  }
>  log_host
>  log_guest() {
> -     local testname=$1
> -
> -     shift
> -     log "test:${testname}:guest" "$@"
> +     LOG_PREFIX=guest log $@

shellcheck also points out that log_guest is never passed
arguments, so $@ can be dropped. If you prefer to keep
it then, as per log_host, it seems reasonable for it to be quoted.

>  }

...

Reply via email to