pkg/util/log: make use of OrigStderr thread safe
Fixes: https://github.com/cockroachdb/cockroach/issues/122791
The log package exports a variable called OrigStderr which is meant to represent the original stderr os.File for the process at startup.
Various operations in the log package may "hijack" the stderr, for example, to redirect the process's stderr logs to a file instead. The package maintains a reference to the original stderr in order to maintain the ability to still make use of it under certain circumstances while still redirecting stderr to a file.
A race condition was appearing in tests that was reassigning this variable while it was in use by the process.
This patch fixes the race condition by wrapping the os.File in a mutex, and then implementing the io.Writer interface on the wrapper type. Swapping out the underlying file can now be an atomic operation, and we also expose an API to perform atomic operations on the underlying file. A RWMutex is used to reduce contention.
Release note: none
Epic: none
bors r=xinhaoz
Build succeeded:
blathers backport 24.1.0-rc