-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstream.go
95 lines (77 loc) · 2.37 KB
/
stream.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
package termlog
import (
"math/rand"
"time"
)
type stream struct {
header string
quiet bool
timestamps bool
id string
log *Log
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func rndstr(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
// Say logs a line
func (s *stream) Say(format string, args ...interface{}) {
s.log.output(s.quiet, &line{"", s.log.format(s.timestamps, say, format, args), s})
}
// Notice logs a line with the Notice color
func (s *stream) Notice(format string, args ...interface{}) {
s.log.output(s.quiet, &line{"", s.log.format(s.timestamps, notice, format, args), s})
}
// Warn logs a line with the Warn color
func (s *stream) Warn(format string, args ...interface{}) {
s.log.output(s.quiet, &line{"", s.log.format(s.timestamps, warn, format, args), s})
}
// Shout logs a line with the Shout color
func (s *stream) Shout(format string, args ...interface{}) {
s.log.output(s.quiet, &line{"", s.log.format(s.timestamps, shout, format, args), s})
}
// SayAs logs a line
func (s *stream) SayAs(name string, format string, args ...interface{}) {
s.log.output(s.quiet, &line{name, s.log.format(s.timestamps, say, format, args), s})
}
// NoticeAs logs a line with the Notice color
func (s *stream) NoticeAs(name string, format string, args ...interface{}) {
s.log.output(s.quiet, &line{name, s.log.format(s.timestamps, notice, format, args), s})
}
// WarnAs logs a line with the Warn color
func (s *stream) WarnAs(name string, format string, args ...interface{}) {
s.log.output(s.quiet, &line{name, s.log.format(s.timestamps, warn, format, args), s})
}
// ShoutAs logs a line with the Shout color
func (s *stream) ShoutAs(name string, format string, args ...interface{}) {
s.log.output(s.quiet, &line{name, s.log.format(s.timestamps, shout, format, args), s})
}
// Quiet disables output for this subgroup
func (s *stream) Quiet() {
s.quiet = true
}
// Header immedately outputs the stream header
func (s *stream) Header() {
outputMutex.Lock()
defer outputMutex.Unlock()
s.log.header(s)
}
func (s *stream) EnableTimestamps() {
s.timestamps = true
}
func (s *stream) getID() string {
if s.id == "" {
s.id = rndstr(16)
}
return s.id
}
func (s *stream) getHeader() string {
return s.header
}
func init() {
rand.Seed(time.Now().UnixNano())
}