-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorizers_interface.go
124 lines (106 loc) · 2.38 KB
/
colorizers_interface.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
112
113
114
115
116
117
118
119
120
121
122
123
124
package glog
import (
"fmt"
"strings"
)
// Auto will automatically choose a highlighter based on the type of the given `interface{}`s.
//
// Related config setting(s):
//
// - `LoggerConfig.AutoFloatPrecision`
// - `LoggerConfig.ColorNil`
func Auto(values ...interface{}) string {
res := []string{}
for _, i := range values {
if ok, v := castBoolSlice(i); ok {
res = append(res, Bool(v...))
continue
}
if ok, v := castIntSlice(i); ok {
res = append(res, Int(v...))
continue
}
if ok, v := castUintSlice(i); ok {
res = append(res, Uint(v...))
continue
}
if ok, v := castFloatSlice(i); ok {
for _, f := range v {
if f >= -1.0 && f <= 1.0 {
res = append(res, Percentage(f, LoggerConfig.AutoFloatPrecision))
} else {
res = append(res, Float(f, LoggerConfig.AutoFloatPrecision))
}
}
continue
}
if ok, v := castTimeSlice(i); ok {
for _, t := range v {
res = append(res, DateTime(t))
}
continue
}
if ok, v := castDurationSlice(i); ok {
for _, d := range v {
res = append(res, Duration(d.Seconds()))
}
continue
}
if ok, v := castStringSlice(i); ok {
res = append(res, Highlight(v...))
continue
}
if ok, v := castBool(i); ok {
res = append(res, Bool(v))
continue
}
if ok, v := castInt(i); ok {
res = append(res, Int(v))
continue
}
if ok, v := castUint(i); ok {
res = append(res, Uint(v))
continue
}
if ok, normalized, v := castFloat(i); ok {
if normalized {
res = append(res, Percentage(v, LoggerConfig.AutoFloatPrecision))
} else {
res = append(res, Float(v, LoggerConfig.AutoFloatPrecision))
}
continue
}
if ok, v := castTime(i); ok {
res = append(res, DateTime(v))
continue
}
if ok, v := castDuration(i); ok {
res = append(res, Duration(v.Seconds()))
continue
}
if ok, v := castString(i); ok {
// maybe it's a path
switch identifyPath(v) {
case INVALID_PATH:
v = Highlight(v)
case FILE_PATH:
v = File(v)
case URL_PATH:
v = URL(v)
}
res = append(res, v)
continue
}
if i == nil {
res = append(res, Wrap("nil", LoggerConfig.ColorNil))
continue
}
if ok, v := castInterfaceSlice(i); ok {
res = append(res, Auto(v...))
continue
}
// if we get we have an unknown type, let's output it as string
res = append(res, Highlight(fmt.Sprint(i)))
}
return strings.Join(res, ", ")
}