Possible Duplicate:
Implement “tail -f” in C++
I am adding a new thread to a C++ application that will be solely responsible for "tailing" a specific log file. Basically, the thread should monitor the last time a certain message was received according to the log file (or in other words, the last time a certain phrase was written to the file). Each instance of this message will look something like this in the log file:
(timestamp here) Heartbeat message received: ... (more text here, but not important)
If the elapsed time between two timestamps received is greater than or equal to (2 * heartbeat interval) seconds, then the thread should notify the application of this condition and from there the application will handle the rest (this part is not important for the question I'm asking). I have some questions about this:
How can I read a log file in this new thread when another thread is continuously writing to it? Won't I run into thread-safety issues? Is there some way to read the file in a shared mode? I was reading other questions on here related to this but couldn't find a clear answer to this.
Should I implement a timer of some sort in this new thread that will fire off every seconds? I suppose I should only fire off every (2 * heartbeat interval) seconds and then perform the check.
How would I perform the actual tailing of the log file? Is there a way to just start at the end of a file and read in reverse? I know file I/O is expensive. So should I read x amount of bytes (in other words, make a guess about how far in reverse I should read) in reverse and search within that block of data for the timestamp/phrase?