From 3c992f0d6bf4482d85368da48d2a37fa6a5a120b Mon Sep 17 00:00:00 2001 From: Thierry FOURNIER Date: Thu, 6 Apr 2023 00:49:11 +0200 Subject: [PATCH] Export function to display packet content (#37) 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 --- ber.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/ber.go b/ber.go index e6d6caa..bafa786 100644 --- a/ber.go +++ b/ber.go @@ -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] @@ -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)