decoder

package
v0.0.0-...-93906e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: Apache-2.0, MIT Imports: 13 Imported by: 0

Documentation

Overview

Package decoder provides decoding of defmt log frames produced by resource-constrained devices.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnexpectedEOF = errors.New("unexpected end of stream")
	ErrMalformed     = errors.New("malformed data")
)

Sentinel errors returned by Decode and StreamDecoder.

View Source
var DEFMT_VERSIONS = []string{"3", "4"}

DEFMT_VERSIONS lists the supported defmt wire format versions.

Functions

This section is empty.

Types

type Arg

type Arg struct {
	Kind ArgKind

	Bool bool
	F32  float32
	F64  float64

	// For ArgKindUxx: non-negative big.Int; for ArgKindIxx: signed big.Int
	BigInt *big.Int

	// For ArgKindStr, ArgKindIStr, ArgKindPreformatted
	Str string

	// For ArgKindFormat and ArgKindFormatSequence
	Format string
	Args   []Arg

	// For ArgKindFormatSlice
	Elements []FormatSliceElement

	// For ArgKindSlice
	Slice []byte

	// For ArgKindChar
	Char rune
}

Arg is a decoded argument from a defmt frame.

type ArgKind

type ArgKind int

ArgKind identifies which union arm of Arg is active.

const (
	ArgKindBool ArgKind = iota
	ArgKindF32
	ArgKindF64
	ArgKindUxx            // unsigned integer (up to u128)
	ArgKindIxx            // signed integer (up to i128)
	ArgKindStr            // dynamic string
	ArgKindIStr           // interned string
	ArgKindFormat         // nested formatted value
	ArgKindFormatSlice    // slice of formatted values
	ArgKindFormatSequence // sequence of formats
	ArgKindSlice          // []byte
	ArgKindChar           // rune
	ArgKindPreformatted   // pre-formatted string
)

type BitflagsKey

type BitflagsKey struct {
	Ident     string
	Package   string
	Disambig  string
	CrateName *string
}

BitflagsKey uniquely identifies a defmt::bitflags! invocation.

type BitflagsValue

type BitflagsValue struct {
	Name  string
	Value uint64 // stored as uint64; actual type is u128 in Rust

}

BitflagsValue is a named bitflag with its value.

type DisplayHint

type DisplayHint struct {
	Kind          DisplayHintKind
	Alternate     bool
	Uppercase     bool
	ZeroPad       int
	Precision     TimePrecision
	Name          string // for Bitflags
	Package       string // for Bitflags
	Disambiguator string // for Bitflags
	CrateName     *string
	UnknownStr    string // for Unknown kind
}

DisplayHint describes how a value should be displayed.

type DisplayHintKind

type DisplayHintKind int

DisplayHintKind is the kind of display hint.

const (
	DisplayHintKindNone        DisplayHintKind = iota
	DisplayHintKindHexadecimal                 // :x or :X
	DisplayHintKindOctal                       // :o
	DisplayHintKindBinary                      // :b
	DisplayHintKindAscii                       // :a
	DisplayHintKindDebug                       // :?
	DisplayHintKindSeconds                     // :us or :ms
	DisplayHintKindTime                        // :tus, :tms, :ts
	DisplayHintKindISO8601                     // :iso8601ms or :iso8601s
	DisplayHintKindBitflags                    // __internal_bitflags_...
	DisplayHintKindCbor                        // :cbor
	DisplayHintKindUnknown
)

type Encoding

type Encoding int

Encoding describes how defmt frames are encoded.

const (
	EncodingRaw    Encoding = iota // no encoding
	EncodingRzcobs                 // Reverse Zero-compressing COBS
)

func (Encoding) CanRecover

func (e Encoding) CanRecover() bool

CanRecover reports whether the encoding can recover from missed bytes.

func (Encoding) String

func (e Encoding) String() string

type FormatSliceElement

type FormatSliceElement struct {
	Format string
	Args   []Arg
}

FormatSliceElement is one element in a FormatSlice arg.

type Fragment

type Fragment struct {
	IsLiteral bool
	Literal   string    // valid when IsLiteral
	Param     Parameter // valid when !IsLiteral
}

Fragment is a part of a format string: either a literal string or a parameter.

func Parse

func Parse(format string) ([]Fragment, error)

Parse parses a defmt format string into fragments (ForwardsCompatible mode).

type Frame

type Frame struct {
	// contains filtered or unexported fields
}

Frame is a decoded defmt log frame.

func (*Frame) Display

func (f *Frame) Display(colored bool) string

Display returns the fully formatted log line including timestamp and level. When colored is true, ANSI color codes are used for the level.

func (*Frame) DisplayMessage

func (f *Frame) DisplayMessage() string

DisplayMessage returns only the formatted message (no timestamp or level).

func (*Frame) DisplayTimestamp

func (f *Frame) DisplayTimestamp() string

DisplayTimestamp returns the formatted timestamp, or empty string if none.

func (*Frame) Format

func (f *Frame) Format() string

Format returns the raw defmt format string.

func (*Frame) Index

func (f *Frame) Index() uint64

Index returns the raw defmt string index of this frame.

func (*Frame) Level

func (f *Frame) Level() *Level

Level returns the log level, or nil if the frame has no level (e.g. println!).

func (*Frame) TimestampArgs

func (f *Frame) TimestampArgs() []Arg

TimestampArgs returns the decoded arguments for the timestamp format string. Returns nil if the frame has no timestamp.

type Level

type Level int

Level is the log level of a defmt message.

const (
	LevelTrace Level = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
)

func (Level) String

func (l Level) String() string

type ParamType

type ParamType struct {
	Kind     TypeKind
	BitStart uint8 // for BitField: start bit (inclusive)
	BitEnd   uint8 // for BitField: end bit (exclusive)
	ArrayLen int   // for FormatArray and U8Array
}

ParamType represents the type of a format parameter.

type Parameter

type Parameter struct {
	Index int
	Type  ParamType
	Hint  *DisplayHint // nil if no hint
}

Parameter is a parsed format parameter.

type StreamDecoder

type StreamDecoder interface {
	// Received feeds new data into the decoder's buffer.
	Received(data []byte)
	// Decode attempts to decode one frame from the buffered data.
	// Returns ErrUnexpectedEOF if more data is needed, or ErrMalformed on error.
	Decode() (*Frame, error)
}

StreamDecoder buffers incoming binary data and decodes defmt frames.

type StringEntry

type StringEntry struct {
	Tag    Tag
	String string
}

StringEntry holds a tag and format string.

type Table

type Table struct {
	Timestamp *TableEntry

	Bitflags map[BitflagsKey][]BitflagsValue
	// contains filtered or unexported fields
}

Table holds the decoded defmt metadata from an ELF file.

func ParseELF

func ParseELF(data []byte) (*Table, error)

ParseELF parses an ELF binary and extracts the defmt Table. Returns nil with no error if the ELF does not use defmt.

func ParseELFIgnoreVersion

func ParseELFIgnoreVersion(data []byte) (*Table, error)

ParseELFIgnoreVersion is like ParseELF but skips the defmt version check.

func (*Table) Decode

func (t *Table) Decode(data []byte) (*Frame, int, error)

Decode decodes one defmt frame from data and returns the frame plus the number of bytes consumed.

func (*Table) Encoding

func (t *Table) Encoding() Encoding

Encoding returns the frame encoding used by the firmware.

func (*Table) HasTimestamp

func (t *Table) HasTimestamp() bool

HasTimestamp reports whether the table has a timestamp format.

func (*Table) Indices

func (t *Table) Indices() []uint64

Indices returns all indices that correspond to log-level or println entries.

func (*Table) IsEmpty

func (t *Table) IsEmpty() bool

IsEmpty reports whether the table has no entries.

func (*Table) NewStreamDecoder

func (t *Table) NewStreamDecoder() StreamDecoder

NewStreamDecoder creates a StreamDecoder appropriate for this table's encoding.

type TableEntry

type TableEntry struct {
	StringEntry StringEntry
	RawSymbol   string
}

TableEntry combines a format string with its raw ELF symbol.

type Tag

type Tag int

Tag classifies the origin of a defmt format string.

const (
	TagPrim Tag = iota
	TagDerived
	TagBitflags
	TagWrite
	TagStr
	TagTimestamp
	TagBitflagsValue
	TagPrintln
	TagTrace
	TagDebug
	TagInfo
	TagWarn
	TagError
)

type TimePrecision

type TimePrecision int

TimePrecision is the precision of a time display hint.

const (
	TimePrecisionMicros TimePrecision = iota
	TimePrecisionMillis
	TimePrecisionSeconds
)

type TypeKind

type TypeKind int

TypeKind represents the kind of a defmt parameter type.

const (
	TypeKindFormat         TypeKind = iota // {=?} or {}
	TypeKindBool                           // {=bool}
	TypeKindChar                           // {=char}
	TypeKindDebug                          // {=__internal_Debug}
	TypeKindDisplay                        // {=__internal_Display}
	TypeKindFormatSequence                 // {=__internal_FormatSequence}
	TypeKindF32                            // {=f32}
	TypeKindF64                            // {=f64}
	TypeKindFormatArray                    // {=[?; N]}
	TypeKindFormatSlice                    // {=[?]}
	TypeKindI8
	TypeKindI16
	TypeKindI32
	TypeKindI64
	TypeKindI128
	TypeKindIsize
	TypeKindIStr // {=istr}
	TypeKindStr  // {=str}
	TypeKindU8
	TypeKindU16
	TypeKindU32
	TypeKindU64
	TypeKindU128
	TypeKindUsize
	TypeKindU8Slice // {=[u8]}
	TypeKindU8Array // {=[u8; N]}
	TypeKindBitField
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL