Thread: Rotating Log

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058

    Rotating Log

    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.

    Thanx

    Bob

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    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
    Last edited by spydoor; 09-07-2005 at 08:58 AM.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    27

    linled list??

    Hi,
    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.
    Pappy
    You learn something new everyday.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    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

    Bob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  2. Show log entries in a date range
    By JizJizJiz in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2005, 05:33 PM
  3. fprintf works in one function but not in the other.
    By smegly in forum C Programming
    Replies: 11
    Last Post: 05-25-2004, 03:30 PM
  4. log and restore
    By TopJo in forum C Programming
    Replies: 3
    Last Post: 07-01-2003, 07:14 PM
  5. My log file player; Hit the brick wall
    By Twig in forum C Programming
    Replies: 6
    Last Post: 07-27-2002, 05:35 PM