I found out how to make a copy of file descriptor 1 (stdout) and 2 (stderr) and how to redirect those copies to a file. Part of it has been sorted out.
You could run the following manually before building.
exec > >(tee --append ~/logfile) ; exec 2>&1
That should redirect everything to ~/logfile and still show usual output in terminal. Does this work for you?
The issue with automating log creation I am thinking about is where to store the log. Ideally, logging would begin as early as possible. Probably not before setting up the error handler trap. Ideally in the help-steps/pre script. Getting the variable which points to the whonix_binary folder doesn't happen before the help-steps/variables script. Also figuring out vmname and version doesn't happen before the help-steps/variables script.
I have some code on my hdd to log to environment variable "log". It could be set using "export log=~/logfile" and used by appending "-E" (which means, preserve environment variables) to "sudo", i.e. "sudo -E ./whonix_build --tor-gateway --build". Would that help?
What in case environment variable "log" isn't set? Should it default to ~/whonix_build_log? "~" could result in /root/whonix_build_log, when the build script has been started as root and to /home/user/whonix_build_log if the build script is run as user using sudo. Not sure how confusing that would be.
Or in case the case the environment variable "log" isn't set, I could default to /var/log/whonix_build_log. Who would look in that folder? Also access rights for the log file are again troubling me. I guess it would be acceptable to set the log to "read/write anyone"? I don't like to see the build script failing, because some step which can be run without root can not continue to write the log file, which has been created as root user.