Example #1
0
func (lm *LogMonitor) Logs() []*LogFile {
	logfiles := make([]*LogFile, 0)

	for _, glob := range lm.config.Logs {
		logs, err := filepath.Glob(glob)
		if err != nil {
			log.Println("error globbing:", glob, err.Error())
		} else {
			for _, logpath := range logs {
				prev, err := lm.db.getHash(logpath)
				if err != nil {
					log.Println("error getting hash:", logpath, err.Error())
					continue
				}

				curr, err := imohash.SumFile(logpath)
				if err != nil {
					log.Println("error summing file:", logpath, err.Error())
					continue
				}

				if bytes.Compare(prev, curr[:]) != 0 {
					offset, err := lm.db.getOffset(logpath)
					if err != nil {
						log.Println("error getting offset:", logpath, err.Error())
						continue
					}

					log.Println("detected changes:", offset, logpath)

					file, err := os.Open(logpath)
					if err != nil {
						log.Println("error opening log:", logpath, err.Error())
						continue
					}

					if _, err := file.Seek(int64(offset), os.SEEK_SET); err != nil {
						log.Println("cannot seek:", logpath, err.Error())
						continue
					}

					logfiles = append(logfiles, &LogFile{
						file:    file,
						monitor: lm,
						hash:    curr[:],
					})
				} else {
					log.Println("no changes:", logpath)
				}
			}
		}
	}

	return logfiles
}
Example #2
0
func main() {
	flag.Parse()
	files := flag.Args()

	if len(files) == 0 {
		fmt.Println("imosum filenames")
		os.Exit(0)
	}

	for _, file := range files {
		hash, err := imohash.SumFile(file)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%016x  %s\n", hash, file)
	}
}