Can anybody suggest the best approach for creating a rotating log file? In other words, I want a maximum of 10 80 character records in my ASCII text rotating log file. When I add the 11th record, I must delete the first record.
I know this can easily be done with CFile but the MFC stuff clashes with my stock C program.
create a temp file.
write the new log entry to the temp file
write (append) the first 9 lines of the log file to the temp file. (or up to EOF if there aren't at least 9 lines yet)
replace the log file with the tmp file (rename)
this way your oldest log entry will always "fall off" the bottom
Why not a circular linked list of 10 nodes (entry 9 next is a pointer to entry 0) and a pointer (lastentry) to the last entry?
Then input the next entry to lastentry->next->whatever and change lastentry pointer to lastentry->next.
Well since we're all presenting random ideas, here's a variation of the circular list above.
Use a 'binary' file. Make the first byte store the 'top' position. This is where you write your new one. Increment 'top'. You'll either need to % 10 'top' when you increment it, or when you seek.
I think my best option would be to use the tmp file. I'm planning to use this rotating log as a debugging log in one of my apps. A primary requirement is that the log should be easily read using any ASCII text editor. Also, the size of the log (number of stored records) will be changed often via a registry entry.
A don't thing a circular linked list (storing the structures) in a file or a binary type file would be easily readable. Also, I like the idea that my most current log record is at the top of the file.
I want to thank all of you for your input :D