Detect when a file has been recreated using a stat watcher

Marc Lehmann schmorp at
Tue Dec 23 23:19:04 CET 2008

On Tue, Dec 23, 2008 at 08:21:59PM +0200, Graham Leggett <minfrin at> wrote:
>> This all depend son how your logfiles get rotated - the file might get
>> trucnated, deleted or somethign else might happen.
> So I should be looking at the size of the file, rather than the ctime of  
> the file?

I don't know, it depends entirely on how you define "logfile rotation",
which probably relies on whatever program does the actual rotation.

>> a specific filesystem supporting higher resolution timestamps. And even
>> then, the higher resolution might sitll just be centiseconds or so.
>> All this doesn't help you. The documentation (that you somehow fail to
>> know, for inexplicable reasons) explains this case in mroe details and
>> offers ways to implement this properly in your app.
> Maybe I am looking at different documentation to you.

No, you are just being difficult.

We were talking about timestamp resolution, and what to do about it. And this
is cetrainly being explained in the docs.

> The headings I see are "ABI Issues (Largefile Support)", "Inotify and  

It might help to read more than just the headings.

>  "The special problem of stat time resolution".

But there it is.

> The word "rotate" doesn't appear at all in the  
> docs I am reading.

I was replying to you talking about stat time resolution, and even quoted
it, and even *you* quoted it, and then completely ignored it, claiming it has
nothing to do with logfile rotation.

Think before replying.

> Is there some other docs that I should be reading but am not aware of?

No, you might be well-advised, however, to actually read the e-mails you
read and the replies you get. Much less embarrassing.

>>> Is there a cross platform method to determine file rotation, by 
>>> looking  at the attr and prev variables returned by a stat event?
>> Yes, but for some reason you claim it wouldn't work.
> So far I was led to believe that ctime can be used to detect file  
> rotation, but the manpage for stat, and the code that I have in testing  
> contradict this.

It can, indeed, as it changes whenever the logfile gets rotated. Other
things might change, too, such as the filesize, or the inode number, all
of which would be hints that the logfile actually was rotated.

If you are looking for a logfile rotation watcher in libev, then sorry,
there isn't one, you still have to know what you are doing.

> Should I be using the file length instead?

I don't think so - but it all depends on how you define it. Note that the
only sure way of detecting logfile rotation in the general case is to let the
program that rotates the log tell you. Eberything else is just heuristics.

Note that, if you get more than a log message per second, then ev_stat
likely isn't the most efficient way to detect that.

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_    
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      pcg at
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list