forked from mattn/goreman
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
69 lines (60 loc) · 1.04 KB
/
log.go
File metadata and controls
69 lines (60 loc) · 1.04 KB
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
package main
import (
"bytes"
"fmt"
"sync"
"time"
"github.com/daviddengcn/go-colortext"
)
type clogger struct {
idx int
proc string
}
var colors = []ct.Color{
ct.Green,
ct.Cyan,
ct.Magenta,
ct.Yellow,
ct.Blue,
ct.Red,
}
var ci int
var mutex = new(sync.Mutex)
// write handler of logger.
func (l *clogger) Write(p []byte) (int, error) {
buf := bytes.NewBuffer(p)
wrote := 0
for {
line, err := buf.ReadBytes('\n')
if len(line) > 1 {
now := time.Now().Format("15:04:05")
format := fmt.Sprintf("%%s %%%ds | ", maxProcNameLength)
s := string(line)
mutex.Lock()
ct.ChangeColor(colors[l.idx], false, ct.None, false)
fmt.Printf(format, now, l.proc)
ct.ResetColor()
fmt.Print(s)
mutex.Unlock()
wrote += len(line)
}
if err != nil {
break
}
}
if len(p) > 0 && p[len(p)-1] != '\n' {
fmt.Println()
}
return len(p), nil
}
// create logger instance.
func createLogger(proc string) *clogger {
mutex.Lock()
defer mutex.Unlock()
l := &clogger{ci, proc}
ci++
if ci >= len(colors) {
ci = 0
}
return l
}