forked from wagoodman/bashful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
149 lines (124 loc) · 2.94 KB
/
log.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
color "github.com/mgutz/ansi"
)
var (
mainLogChan chan LogItem = make(chan LogItem)
mainLogConcatChan chan LogConcat = make(chan LogConcat)
)
type LogItem struct {
Name string
Message string
}
type LogConcat struct {
File string
}
func logToMain(msg, format string) {
if config.Options.LogPath != "" {
if format != "" {
mainLogChan <- LogItem{Name: "[Main]", Message: color.Color(msg, format)}
} else {
mainLogChan <- LogItem{Name: "[Main]", Message: msg}
}
}
}
func removeDirContents(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}
func setupLogging() {
err := os.MkdirAll(config.cachePath, 0755)
if err != nil {
exitWithErrorMessage("\nUnable to create cache dir\n" + err.Error())
}
err = os.MkdirAll(config.logCachePath, 0755)
if err != nil {
exitWithErrorMessage("\nUnable to create log dir\n" + err.Error())
}
removeDirContents(config.logCachePath)
go MainLogger(config.Options.LogPath)
}
func SingleLogger(SingleLogChan chan LogItem, name, logPath string) {
file, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
exitWithErrorMessage("\nUnable to create log\n" + err.Error())
}
defer file.Close()
defer func() {
mainLogConcatChan <- LogConcat{logPath}
}()
logger := log.New(file, "", log.Ldate|log.Ltime)
logger.Println(bold("Task full output: " + name))
logger.SetFlags(0)
for {
select {
case logObj, ok := <-SingleLogChan:
if ok {
logger.Print(logObj.Message)
} else {
SingleLogChan = nil
}
}
if SingleLogChan == nil {
break
}
}
}
func MainLogger(logPath string) {
file, err := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
exitWithErrorMessage("\nUnable to create main log\n" + err.Error())
}
defer file.Close()
logger := log.New(file, "", log.Ldate|log.Ltime)
for {
select {
case logObj, ok := <-mainLogChan:
if ok {
logger.Print(logObj.Message)
} else {
mainLogChan = nil
}
case logCmd, ok := <-mainLogConcatChan:
if ok {
file.Close()
out, err := exec.Command("bash", "-c", "cat "+logCmd.File+" >> "+logPath).CombinedOutput()
if err != nil {
fmt.Printf("%s\n", out)
exitWithErrorMessage("\nUnable to concat logs\n" + err.Error())
}
file, err = os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
exitWithErrorMessage("\nUnable to create main log\n" + err.Error())
}
logger = log.New(file, "", log.Ldate|log.Ltime)
os.Remove(logCmd.File)
} else {
mainLogConcatChan = nil
}
}
if mainLogChan == nil && mainLogConcatChan == nil {
break
}
}
logger.Println(bold("Finished!"))
}