Skip to content

Commit

Permalink
chore(mtproto): improve type name logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Jan 6, 2021
1 parent 7f4bb0d commit d3c266b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
7 changes: 5 additions & 2 deletions mtproto/handle_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func (c *Conn) handleResult(b *bin.Buffer) error {
if err := res.Decode(b); err != nil {
return xerrors.Errorf("decode: %x", err)
}
c.log.With(
c.logWithType(b).Debug("Handle result",
zap.Int64("msg_id", res.RequestMessageID),
).Debug("Handle result")
)

// Handling gzipped results.
id, err := b.PeekID()
Expand All @@ -32,6 +32,9 @@ func (c *Conn) handleResult(b *bin.Buffer) error {

// Replacing buffer so callback will deal with uncompressed data.
b = content
c.logWithType(b).Debug("Decompressed",
zap.Int64("msg_id", res.RequestMessageID),
)

// Replacing id with inner id if error is compressed for any reason.
if id, err = b.PeekID(); err != nil {
Expand Down
11 changes: 1 addition & 10 deletions mtproto/read_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mtproto
import (
"context"
"errors"
"fmt"
"net"

"go.uber.org/zap"
Expand All @@ -23,15 +22,7 @@ func (c *Conn) handleMessage(b *bin.Buffer) error {
return xerrors.Errorf("failed to determine message type: %w", err)
}

typeStr := "unknown"
if s := c.types.Get(id); s != "" {
typeStr = s
}
c.log.With(
zap.String("type_id", fmt.Sprintf("0x%x", id)),
zap.String("type_str", typeStr),
).Debug("HandleMessage")

c.logWithType(b).Debug("Handle message")
switch id {
case mt.NewSessionCreatedTypeID:
return c.handleSessionCreated(b)
Expand Down
29 changes: 29 additions & 0 deletions mtproto/zap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mtproto

import (
"fmt"

"go.uber.org/zap"

"github.com/gotd/td/bin"
)

func (c *Conn) logWithType(b *bin.Buffer) *zap.Logger {
id, err := b.PeekID()
if err != nil {
// Type info not available.
return c.log
}

// Adding hex id of type.
typeIDStr := fmt.Sprintf("0x%x", id)
log := c.log.With(zap.String("type_id", typeIDStr))

// Adding verbose type name if available.
typeName := c.types.Get(id)
if typeName != "" {
log = log.With(zap.String("type_name", typeName))
}

return log
}

0 comments on commit d3c266b

Please sign in to comment.