Add logging functionality

This slows the lock down considerably. It would be better if the
streamwriting was done in a different thread.