Debian Codegraph @

modules/events_stream/events_rotation.go

raw symbols SCIP
1 package events_stream
2
3 import (
4 "fmt"
5 "github.com/evilsocket/islazy/zip"
6 "os"
7 "time"
8 )
9
10 func (mod *EventsStream) doRotation() {
11 if mod.output == os.Stdout {
12 return
13 } else if !mod.rotation.Enabled {
14 return
15 }
16
17 output, isFile := mod.output.(*os.File)
18 if !isFile {
19 return
20 }
21
22 mod.rotation.Lock()
23 defer mod.rotation.Unlock()
24
25 doRotate := false
26 if info, err := output.Stat(); err == nil {
27 if mod.rotation.How == "size" {
28 doRotate = float64(info.Size()) >= float64(mod.rotation.Period*1024*1024)
29 } else if mod.rotation.How == "time" {
30 doRotate = info.ModTime().Unix()%int64(mod.rotation.Period) == 0
31 }
32 }
33
34 if doRotate {
35 var err error
36
37 name := fmt.Sprintf("%s-%s", mod.outputName, time.Now().Format(mod.rotation.Format))
38
39 if err := output.Close(); err != nil {
40 mod.Printf("could not close log for rotation: %s\n", err)
41 return
42 }
43
44 if err := os.Rename(mod.outputName, name); err != nil {
45 mod.Printf("could not rename %s to %s: %s\n", mod.outputName, name, err)
46 } else if mod.rotation.Compress {
47 zipName := fmt.Sprintf("%s.zip", name)
48 if err = zip.Files(zipName, []string{name}); err != nil {
49 mod.Printf("error creating %s: %s", zipName, err)
50 } else if err = os.Remove(name); err != nil {
51 mod.Printf("error deleting %s: %s", name, err)
52 }
53 }
54
55 mod.output, err = os.OpenFile(mod.outputName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
56 if err != nil {
57 mod.Printf("could not open %s: %s", mod.outputName, err)
58 }
59 }
60 }
61