Пример #1
0
// Close release file handle
func (l *ChatLog) Close() {
	l.WriteNicks()
	l.Lock()
	l.f.Close()
	if _, err := common.CompressFile(l.f.Name()); !os.IsNotExist(err) && err != nil {
		log.Printf("error compressing log %s %s", l.f.Name(), err)
	}
	l.Unlock()
}
Пример #2
0
func compress() error {
	if len(os.Args) < 3 {
		return errors.New("not enough args")
	}
	path := os.Args[2]
	if _, err := common.CompressFile(path); err != nil {
		return err
	}
	return nil
}
Пример #3
0
func migrate() error {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	runtime.GOMAXPROCS(runtime.NumCPU())
	if len(os.Args) < 4 {
		return errors.New("not enough args")
	}
	src := os.Args[2]
	dst := os.Args[3]

	f, err := os.Open(src)
	if err != nil {
		return err
	}
	channels, err := f.Readdirnames(0)
	if err != nil {
		return err
	}
	for _, c := range channels {
		f, err := os.Open(src + "/" + c)
		if err != nil {
			log.Println(err)
			continue
		}
		months, err := f.Readdirnames(0)
		if err != nil {
			log.Println(err)
			continue
		}
		for _, m := range months {
			f, err := os.Open(src + "/" + c + "/" + m)
			if err != nil {
				log.Println(err)
				continue
			}
			logs, err := f.Readdir(0)
			if err != nil {
				log.Println(err)
				continue
			}
			banInjector, err := newInjector("Ban", src+"/"+c+"/"+m+"/bans.txt")
			if err != nil && !os.IsNotExist(err) {
				log.Printf("error reading bans %s", err)
			}
			subInjector, err := newInjector("Subscriber", src+"/"+c+"/"+m+"/subs.txt")
			if err != nil && !os.IsNotExist(err) {
				log.Printf("error reading subs %s", err)
			}
			names := []string{}
			for _, l := range logs {
				if fileNameDate.MatchString(l.Name()) {
					names = append(names, l.Name())
				}
			}
			sort.Sort(logsByDay(names))
			for _, l := range names {
				d := fileNameDate.FindString(l)
				if d, err = normalizeDate(d); err != nil {
					continue
				}
				srcFile := src + "/" + c + "/" + m + "/" + l
				dstFile := dst + "/" + c + "/" + m + "/" + d + ".txt"
				data, err := ioutil.ReadFile(srcFile)
				if err != nil {
					continue
				}
				if _, err := os.Stat(dst + "/" + c + "/" + m); err != nil {
					err := os.MkdirAll(dst+"/"+c+"/"+m, 0755)
					if err != nil {
						log.Printf("error creating target dir %s", err)
						continue
					}
				}
				f, err := os.OpenFile(dstFile, os.O_CREATE|os.O_TRUNC|os.O_APPEND|os.O_WRONLY, 0644)
				if err != nil {
					log.Printf("error creating target file %s", err)
					continue
				}
				for {
					parts, err := readLine(&data, logLine)
					if err != nil {
						if err != io.EOF {
							log.Printf("error reading log line %s %s", srcFile, err)
						}
						break
					}
					t, err := parseTime(d, parts[0])
					if err != nil {
						log.Printf("error parsing time %s \"%s\" %s", srcFile, parts[0], err)
					}
					if banInjector != nil && banInjector.currentTime != nil && t.After(*banInjector.currentTime) {
						log.Println("added", srcFile, banInjector.currentLine)
						if _, err := f.WriteString(banInjector.currentLine); err != nil {
							log.Printf("error writing log line %s", err)
							break
						}
						if err := banInjector.advance(); err != nil {
							if err != io.EOF {
								log.Printf("error advancing ban injector %s", err)
							}
							banInjector = nil
						}
					}
					if subInjector != nil && subInjector.currentTime != nil && t.After(*subInjector.currentTime) {
						log.Println("added", srcFile, subInjector.currentLine)
						if _, err := f.WriteString(subInjector.currentLine); err != nil {
							log.Printf("error writing log line %s", err)
							break
						}
						if err := subInjector.advance(); err != nil {
							if err != io.EOF {
								log.Printf("error advancing sub injector %s", err)
							}
							subInjector = nil
						}
					}
					if parts[1] == "##################################" {
						parts[1] = "twitchnotify"
					}
					if _, err := f.WriteString(formatLine(t, parts[1], parts[2])); err != nil {
						log.Printf("error writing log line %s", err)
						break
					}
				}
				f.Close()
				go func() {
					time.Sleep(1 * time.Second)
					if err := exec.Command(os.Args[0], "nicks", dstFile).Run(); err != nil {
						log.Printf("error generating nick list for %s %s", dstFile, err)
						return
					}
					time.Sleep(1 * time.Second)
					if _, err := common.CompressFile(dstFile); err != nil {
						log.Printf("error compressing file %s, %s", dstFile, err)
						return
					}
				}()
				log.Printf("finished with %s", srcFile)
			}
		}
	}
	return nil
}