开发者

Logging Mechanism using memory mapping technique

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. ??


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.


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜