teach stats_log how to do dynamic memory allocation

This fixes the logging implementation to not use a statically sized buffer, and instead use `malloc(3)` to create a sufficiently large buffer. This will incur extra memory allocations, but it should be offset by the fact that there's less stack memory used.