Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Commit

Permalink
more compact output
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <[email protected]>
  • Loading branch information
achille-roussel committed Jun 3, 2023
1 parent e4f752a commit 6ac406d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
60 changes: 32 additions & 28 deletions internal/debug/nettrace/nettrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/hex"
"fmt"
"net"
"time"

"github.com/stealthrocket/timecraft/internal/stream"
Expand Down Expand Up @@ -141,43 +142,46 @@ var (
)

type Event struct {
Time time.Time `json:"time" yaml:"time"`
Type EventType `json:"type" yaml:"type"`
Proto Protocol `json:"protocol" yaml:"protocol"`
Error wasi.Errno `json:"error,omitempty" yaml:"error,omitempty"`
FD wasi.FD `json:"fd" yaml:"fd"`
Addr wasi.SocketAddress `json:"addr,omitempty" yaml:"addr,omitempty"`
Peer wasi.SocketAddress `json:"peer,omitempty" yaml:"peer,omitempty"`
Data []Bytes `json:"data,omitempty" yaml:"data,omitempty"`
Record int64 `json:"record" yaml:"record"`
Time time.Time `json:"time" yaml:"time"`
Type EventType `json:"type" yaml:"type"`
Proto Protocol `json:"proto" yaml:"proto"`
Error wasi.Errno `json:"error,omitempty" yaml:"error,omitempty"`
FD wasi.FD `json:"fd" yaml:"fd"`
Addr net.Addr `json:"addr,omitempty" yaml:"addr,omitempty"`
Peer net.Addr `json:"peer,omitempty" yaml:"peer,omitempty"`
Data []Bytes `json:"data,omitempty" yaml:"data,omitempty"`
}

func (e Event) Format(w fmt.State, _ rune) {
fmt.Fprintf(w, `%s %s %s (fd=%d, size=%d, %s)
ADDR: %s
PEER: %s
`,
src := e.Addr
dst := e.Peer

switch e.Type & EventTypeMask {
case Accept, Receive:
src, dst = dst, src
}

fmt.Fprintf(w, "%08X %s %s %s > %s: %s %d %v\n",
e.Record,
e.Time.In(time.Local).Format("2006/01/02 15:04:05.000000"),
e.Proto,
socketAddressString(src),
socketAddressString(dst),
e.Type,
e.FD,
iovecSize(e.Data),
e.Error,
socketAddressString(e.Addr),
socketAddressString(e.Peer),
)

if w.Flag('+') {
const separator = `
------------------------------------------------------------------------------
`
if e.Type == Receive || e.Type == Send {
fmt.Fprint(w, separator)
if (e.Type == Receive || e.Type == Send) && iovecSize(e.Data) > 0 {
fmt.Fprintln(w)
hexdump := hex.Dumper(w)
for _, iov := range e.Data {
_, _ = hexdump.Write(iov)
}
hexdump.Close()
fmt.Fprint(w, separator[1:])
fmt.Fprintln(w)
}
}
}
Expand All @@ -189,7 +193,7 @@ func iovecSize(iovs []Bytes) (size wasi.Size) {
return size
}

func socketAddressString(addr wasi.SocketAddress) string {
func socketAddressString(addr net.Addr) string {
if addr == nil {
return "?"
}
Expand Down Expand Up @@ -312,7 +316,7 @@ func (r *EventReader) Read(events []Event) (n int, err error) {
if errno == wasi.EAGAIN {
errno = wasi.ESUCCESS
}
if (errno == wasi.ESUCCESS) && (size == 0) {
if (errno == wasi.ESUCCESS) && (int32(size) <= 0) {
continue
}
socket, ok := r.sockets[fd]
Expand All @@ -332,7 +336,7 @@ func (r *EventReader) Read(events []Event) (n int, err error) {
if errno == wasi.EAGAIN {
errno = wasi.ESUCCESS
}
if (errno == wasi.ESUCCESS) && (size == 0) {
if (errno == wasi.ESUCCESS) && (int32(size) <= 0) {
continue
}
socket, ok := r.sockets[fd]
Expand Down Expand Up @@ -380,7 +384,7 @@ func (r *EventReader) Read(events []Event) (n int, err error) {
if errno == wasi.EAGAIN {
errno = wasi.ESUCCESS
}
if (errno == wasi.ESUCCESS) && (size == 0) {
if (errno == wasi.ESUCCESS) && (int32(size) <= 0) {
continue
}
socket, ok := r.sockets[fd]
Expand All @@ -400,7 +404,7 @@ func (r *EventReader) Read(events []Event) (n int, err error) {
if errno == wasi.EAGAIN {
errno = wasi.ESUCCESS
}
if (errno == wasi.ESUCCESS) && (size == 0) {
if (errno == wasi.ESUCCESS) && (int32(size) <= 0) {
continue
}
socket, ok := r.sockets[fd]
Expand Down Expand Up @@ -496,7 +500,7 @@ func (r *EventReader) Read(events []Event) (n int, err error) {
if errno == wasi.EAGAIN {
errno = wasi.ESUCCESS
}
if (errno == wasi.ESUCCESS) && (size == 0) {
if (errno == wasi.ESUCCESS) && (int32(size) <= 0) {
continue
}
socket, ok := r.sockets[fd]
Expand All @@ -517,7 +521,7 @@ func (r *EventReader) Read(events []Event) (n int, err error) {
if errno == wasi.EAGAIN {
errno = wasi.ESUCCESS
}
if (errno == wasi.ESUCCESS) && (size == 0) {
if (errno == wasi.ESUCCESS) && (int32(size) <= 0) {
continue
}
socket, ok := r.sockets[fd]
Expand Down
2 changes: 1 addition & 1 deletion trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func trace(ctx context.Context, args []string) error {
default:
writer = textprint.NewWriter[nettrace.Event](os.Stdout,
textprint.Format[nettrace.Event](format),
textprint.Separator[nettrace.Event]("\n"),
textprint.Separator[nettrace.Event](""),
)
}
defer writer.Close()
Expand Down

0 comments on commit 6ac406d

Please sign in to comment.