Per-thread logging

Based on the `cs` branch. Log each thread in a separate file. See each commit for more info.