registrar.go 1.17 KB
Newer Older
1
package main
2 3 4 5 6 7 8 9 10 11 12

import (
  "log"
)

func Registrar(input chan []*FileEvent) {
  for events := range input {
    state := make(map[string]*FileState)
    log.Printf("Registrar received %d events\n", len(events))
    // Take the last event found for each file source
    for _, event := range events {
13
      // skip stdin
14 15 16 17 18 19
      if *event.Source == "-" {
        continue
      }
      // have to dereference the FileInfo here because os.FileInfo is an
      // interface, not a struct, so Go doesn't have smarts to call the Sys()
      // method on a pointer to os.FileInfo. :(
20
      ino, dev := file_ids(event.fileinfo)
21 22
      state[*event.Source] = &FileState{
        Source: event.Source,
23 24
        // take the offset + length of the line + newline char and
        // save it as the new starting offset.
25
        // This issues a problem, if the EOL is a CRLF! Then on start it read the LF again and generates a event with an empty line
26
        Offset: event.Offset + int64(len(*event.Text)) + 1,
27
        Inode:  ino,
28
        Device: dev,
29
      }
30
      //log.Printf("State %s: %d\n", *event.Source, event.Offset)
31 32 33
    }

    if len(state) > 0 {
34
      WriteRegistry(state, ".logstash-forwarder")
35 36 37
    }
  }
}