How to zip a log file in C ++?

Possible duplicate:
Implement "tail -f" in C ++

I am adding a new thread to a C ++ application that will be solely responsible for the "tail" of a specific log file. Basically, a thread has to keep track of the last time a particular message was received according to the log file (or in other words, the last time a particular phrase was written to the file). Each instance of this message will look something like this in the log file:

(timestamp here) Heartbeat message got: ... (more text here, but not important)

If the elapsed time between the two received timestamps is greater than or equal to (2 * heartbeat interval) seconds, then the thread has to notify the condition, and from there the application will process the rest (this part is not important to the question I'm asking). I have some questions about this:

  • How can I read the log file on this new thread when another thread is continuously writing it? Will I run into thread safety issues? Is there a way to read the file in shared mode? I have read other questions related to this but could not find a clear answer to this question.

  • Should I implement some kind of timer on this new thread that will fire every seconds? I suppose I should only let go of every (2 * heartbeat interval) seconds and then check.

  • How do I follow the actual tail of the log file? Is there a way to start at the end of the file and read in reverse order? I know file I / O is expensive. So should I read x number of bytes (in other words, think about how far in reverse order I should read) in reverse order and search that chunk of data for a timestamp / phrase?

+3


source to share





All Articles