forked from ox/aim-oscar-server
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oscar_log_handler.go
133 lines (116 loc) · 3.04 KB
/
oscar_log_handler.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
125
126
127
128
129
130
131
132
133
package main
import (
"aim-oscar/oscar"
"aim-oscar/util"
"context"
"io"
"log"
"strings"
"sync"
"github.com/fatih/color"
"golang.org/x/exp/slog"
)
type OSCARLogHandler struct {
logger *log.Logger
level slog.Level
attrs []slog.Attr
openGroup string
lock *sync.Mutex
}
func (h *OSCARLogHandler) Handle(ctx context.Context, r slog.Record) error {
h.lock.Lock()
level := r.Level.String() + ":"
switch r.Level {
case slog.LevelDebug:
level = color.MagentaString(level)
case slog.LevelInfo:
level = color.BlueString(level)
case slog.LevelWarn:
level = color.YellowString(level)
case slog.LevelError:
level = color.RedString(level)
}
timeStr := r.Time.Format("[2006-01-02 15:05:05.000]")
msg := color.CyanString(r.Message)
h.logger.Println(timeStr, level, msg)
for _, attr := range h.attrs {
h.logger.Printf(" %s=%s\n", color.YellowString(h.openGroup+attr.Key), color.WhiteString("%v", attr.Value.Any()))
}
r.Attrs(func(a slog.Attr) bool {
if a.Key == "flap" {
return true
} else {
h.logger.Printf(" %s=%s\n", color.YellowString(h.openGroup+a.Key), color.WhiteString("%v", a.Value.Any()))
}
return true
})
r.Attrs(func(a slog.Attr) bool {
if a.Key == "flap" {
flap := a.Value.Any().(*oscar.FLAP)
snac := &oscar.SNAC{}
if err := snac.UnmarshalBinary(flap.Data.Bytes()); err == nil {
h.logger.Printf(" FLAP(CH:%d, SEQ:%d):", flap.Header.Channel, flap.Header.SequenceNumber)
h.logger.Printf(" SNAC(%#x, %#x):", snac.Header.Family, snac.Header.Subtype)
tlvs, err := oscar.UnmarshalTLVs(snac.Data.Bytes())
if err == nil {
for _, tlv := range tlvs {
h.logger.Printf(" TLV(%#x):", tlv.Type)
tlvLines := strings.Split(util.PrettyBytes(tlv.Data), "\n")
for _, line := range tlvLines {
h.logger.Printf(" %s\n", line)
}
}
} else {
snaclines := strings.Split(util.PrettyBytes(snac.Data.Bytes()), "\n")
for _, line := range snaclines {
h.logger.Printf(" %s\n", line)
}
}
} else {
flapLines := strings.Split(flap.String(), "\n")
for _, line := range flapLines {
h.logger.Printf(" %s\n", line)
}
}
}
return true
})
h.lock.Unlock()
return nil
}
func (h *OSCARLogHandler) Enabled(ctx context.Context, level slog.Level) bool {
return level >= h.level
}
func (h *OSCARLogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &OSCARLogHandler{
attrs: append(h.attrs, attrs...),
logger: h.logger,
level: h.level,
lock: h.lock,
openGroup: h.openGroup,
}
}
func (h *OSCARLogHandler) WithGroup(name string) slog.Handler {
return &OSCARLogHandler{
attrs: h.attrs,
logger: h.logger,
level: h.level,
lock: h.lock,
openGroup: h.openGroup + name + ".",
}
}
func NewOSCARLogHandler(
out io.Writer,
opts *slog.HandlerOptions,
) *OSCARLogHandler {
if opts == nil {
opts = &slog.HandlerOptions{
Level: slog.LevelInfo,
}
}
return &OSCARLogHandler{
level: opts.Level.Level(),
logger: log.New(out, "", 0),
lock: &sync.Mutex{},
}
}