forked from mattn/goreman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_linux.go
51 lines (45 loc) · 834 Bytes
/
log_linux.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
package main
import (
"github.com/mewkiz/pkg/term"
"log"
"strings"
"sync"
)
type clogger struct {
idx int
proc string
}
var colors = []string{
term.FgGreen,
term.FgCyan,
term.FgMagenta,
term.FgYellow,
term.FgBlue,
term.FgRed,
}
var ci int
var mutex = new(sync.Mutex)
// write handler of logger.
func (l *clogger) Write(p []byte) (n int, err error) {
mutex.Lock()
defer mutex.Unlock()
for _, line := range strings.Split(string(p), "\n") {
if len(line) > 0 && line[len(line)-1] == '\n' {
line = line[0 : len(line)-2]
}
if line != "" {
log.Printf("[%s] %s", term.Color(l.proc, colors[l.idx]), term.Color(line, colors[l.idx]))
}
}
n = len(p)
return
}
// create logger instance.
func createLogger(proc string) *clogger {
l := &clogger{ci, proc}
ci++
if ci >= len(colors) {
ci = 0
}
return l
}