node: avoid clobbering terminal with binary data

commit-id:222d1f31
This commit is contained in:
Leo 2022-01-25 12:59:18 +01:00 committed by Leopold Schabel
parent 7a9a5e6487
commit b8c30314b5
3 changed files with 34 additions and 7 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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