node: avoid clobbering terminal with binary data
commit-id:222d1f31
This commit is contained in:
parent
7a9a5e6487
commit
b8c30314b5
|
@ -0,0 +1,28 @@
|
|||
package guardiand
|
||||
|
||||
import (
|
||||
"go.uber.org/zap/buffer"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
type consoleEncoder struct {
|
||||
zapcore.Encoder
|
||||
}
|
||||
|
||||
func (e consoleEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
|
||||
buf, err := e.Encoder.EncodeEntry(entry, fields)
|
||||
if err != nil {
|
||||
buf.Free()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b := buf.Bytes()
|
||||
for i := range b {
|
||||
if unicode.IsControl(rune(b[i])) && !unicode.IsSpace(rune(b[i])) {
|
||||
b[i] = '\x1A' // Substitute character
|
||||
}
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
|
@ -245,12 +245,11 @@ func runNode(cmd *cobra.Command, args []string) {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
cfg := zap.NewDevelopmentConfig()
|
||||
cfg.Level = zap.NewAtomicLevelAt(zapcore.Level(lvl))
|
||||
logger, err := cfg.Build()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logger := zap.New(zapcore.NewCore(
|
||||
consoleEncoder{zapcore.NewConsoleEncoder(
|
||||
zap.NewDevelopmentEncoderConfig())},
|
||||
zapcore.AddSync(zapcore.Lock(os.Stderr)),
|
||||
zap.NewAtomicLevelAt(zapcore.Level(lvl))))
|
||||
|
||||
if *unsafeDevMode {
|
||||
// Use the hostname as nodeName. For production, we don't want to do this to
|
||||
|
|
|
@ -293,7 +293,7 @@ func Run(obsvC chan *gossipv1.SignedObservation, sendC chan []byte, signedInC ch
|
|||
err = proto.Unmarshal(envelope.Data, &msg)
|
||||
if err != nil {
|
||||
logger.Info("received invalid message",
|
||||
zap.String("data", string(envelope.Data)),
|
||||
zap.Binary("data", envelope.Data),
|
||||
zap.String("from", envelope.GetFrom().String()))
|
||||
p2pMessagesReceived.WithLabelValues("invalid").Inc()
|
||||
continue
|
||||
|
|
Loading…
Reference in New Issue