echo: really ensure log messages go to stdout

all echo helpers are used as logging functions with output to go to the
terminal. when used in functions that are designed to return a string
though the message printed would get incorrectly captured.

the previous fix done in e3a987d977 was
stupidly flawed; somehow my testing led me to mistakenly believe that
was adequate, but retesting proves that it was not.

here we create a new FD #3 linked to stdout to output the messages on,
which testing shows works as I had actually intended it.

e.g. here:
```
Foo () { if [ "$1" = "a" ]; then printf "foo\n"; else printf "error\n"; fi; }
```

we get:
```
~$ Foo a
foo
~$ Foo b
error
~$ XX="$(Foo a)"
~$ echo "${XX}"
foo
~$ XX="$(Foo b)"
~$ echo "${XX}"
error
```

and as demonstrated, "error" got incorrectly captured by in the variable

whereas here:
```
exec 3>&1
Foo () { if [ "$1" = "a" ]; then printf "foo\n"; else printf "error\n" >&3; fi; }
```

it is different in the last case:
```
~$ XX="$(Foo b)"
error
~$ echo "${XX}"

```

the error successfully makes it to the terminal, and the variable is an
empty string (with a newline automatically printed).

Gbp-Dch: Short
This commit is contained in:
Lyndon Brown 2020-03-07 02:33:08 +00:00 committed by Raphaël Hertzog
parent 1edc3af346
commit c54cc2c497
1 changed files with 6 additions and 5 deletions

View File

@ -8,13 +8,14 @@
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.
exec 3>&1
Echo ()
{
STRING="${1}"
shift
printf "${STRING}\n" "${@}" >&1
printf "${STRING}\n" "${@}" >&3
}
Echo_debug ()
@ -23,7 +24,7 @@ Echo_debug ()
STRING="${1}"
shift
printf "D: ${STRING}\n" "${@}" >&1
printf "D: ${STRING}\n" "${@}" >&3
fi
}
@ -52,7 +53,7 @@ Echo_message ()
PREFIX="P"
fi
printf "${PREFIX}: ${STRING}\n" "${@}" >&1
printf "${PREFIX}: ${STRING}\n" "${@}" >&3
fi
}
@ -62,7 +63,7 @@ Echo_verbose ()
STRING="${1}"
shift
printf "I: ${STRING}\n" "${@}" >&1
printf "I: ${STRING}\n" "${@}" >&3
fi
}
@ -83,7 +84,7 @@ Echo_file ()
{
while read -r LINE
do
echo "${1}: ${LINE}" >&1
echo "${1}: ${LINE}" >&3
done < "${1}"
}