-
Notifications
You must be signed in to change notification settings - Fork 1
/
line.go
78 lines (63 loc) · 1.47 KB
/
line.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
package message
import (
"fmt"
"strings"
"github.com/cdr/grip/level"
)
type lineMessenger struct {
lines []interface{}
Base `bson:"metadata" json:"metadata" yaml:"metadata"`
Message string `bson:"message" json:"message" yaml:"message"`
}
// NewLineMessage is a basic constructor for a type that, given a
// bunch of arguments, calls fmt.Sprintln() on the arguments passed to
// the constructor during the String() operation. Use in combination
// with Compose[*] logging methods.
func NewLineMessage(p level.Priority, args ...interface{}) Composer {
m := NewLine(args...)
_ = m.SetPriority(p)
return m
}
// NewLine returns a message Composer roughly equivalent to
// fmt.Sprintln().
func NewLine(args ...interface{}) Composer {
m := &lineMessenger{}
for _, arg := range args {
if arg != nil {
m.lines = append(m.lines, arg)
}
}
return m
}
func newLinesFromStrings(p level.Priority, args []string) Composer {
m := &lineMessenger{}
_ = m.SetPriority(p)
for _, arg := range args {
if arg != "" {
m.lines = append(m.lines, arg)
}
}
return m
}
func (l *lineMessenger) Loggable() bool {
if len(l.lines) <= 0 {
return false
}
for idx := range l.lines {
if l.lines[idx] != "" {
return true
}
}
return false
}
func (l *lineMessenger) String() string {
if l.Message == "" {
l.Message = strings.Trim(fmt.Sprintln(l.lines...), "\n ")
}
return l.Message
}
func (l *lineMessenger) Raw() interface{} {
_ = l.Collect()
_ = l.String()
return l
}