Convert logging to fopen/fprintf/fclose. Handle logging directory creation better.