-
Notifications
You must be signed in to change notification settings - Fork 6
/
writer.go
90 lines (73 loc) · 1.5 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"bufio"
"fmt"
"os"
"sync"
)
var logs = make(chan string, 100)
const DEPCHECK_DIR = ".depp"
var loggerWg sync.WaitGroup
var fileOps sync.WaitGroup
func fileLog(a ...interface{}) {
str := fmt.Sprintln(a...)
// fmt.Println("Adding to log queue")
loggerWg.Add(1)
logs <- str
}
func createDirectory() {
if _, err := os.Stat(DEPCHECK_DIR); os.IsNotExist(err) {
err := os.Mkdir(DEPCHECK_DIR, 0755)
if err != nil {
panic(err)
}
}
}
func removeDirectory(noLog bool) {
if _, err := os.Stat(DEPCHECK_DIR); !os.IsNotExist(err) {
// path/to/whatever exists
err := os.RemoveAll(DEPCHECK_DIR)
if err != nil {
panic(err)
}
if !noLog {
fmt.Println("Cleaned!")
}
} else {
fmt.Println("Nothing to clean")
}
}
func writeLogsToFile() {
if globalConfig.Log {
fmt.Println("Will be logging output to " + DEPCHECK_DIR + "/debug.log")
// open output file
fo, err := os.Create(DEPCHECK_DIR + "/debug.log")
fileOps.Add(1)
if err != nil {
panic(err)
}
// close fo on exit and check for its returned error
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
datawriter := bufio.NewWriter(fo)
for line := range logs {
// fmt.Println("Writing", line, "to file")
_, err := datawriter.WriteString(line + "\n")
if err != nil {
panic(err)
}
loggerWg.Done()
}
datawriter.Flush()
fo.Close()
fileOps.Done()
} else {
for line := range logs {
fmt.Sprintln(line) // Just ignore this line
loggerWg.Done()
}
}
}