Per-thread logging

Log each thread in a separate file. See each commit for more info.