Write log file one line at a time

This fixes bug #1137