tags:

views:

81

answers:

2

Just create a mapping of the file of the required size (CreateFileMapping or mmap), write the lines in the buffer and start over when the maximum number is reached. -- Your answer for write-a-circular-file-in-c.

I am also writing the LogWriter module. In this caase i am mapping the whole file to the memory using mmap().

I am maintaining the Read and Write pointers.I want to write the log to the file in append mode. Then when logger service is started first time it writes it appends the logs. But when system gets shutdown next time when i run the service it doesn't append the data at the end. I want to maintain the write and read offsets even if system shut down.How to achieve this ..?

How to find the how much data is written to the log file. ??

+1  A: 

How about reserving the first 16 bytes of the file to store two uint64_t values (in network byte order, of course). Just keep your read & write offsets in there, and they'll always be available when the file is open.

Of course, if you expect to be able to read the file with tools like less, this could be a small problem, but you didn't mention such requirements so I'm assuming there are none.

John Zwinck
A: 

As John mentioned, you could save the pointers in the buffer - but it is very difficult to ensure they are updated in the right order with respect to the data in the buffer.

You might want to consider this alternate, simpler system:

  • Write data to the log file normally in append mode (no mmap) until it reaches a size threshold
  • When the threshold is reached, switch to a new log file
  • When you have more than a certain number of log files, delete the oldest.
bdonlan