Log files are 0640, not 0666.  WTF?
Added logging API

So that multiple threads can log to files with (a) no thread
race conditions, and (b) file locks for external processes.

We do this by opening a file descriptor for each file, and leaving
it open for a long time.  When a thread needs to log to a file,
it passes the filename to the API, and gets an FD in return.
That FD is then left open, for re-use by other threads.

The FD is also protected by a mutex, so that other threads which
try to log to the same file have to wait until the current one
is finished.

This nonsense is required because the POSIX file lock API is
per process, so that locks in one thread don't block other threads.
The POSIX API also releases *all* locks on a file when ANY FD for
that file.  So the threads can't rely on just file locks to prevent
readers from reading the wrong thing.  They have to have ONE FD
for all threads.

Since the FD is never closed, the locks on it always apply.
Since the FD is protected by a mutex, only one thread at a time
can write to the file.

And peace reigns in the land.