-
Notifications
You must be signed in to change notification settings - Fork 3
/
ui_funcs.go
41 lines (35 loc) · 896 Bytes
/
ui_funcs.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
package glass
import (
"fmt"
"github.com/hamba/logger/v2"
lctx "github.com/hamba/logger/v2/ctx"
"github.com/zserge/lorca"
)
func (ui *UI) bindFuncs(log *logger.Logger) error {
return bindLogger(ui.win, log)
}
func bindLogger(ui lorca.UI, log *logger.Logger) error {
if err := ui.Bind("log.debug", logFunc(log.Debug)); err != nil {
return err
}
if err := ui.Bind("log.info", logFunc(log.Info)); err != nil {
return err
}
return ui.Bind("log.error", logFunc(log.Error))
}
func logFunc(fn func(string, ...logger.Field)) func(string, []any) {
return func(msg string, args []any) {
if len(args)%2 == 1 {
args = append(args, "Missing")
}
ctx := make([]logger.Field, 0, len(args)/2)
for i := 0; i < len(args); i += 2 {
k, ok := args[i].(string)
if !ok {
k = fmt.Sprint(args[i])
}
ctx = append(ctx, lctx.Interface(k, args[i+1]))
}
fn(msg, ctx...)
}
}