teach stats_log how to do dynamic memory allocation

This redoes the logging implementation so that it only does memory allocation on the first log message, and on subsequent log messages that are > 256 bytes.

The logger can still log messages of arbitrary size.