-
Notifications
You must be signed in to change notification settings - Fork 2
/
messages.go
111 lines (98 loc) · 2.21 KB
/
messages.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
package rrh
import "io"
/*
MessageCenter manages log entries.
*/
type MessageCenter struct {
messages []logEntry
}
type logEntry struct {
message string
level LogLevel
}
/*
LogLevel represent the level for logging.
*/
type LogLevel int
/*
VERBOSE shows verbose level of log level.
*/
const (
VERBOSE LogLevel = iota + 1
/*
INFO shows info level of log level.
*/
INFO
/*
WARN shows warning level of log level.
*/
WARN
/*
SEVERE shows severe level of log level.
*/
SEVERE
)
/*
NewMessageCenter creates an instance of MessageCenter.
*/
func NewMessageCenter() *MessageCenter {
var center = new(MessageCenter)
center.messages = []logEntry{}
return center
}
/*
PushLog append given message to receiver MessageCenter as INFO level.
*/
func (mc *MessageCenter) PushLog(message string) {
mc.Push(message, INFO)
}
/*
PushVerbose append given message to receiver MessageCenter as VERBOSE level.
*/
func (mc *MessageCenter) PushVerbose(message string) {
mc.Push(message, VERBOSE)
}
/*
Push append given message to receiver MessageCenter as given level.
*/
func (mc *MessageCenter) Push(message string, level LogLevel) {
mc.messages = append(mc.messages, logEntry{message: message, level: level})
}
/*
PrintVerbose prints messages of VERBOSE, INFO, WARN, and SEVERE log level
in the receiver MessageCenter to the given writer.
*/
func (mc *MessageCenter) PrintVerbose(out io.Writer) {
mc.Print(out, VERBOSE)
}
/*
PrintLog prints messages of INFO, WARN, and SEVERE log level
in the receiver MessageCenter to the given writer.
*/
func (mc *MessageCenter) PrintLog(out io.Writer) {
mc.Print(out, INFO)
}
/*
Print prints greater than given level messages
in the receiver MessageCenter to the given writer.
*/
func (mc *MessageCenter) Print(out io.Writer, level LogLevel) {
var messages = mc.FindMessages(level)
for _, msg := range messages {
out.Write([]byte(msg))
out.Write([]byte("\n"))
}
}
/*
FindMessages returns messages in the receiver MessageCenter with
greater than given log level.
*/
func (mc *MessageCenter) FindMessages(level LogLevel) []string {
var messages = []string{}
for _, msg := range mc.messages {
if msg.level >= level {
messages = append(messages, msg.message)
}
}
return messages
}