forked from wagoodman/bashful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
152 lines (126 loc) · 3.22 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
150
151
152
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
color "github.com/mgutz/ansi"
)
var (
mainLogChan = make(chan LogItem)
mainLogConcatChan = make(chan LogConcat)
)
// LogItem represents all fields in a log message
type LogItem struct {
Name string
Message string
}
// LogConcat contains all metadata necessary to concatenate a subprocess log to the main log
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)
}
// singleLogger creats a separatly managed log (typically for an individual task to be later concatenated with the mainlog)
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 {
logObj, ok := <-SingleLogChan
if ok {
logger.Print(logObj.Message)
} else {
SingleLogChan = nil
}
if SingleLogChan == nil {
break
}
}
}
// mainLogger creates the main log configured by the `log-path` option
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!"))
}