lotus/node/modules/tracer/json_transport.go

42 lines
756 B
Go
Raw Normal View History

2021-09-15 11:41:03 +00:00
package tracer
import (
"encoding/json"
"fmt"
2021-09-15 11:41:03 +00:00
"os"
)
type jsonTracerTransport struct {
out *os.File
}
func NewJsonTracerTransport(file string) (TracerTransport, error) {
out, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0660)
if err != nil {
return nil, err
}
2021-09-15 11:41:03 +00:00
return &jsonTracerTransport{
out: out,
}, nil
2021-09-15 11:41:03 +00:00
}
2021-09-15 14:16:12 +00:00
func (jtt *jsonTracerTransport) Transport(evt TracerTransportEvent) error {
var e interface{}
2021-09-15 14:16:12 +00:00
if evt.lotusTraceEvent != nil {
e = *evt.lotusTraceEvent
} else if evt.pubsubTraceEvent != nil {
e = *evt.pubsubTraceEvent
} else {
return nil
}
2021-09-15 14:16:12 +00:00
jsonEvt, err := json.Marshal(e)
if err != nil {
return fmt.Errorf("error while marshaling event: %s", err)
}
2021-09-15 14:16:12 +00:00
_, err = jtt.out.Write(jsonEvt)
2021-09-15 11:41:03 +00:00
return err
}