|
1 |
| -package sink |
| 1 | +package stdiosink |
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 | 4 | "fmt"
|
6 |
| - "io" |
7 |
| - "sort" |
8 |
| - "strings" |
9 |
| - "text/tabwriter" |
10 |
| - "time" |
11 | 5 |
|
12 | 6 | "github.com/fatih/color"
|
13 | 7 | "github.com/humanlogio/humanlog/internal/pkg/config"
|
14 |
| - "github.com/humanlogio/humanlog/internal/pkg/model" |
15 | 8 | )
|
16 | 9 |
|
17 |
| -var ( |
18 |
| - eol = [...]byte{'\n'} |
19 |
| -) |
20 |
| - |
21 |
| -type Stdio struct { |
22 |
| - w io.Writer |
23 |
| - opts StdioOpts |
24 |
| - |
25 |
| - lastEvent *model.Event |
26 |
| - lastKVs map[string]string |
27 |
| -} |
28 |
| - |
29 |
| -type StdioOpts struct { |
30 |
| - Skip map[string]struct{} |
31 |
| - Keep map[string]struct{} |
32 |
| - SkipUnchanged bool |
33 |
| - SortLongest bool |
34 |
| - TimeFormat string |
35 |
| - Truncates bool |
36 |
| - TruncateLength int |
37 |
| - |
38 |
| - LightBg bool |
39 |
| - Palette Palette |
40 |
| -} |
41 |
| - |
42 |
| -var DefaultStdioOpts = StdioOpts{ |
43 |
| - SkipUnchanged: true, |
44 |
| - SortLongest: true, |
45 |
| - Truncates: true, |
46 |
| - LightBg: false, |
47 |
| - TruncateLength: 15, |
48 |
| - TimeFormat: time.Stamp, |
49 |
| - |
50 |
| - Palette: DefaultPalette, |
51 |
| -} |
52 |
| - |
53 |
| -func StdioOptsFrom(cfg config.Config) (StdioOpts, []error) { |
54 |
| - var errs []error |
55 |
| - opts := DefaultStdioOpts |
56 |
| - if cfg.Skip != nil { |
57 |
| - opts.Skip = sliceToSet(cfg.Skip) |
58 |
| - } |
59 |
| - if cfg.Keep != nil { |
60 |
| - opts.Keep = sliceToSet(cfg.Keep) |
61 |
| - } |
62 |
| - if cfg.SortLongest != nil { |
63 |
| - opts.SortLongest = *cfg.SortLongest |
64 |
| - } |
65 |
| - if cfg.SkipUnchanged != nil { |
66 |
| - opts.SkipUnchanged = *cfg.SkipUnchanged |
67 |
| - } |
68 |
| - if cfg.Truncates != nil { |
69 |
| - opts.Truncates = *cfg.Truncates |
70 |
| - } |
71 |
| - if cfg.LightBg != nil { |
72 |
| - opts.LightBg = *cfg.LightBg |
73 |
| - } |
74 |
| - if cfg.TruncateLength != nil { |
75 |
| - opts.TruncateLength = *cfg.TruncateLength |
76 |
| - } |
77 |
| - if cfg.TimeFormat != nil { |
78 |
| - opts.TimeFormat = *cfg.TimeFormat |
79 |
| - } |
80 |
| - if cfg.Palette != nil { |
81 |
| - pl, err := PaletteFrom(*cfg.Palette) |
82 |
| - if err != nil { |
83 |
| - errs = append(errs, fmt.Errorf("invalid palette, using default one: %v", err)) |
84 |
| - } else { |
85 |
| - opts.Palette = *pl |
86 |
| - } |
87 |
| - } |
88 |
| - return opts, errs |
89 |
| -} |
90 |
| - |
91 |
| -var _ Sink = (*Stdio)(nil) |
92 |
| - |
93 |
| -func NewStdio(w io.Writer, opts StdioOpts) *Stdio { |
94 |
| - return &Stdio{ |
95 |
| - w: w, |
96 |
| - opts: opts, |
97 |
| - } |
98 |
| -} |
99 |
| - |
100 |
| -func (std *Stdio) Receive(ev *model.Event) error { |
101 |
| - if ev.Structured == nil { |
102 |
| - if _, err := std.w.Write(ev.Raw); err != nil { |
103 |
| - return err |
104 |
| - } |
105 |
| - return nil |
106 |
| - } |
107 |
| - data := ev.Structured |
108 |
| - |
109 |
| - buf := bytes.NewBuffer(nil) |
110 |
| - out := tabwriter.NewWriter(buf, 0, 1, 0, '\t', 0) |
111 |
| - |
112 |
| - var ( |
113 |
| - msgColor *color.Color |
114 |
| - msgAbsentColor *color.Color |
115 |
| - ) |
116 |
| - if std.opts.LightBg { |
117 |
| - msgColor = std.opts.Palette.MsgLightBgColor |
118 |
| - msgAbsentColor = std.opts.Palette.MsgAbsentLightBgColor |
119 |
| - } else { |
120 |
| - msgColor = std.opts.Palette.MsgDarkBgColor |
121 |
| - msgAbsentColor = std.opts.Palette.MsgAbsentDarkBgColor |
122 |
| - } |
123 |
| - var msg string |
124 |
| - if data.Msg == "" { |
125 |
| - msg = msgAbsentColor.Sprint("<no msg>") |
126 |
| - } else { |
127 |
| - msg = msgColor.Sprint(data.Msg) |
128 |
| - } |
129 |
| - |
130 |
| - lvl := strings.ToUpper(data.Level)[:imin(4, len(data.Level))] |
131 |
| - var level string |
132 |
| - switch data.Level { |
133 |
| - case "debug": |
134 |
| - level = std.opts.Palette.DebugLevelColor.Sprint(lvl) |
135 |
| - case "info": |
136 |
| - level = std.opts.Palette.InfoLevelColor.Sprint(lvl) |
137 |
| - case "warn", "warning": |
138 |
| - level = std.opts.Palette.WarnLevelColor.Sprint(lvl) |
139 |
| - case "error": |
140 |
| - level = std.opts.Palette.ErrorLevelColor.Sprint(lvl) |
141 |
| - case "fatal", "panic": |
142 |
| - level = std.opts.Palette.FatalLevelColor.Sprint(lvl) |
143 |
| - default: |
144 |
| - level = std.opts.Palette.UnknownLevelColor.Sprint(lvl) |
145 |
| - } |
146 |
| - |
147 |
| - var timeColor *color.Color |
148 |
| - if std.opts.LightBg { |
149 |
| - timeColor = std.opts.Palette.TimeLightBgColor |
150 |
| - } else { |
151 |
| - timeColor = std.opts.Palette.TimeDarkBgColor |
152 |
| - } |
153 |
| - |
154 |
| - _, _ = fmt.Fprintf(out, "%s |%s| %s\t %s", |
155 |
| - timeColor.Sprint(data.Time.Format(std.opts.TimeFormat)), |
156 |
| - level, |
157 |
| - msg, |
158 |
| - strings.Join(std.joinKVs(data, "="), "\t "), |
159 |
| - ) |
160 |
| - |
161 |
| - if err := out.Flush(); err != nil { |
162 |
| - return err |
163 |
| - } |
164 |
| - |
165 |
| - buf.Write(eol[:]) |
166 |
| - |
167 |
| - if _, err := buf.WriteTo(std.w); err != nil { |
168 |
| - return err |
169 |
| - } |
170 |
| - |
171 |
| - kvs := make(map[string]string, len(data.KVs)) |
172 |
| - for _, kv := range data.KVs { |
173 |
| - kvs[kv.Key] = kv.Value |
174 |
| - } |
175 |
| - std.lastEvent = ev |
176 |
| - std.lastKVs = kvs |
177 |
| - return nil |
178 |
| -} |
179 |
| - |
180 |
| -func (std *Stdio) joinKVs(data *model.Structured, sep string) []string { |
181 |
| - |
182 |
| - kv := make([]string, 0, len(data.KVs)) |
183 |
| - for _, pair := range data.KVs { |
184 |
| - k, v := pair.Key, pair.Value |
185 |
| - if !std.opts.shouldShowKey(k) { |
186 |
| - continue |
187 |
| - } |
188 |
| - |
189 |
| - if std.opts.SkipUnchanged { |
190 |
| - if lastV, ok := std.lastKVs[k]; ok && lastV == v && !std.opts.shouldShowUnchanged(k) { |
191 |
| - continue |
192 |
| - } |
193 |
| - } |
194 |
| - kstr := std.opts.Palette.KeyColor.Sprint(k) |
195 |
| - |
196 |
| - var vstr string |
197 |
| - if std.opts.Truncates && len(v) > std.opts.TruncateLength { |
198 |
| - vstr = v[:std.opts.TruncateLength] + "..." |
199 |
| - } else { |
200 |
| - vstr = v |
201 |
| - } |
202 |
| - vstr = std.opts.Palette.ValColor.Sprint(vstr) |
203 |
| - kv = append(kv, kstr+sep+vstr) |
204 |
| - } |
205 |
| - |
206 |
| - sort.Strings(kv) |
207 |
| - |
208 |
| - if std.opts.SortLongest { |
209 |
| - sort.Stable(byLongest(kv)) |
210 |
| - } |
211 |
| - |
212 |
| - return kv |
213 |
| -} |
214 |
| - |
215 |
| -func (opts *StdioOpts) shouldShowKey(key string) bool { |
216 |
| - if len(opts.Keep) != 0 { |
217 |
| - if _, keep := opts.Keep[key]; keep { |
218 |
| - return true |
219 |
| - } |
220 |
| - } |
221 |
| - if len(opts.Skip) != 0 { |
222 |
| - if _, skip := opts.Skip[key]; skip { |
223 |
| - return false |
224 |
| - } |
225 |
| - } |
226 |
| - return true |
227 |
| -} |
228 |
| - |
229 |
| -func (opts *StdioOpts) shouldShowUnchanged(key string) bool { |
230 |
| - if len(opts.Keep) != 0 { |
231 |
| - if _, keep := opts.Keep[key]; keep { |
232 |
| - return true |
233 |
| - } |
234 |
| - } |
235 |
| - return false |
236 |
| -} |
237 |
| - |
238 | 10 | var DefaultPalette = Palette{
|
239 | 11 | KeyColor: color.New(color.FgGreen),
|
240 | 12 | ValColor: color.New(color.FgHiWhite),
|
@@ -383,27 +155,3 @@ var colorAttributeIndex = map[string]color.Attribute{
|
383 | 155 | "bg_hi_cyan": color.BgHiCyan,
|
384 | 156 | "bg_hi_white": color.BgHiWhite,
|
385 | 157 | }
|
386 |
| - |
387 |
| -type byLongest []string |
388 |
| - |
389 |
| -func (s byLongest) Len() int { return len(s) } |
390 |
| -func (s byLongest) Less(i, j int) bool { return len(s[i]) < len(s[j]) } |
391 |
| -func (s byLongest) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
392 |
| - |
393 |
| -func imin(a, b int) int { |
394 |
| - if a < b { |
395 |
| - return a |
396 |
| - } |
397 |
| - return b |
398 |
| -} |
399 |
| - |
400 |
| -func sliceToSet(arr *[]string) map[string]struct{} { |
401 |
| - if arr == nil { |
402 |
| - return nil |
403 |
| - } |
404 |
| - out := make(map[string]struct{}) |
405 |
| - for _, key := range *arr { |
406 |
| - out[key] = struct{}{} |
407 |
| - } |
408 |
| - return out |
409 |
| -} |
0 commit comments