-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
49 lines (43 loc) · 925 Bytes
/
util.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
package xlog
import (
"fmt"
"io"
"strings"
)
type color int
const (
red color = 31
green color = 32
yellow color = 33
blue color = 34
gray color = 37
)
func colorPrint(w io.Writer, s string, c color) {
w.Write([]byte{0x1b, '[', byte('0' + c/10), byte('0' + c%10), 'm'})
w.Write([]byte(s))
w.Write([]byte("\x1b[0m"))
}
func needsQuotedValueRune(r rune) bool {
return r <= ' ' || r == '=' || r == '"'
}
// writeValue writes a value on the writer in a logfmt compatible way
func writeValue(w io.Writer, v interface{}) (err error) {
switch v := v.(type) {
case nil:
_, err = w.Write([]byte("null"))
case string:
if strings.IndexFunc(v, needsQuotedValueRune) != -1 {
s := fmt.Sprintf("%q", v)
_, err = w.Write([]byte(s))
} else {
_, err = w.Write([]byte(v))
}
case error:
s := v.Error()
err = writeValue(w, s)
default:
s := fmt.Sprint(v)
err = writeValue(w, s)
}
return
}