add recorder for serial handler
This commit is contained in:
@ -12,33 +12,52 @@ import (
|
||||
|
||||
type recorderConn struct {
|
||||
net.Conn
|
||||
recorder recorder.Recorder
|
||||
recorder recorder.RecorderObject
|
||||
}
|
||||
|
||||
func (c *recorderConn) Read(b []byte) (n int, err error) {
|
||||
n, err = c.Conn.Read(b)
|
||||
|
||||
if n > 0 && c.recorder != nil {
|
||||
if n > 0 && c.recorder.Recorder != nil {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteByte('>')
|
||||
buf.WriteString(time.Now().Format("2006-01-02 15:04:05.000"))
|
||||
buf.WriteByte('\n')
|
||||
buf.WriteString(hex.Dump(b[:n]))
|
||||
c.recorder.Record(context.Background(), buf.Bytes())
|
||||
if c.recorder.Options != nil && c.recorder.Options.Direction {
|
||||
buf.WriteByte('>')
|
||||
}
|
||||
if c.recorder.Options != nil && c.recorder.Options.TimestampFormat != "" {
|
||||
buf.WriteString(time.Now().Format(c.recorder.Options.TimestampFormat))
|
||||
}
|
||||
if buf.Len() > 0 {
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
if c.recorder.Options != nil && c.recorder.Options.Hexdump {
|
||||
buf.WriteString(hex.Dump(b[:n]))
|
||||
} else {
|
||||
buf.Write(b[:n])
|
||||
}
|
||||
c.recorder.Recorder.Record(context.Background(), buf.Bytes())
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *recorderConn) Write(b []byte) (int, error) {
|
||||
if c.recorder != nil {
|
||||
if c.recorder.Recorder != nil {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteByte('<')
|
||||
buf.WriteString(time.Now().Format("2006-01-02 15:04:05.000"))
|
||||
buf.WriteByte('\n')
|
||||
buf.WriteString(hex.Dump(b))
|
||||
c.recorder.Record(context.Background(), buf.Bytes())
|
||||
if c.recorder.Options != nil && c.recorder.Options.Direction {
|
||||
buf.WriteByte('<')
|
||||
}
|
||||
if c.recorder.Options != nil && c.recorder.Options.TimestampFormat != "" {
|
||||
buf.WriteString(time.Now().Format(c.recorder.Options.TimestampFormat))
|
||||
}
|
||||
if buf.Len() > 0 {
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
if c.recorder.Options != nil && c.recorder.Options.Hexdump {
|
||||
buf.WriteString(hex.Dump(b))
|
||||
} else {
|
||||
buf.Write(b)
|
||||
}
|
||||
c.recorder.Recorder.Record(context.Background(), buf.Bytes())
|
||||
}
|
||||
return c.Conn.Write(b)
|
||||
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ type serialHandler struct {
|
||||
router *chain.Router
|
||||
md metadata
|
||||
options handler.Options
|
||||
recorder recorder.Recorder
|
||||
recorder recorder.RecorderObject
|
||||
}
|
||||
|
||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||
@ -54,7 +54,7 @@ func (h *serialHandler) Init(md md.Metadata) (err error) {
|
||||
if opts := h.router.Options(); opts != nil {
|
||||
for _, ro := range opts.Recorders {
|
||||
if ro.Record == xrecorder.RecorderServiceHandlerSerial {
|
||||
h.recorder = ro.Recorder
|
||||
h.recorder = ro
|
||||
break
|
||||
}
|
||||
}
|
||||
@ -96,11 +96,9 @@ func (h *serialHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
||||
|
||||
log.Debugf("%s >> %s", conn.LocalAddr(), target.Addr)
|
||||
|
||||
if h.recorder != nil {
|
||||
conn = &recorderConn{
|
||||
Conn: conn,
|
||||
recorder: h.recorder,
|
||||
}
|
||||
conn = &recorderConn{
|
||||
Conn: conn,
|
||||
recorder: h.recorder,
|
||||
}
|
||||
|
||||
// serial port
|
||||
|
Reference in New Issue
Block a user