emitter

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package emitter formats decoded trice text lines and writes them either to a local display or to a remote RPC display server.

It combines line composition (timestamps/prefix/suffix), optional tag-based filtering/colorization, and output routing.

Package emitter emits the translated trice strings.

Example (NewColorDisplay)

Example_newColorDisplay demonstrates typical usage.

p := newColorDisplay(os.Stdout, "none")
l1 := []string{"msg:This is ", "the 1st ", "line"}
l2 := []string{"MSG:This is ", "the 2nd ", "line"}
p.WriteLine(l1)
p.WriteLine(l2)
Output:
This is the 1st line
MSG:This is the 2nd line
Example (NewLocalDisplay)

Example_newLocalDisplay demonstrates typical usage.

p := newLocalDisplay(os.Stdout)
l1 := []string{"This is ", "the 1st ", "line"}
l2 := []string{"This is ", "the 2nd ", "line"}
p.WriteLine(l1)
p.WriteLine(l2)
Output:
This is the 1st line
This is the 2nd line

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Verbose enables optional diagnostic output.
	Verbose bool

	// HostStamp configures line timestamps.
	// off/none = no timestamp
	// LOCmicro = local time with microseconds
	// UTCmicro = UTC time with microseconds
	// zero = fixed "2006-01-02_1504-05" timestamp (for tests)
	HostStamp string

	// Prefix is written before the first payload fragment of each line.
	Prefix string

	// Suffix is written when a line is completed. Usually empty.
	Suffix string

	// ColorPalette controls tag-prefix handling.
	// off = no color handling at all. Lower case color prefixes are not removed. Use with care.
	// none = no colors. Lower case color prefixes are removed.
	// default = color codes added (TODO: change to ANSI)
	ColorPalette string

	// IPAddr is the remote display server IP address.
	IPAddr string

	// IPPort is the remote display server TCP port.
	IPPort string

	// DisplayRemote sends lines to a remote RPC display server when true.
	DisplayRemote bool

	// TestTableMode avoids Prefix rewrites in New.
	TestTableMode bool

	// NextLine is set when a line is completed.
	// It is used by decoder table-mode output coordination.
	NextLine bool

	// Ban contains channel tags to suppress.
	Ban channelArrayFlag

	// Pick contains channel tags to display exclusively.
	Pick channelArrayFlag

	// UserLabel adds user-defined tag names at runtime.
	UserLabel ArrayFlag
)
View Source
var (
	// LogLevel is usable to suppress less important logs.
	LogLevel = "all"

	AllStatistics bool // Keep the complete statistics when Trice is closed.
	TagStatistics bool // Print the occured count for each Trice log when Trice is closed.
)
View Source
var Tags = []tag{

	{0, []string{"Fatal", "fatal", "FATAL"}, colorizeFATAL},
	{0, []string{"crit", "Critical", "critical", "CRITICAL", "Crit", "CRIT"}, colorizeCRITICAL},
	{0, []string{"em", "Emergency", "emergency", "EMERGENCY"}, colorizeEMERGENCY},
	{0, []string{"e", "Error", "err", "error", "E", "ERR", "ERROR"}, colorizeERROR},
	{0, []string{"w", "wrn", "Warning", "warning", "W", "WRN", "WARNING", "Warn", "warn", "WARN"}, colorizeWARNING},
	{0, []string{"att", "attention", "Attention", "ATT", "ATTENTION"}, colorizeATTENTION},
	{0, []string{"i", "inf", "info", "Info", "informal", "I", "INF", "INFO", "INFORMAL"}, colorizeINFO},
	{0, []string{"d", "db", "Debug", "dbg", "deb", "debug", "D", "DB", "DBG", "DEBUG"}, colorizeDEBUG},
	{0, []string{"tr", "Trace", "trace", "TRACE"}, colorizeTRACE},

	{0, []string{"tim", "time", "Time", "TIM", "TIME", "TIMESTAMP", "timestamp", "Timestamp"}, colorizeTIME},
	{0, []string{"m", "msg", "message", "M", "MSG", "MESSAGE", "OK"}, colorizeMESSAGE},
	{0, []string{"r", "rx", "rd", "read", "rd_", "RD", "RD_", "READ"}, colorizeREAD},
	{0, []string{"w", "tx", "wr", "write", "wr_", "WR", "WR_", "WRITE"}, colorizeWRITE},
	{0, []string{"rx", "receive", "RECEIVE", "Receive", "RX"}, colorizeRECEIVE},
	{0, []string{"tx", "transmit", "TRANSMIT", "Transmit", "TX"}, colorizeTRANSMIT},
	{0, []string{"dia", "diag", "Diag", "DIA", "DIAG"}, colorizeDIAG},
	{0, []string{"int", "isr", "ISR", "INT", "interrupt", "Interrupt", "INTERRUPT"}, colorizeINTERRUPT},
	{0, []string{"s", "sig", "signal", "S", "SIG", "SIGNAL"}, colorizeSIGNAL},
	{0, []string{"t", "tst", "test", "T", "TST", "TEST"}, colorizeTEST},

	{0, []string{"def", "Default", "DEFAULT", "default"}, colorizeDEFAULT},
	{0, []string{"note", "Notice", "NOTICE", "notice", "Note", "NOTE"}, colorizeNOTICE},
	{0, []string{"Alert", "alert", "ALERT"}, colorizeALERT},
	{0, []string{"Assert", "assert", "ASSERT"}, colorizeASSERT},
	{0, []string{"a", "Alarm", "alarm", "ALARM"}, colorizeALARM},
	{0, []string{"CYCLE_ERROR"}, colorizeCYCLE},
	{0, []string{"v", "Verbose", "verbose", "VERBOSE"}, colorizeVERBOSE},
	{0, []string{"cfg", "config"}, colorizeDEFAULT},
	{0, []string{"us", "µs", "uS", "µS", "uSec", "µSec", "uSEC", "µSEC", "MicroSec", "Microsecond", "Microseconds"}, colorizeTIME},
	{0, []string{"ms", "mS", "mSec", "mSEC", "MSEC", "MilliSec", "Millisecond", "Milliseconds"}, colorizeTIME},
	{0, []string{"s", "S", "Sec", "SEC", "SECOND", "SECONDS", "Second", "Seconds"}, colorizeTIME},
	{0, []string{"dt", "delta", "dT", "deltaTime", "delta-time"}, colorizeTIME},
}

Tags contains all usable trice Tags and their possible names.

Functions

func AddUserLabels added in v1.1.0

func AddUserLabels()

AddUserLabels appends user-defined labels to Tags once per process.

func BanOrPickFilter added in v0.31.0

func BanOrPickFilter(b []byte) (n int)

BanOrPickFilter returns len(b) when b passes filtering, otherwise 0. If Ban and Pick are nil nothing is filtered out. If Ban and Pick are both not nil this is a fatal error (os.Exit). If b starts with a known channel specifier existent in Ban 0, is returned. If b starts with a known channel specifier existent in Pick len of b, is returned.

func Colorize added in v0.72.4

func Colorize(s string) (r string)

colorize prefixes s with an ansi color code according to these conditions: If p.colorPalette is "off", do nothing. If p.colorPalette is "none" remove only lower case channel info "col:" If "COL:" is start of string add ANSI color code according to COL: If "col:" is start of string replace "col:" with ANSI color code according to col: Additionally, if global variable LogLevel is not the default "all", but found inside ColorChannels, logs with higher index positions are suppressed. As special case LogLevel == "off" does not output anything. Colorize applies global tag/palette rules to s.

func FindTagName added in v0.71.0

func FindTagName(name string) (tagName string, err error)

FindTagName maps any tag alias to its canonical name.

func PrintTagStatistics added in v0.72.4

func PrintTagStatistics(w io.Writer)

PrintTagStatistics shows the amount of occurred tag events.

func ScDisplayServer

func ScDisplayServer(w io.Writer) error

ScDisplayServer serves RPC display requests until the listener is closed.

func ScShutdownRemoteDisplayServer

func ScShutdownRemoteDisplayServer(w io.Writer, timeStamp int64, args ...string) error

ScShutdownRemoteDisplayServer connects to a client to send shutdown message to display server. 0==timeStamp -> no timestamp in shutdown message, use for tests. 1==timeStamp -> timestamp in shutdown message, use normally. It accepts 0 to 2 string arguments as args. More arguments are ignored. For not given parameters default values are taken. The parameters are in the following order. args[0] (ipa) is the IP address to be used to connect to the remote display. args[1] (ipp) is the IP port to be used to connect to the remote display.

func ShowAllColors added in v0.52.0

func ShowAllColors()

ShowAllColors prints all foreground/background style combinations.

func TagEvents added in v0.71.0

func TagEvents(ch string) int

TagEvents returns count of occurred channel events. If ch is unknown, the returned value is -1.

Types

type ArrayFlag added in v1.1.0

type ArrayFlag []string

ArrayFlag supports repeated CLI values.

func (*ArrayFlag) Set added in v1.1.0

func (i *ArrayFlag) Set(value string) error

Set implements flag.Value.

func (*ArrayFlag) String added in v1.1.0

func (i *ArrayFlag) String() string

String implements flag.Value.

type DisplayServer added in v0.51.1

type DisplayServer struct {
	Display colorDisplay
}

DisplayServer exposes RPC methods for remote line rendering.

func (*DisplayServer) ColorPalette added in v0.51.1

func (p *DisplayServer) ColorPalette(s []string, reply *int64) error

ColorPalette is the exported server function for color palette, if trice tool acts as display server. By declaring it as a Server struct method it is registered as RPC destination.

func (*DisplayServer) LogSetFlags added in v0.51.1

func (p *DisplayServer) LogSetFlags(f []int64, r *int64) error

LogSetFlags updates the process log package flags remotely.

func (*DisplayServer) Shutdown added in v0.51.1

func (p *DisplayServer) Shutdown(ts []int64, _ *int64) error

Shutdown emits a shutdown marker and closes the server listener.

func (*DisplayServer) WriteLine added in v0.51.1

func (p *DisplayServer) WriteLine(line []string, reply *int64) error

WriteLine is the exported server method for string display, if trice tool acts as display server. By declaring it as a Server struct method it is registered as RPC destination.

type LineWriter

type LineWriter interface {
	WriteLine([]string)
}

LineWriter is the common interface for output devices. The string slice `line` contains all string parts of one line including prefix and suffix. The last string part is without newline char and must be handled by the output device.

type TriceLineComposer

type TriceLineComposer struct {
	Line []string // line collector
	// contains filtered or unexported fields
}

TriceLineComposer collects all partial strings forming one line.

func New

func New(w io.Writer) *TriceLineComposer

New creates the emitter instance and returns a string writer to be used for emitting.

func (*TriceLineComposer) Write added in v0.18.4

func (p *TriceLineComposer) Write(b []byte) (n int, err error)

Write treats received buffer as a string.

func (*TriceLineComposer) WriteString

func (p *TriceLineComposer) WriteString(s string) (n int, err error)

WriteString implements the io.StringWriter interface. The triceLineComposer can use it. WriteString uses the internal line writer p.lw for writing out full lines. If s is empty, WriteString returns 0, nil. If p.line is empty, the actual timestamp and the prefix are added to p.line. If s is without newline it is added to p.line and WriteString returns. If s ends with newline it is added to p.line and also the suffix is added to p.line and pline is written to p.lw. If s contains several newlines it is split there and the substrings are handled accordingly. That means it writes internally a separate line for each substring (in s) ending with a newline.

Jump to

Keyboard shortcuts

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