Skip to content

Commit

Permalink
Export function to display packet content (#37)
Browse files Browse the repository at this point in the history
This patch split PrintPacket in two parts, the second part
is DescribePacket a function which describe the packet content
without recursion, indentation, nor jump line.

The goal is providing a simple way logging detailled protocol
errors on speciafic output (other than io.Stdout)

Co-authored-by: Thierry Fournier <[email protected]>
  • Loading branch information
thierry-f-78 and thierry-f-78 authored Apr 5, 2023
1 parent dab8761 commit 3c992f0
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions ber.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,10 @@ func PrintPacket(p *Packet) {
printPacket(os.Stdout, p, 0, false)
}

func printPacket(out io.Writer, p *Packet, indent int, printBytes bool) {
indentStr := ""

for len(indentStr) != indent {
indentStr += " "
}
// Return a string describing packet content. This is not recursive,
// If the packet is a sequence, use `printPacket()`, or browse
// sequence yourself.
func DescribePacket(p *Packet) string {

classStr := ClassMap[p.ClassType]

Expand All @@ -194,7 +192,17 @@ func printPacket(out io.Writer, p *Packet, indent int, printBytes bool) {
description = p.Description + ": "
}

_, _ = fmt.Fprintf(out, "%s%s(%s, %s, %s) Len=%d %q\n", indentStr, description, classStr, tagTypeStr, tagStr, p.Data.Len(), value)
return fmt.Sprintf("%s(%s, %s, %s) Len=%d %q", description, classStr, tagTypeStr, tagStr, p.Data.Len(), value)
}

func printPacket(out io.Writer, p *Packet, indent int, printBytes bool) {
indentStr := ""

for len(indentStr) != indent {
indentStr += " "
}

_, _ = fmt.Fprintf(out, "%s%s\n", indentStr, DescribePacket(p))

if printBytes {
PrintBytes(out, p.Bytes(), indentStr)
Expand Down

0 comments on commit 3c992f0

Please sign in to comment.