You have to prevent memory overruns, both in RAM and mass storage. RAM usage is easy to control, flush your buffer to a file whenever it gets full, then fill the buffer again. On mass storage you'll have to delete old log files, in order to make room for a new file.