fd / tailf

Read and follow subsequent writes to a file. Like tailf.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tailf

An io.ReaderCloser to a file, which never reaches io.EOF and instead blocks for new data to be appended to the file it watches. Effectively, the same as what tail -f {{filename}} does.

This works by putting an inotify watch on the file and blocking for events when we reach the file's max size.

When the io.ReaderCloser is closed, the watch is cancelled and the following reads will return normally until they reach the offset that was last reported as the max file size, where the reader will return io.EOF.

Example

See example/example.go:

tailf

func main() {

    tempFile := makeTempFile()
    defer os.Remove(tempFile.Name())
    defer tempFile.Close()

    done := make(chan struct{})
    go func() {
        writeSlowlyToFile(tempFile)
        close(done)
    }()

    follow, err := tailf.Follow(tempFile.Name())
    if err != nil {
        log.Fatalf("couldn't follow %q: %v", tempFile.Name(), err)
    }

    go func() {
        <-done
        if err := follow.Close(); err != nil {
            log.Fatalf("couldn't close follower: %v", err)
        }
    }()

    _, err = io.Copy(os.Stdout, follow)
    if err != nil {
        log.Fatalf("couldn't read from follower: %v", err)
    }
}

About

Read and follow subsequent writes to a file. Like tailf.


Languages

Language:Go 100.0%