Skip to content

Commit b29d0e8

Browse files
committed
feat(cli): add target timestamp delta columns
1 parent 46f94c9 commit b29d0e8

File tree

7 files changed

+537
-73
lines changed

7 files changed

+537
-73
lines changed

docs/TriceUserManual.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,169 @@ trice log -port FILEBUFFER -args myLogs.bin -ts32='"3:04PM")'
19841984

19851985
After the year 2106 the Trice tool needs a small modification to correctly compute the epoch time then. Probably I will not be alive anymore to do that then, but, hey, Trice is Open Source!
19861986

1987+
### 18.2. <a id='target-(time)stamp-delta-columns'></a>Target (Time)Stamp Delta Columns
1988+
1989+
`trice` can display target timestamps not only as absolute values, but also as deltas to the previous target timestamp of the same size. For that purpose the CLI provides three additional switches:
1990+
1991+
* `-ts0delta`
1992+
* `-ts16delta`
1993+
* `-ts32delta`
1994+
1995+
These switches are delta variants of `-ts0`, `-ts16`, and `-ts32`. All three default to `""`, which means disabled.
1996+
1997+
#### Purpose
1998+
1999+
The delta switches add a second, independent timestamp column. This makes it possible to show:
2000+
2001+
* only absolute timestamps
2002+
* only delta timestamps
2003+
* both absolute and delta timestamps side by side
2004+
* differently formatted absolute and delta columns
2005+
2006+
This is useful when absolute time is needed for long-term orientation, while delta time is needed for short-term timing analysis.
2007+
2008+
#### General Behavior
2009+
2010+
`-ts16delta` and `-ts32delta` behave like the corresponding absolute timestamp switches, except that they print the difference to the previous timestamp of the same type:
2011+
2012+
* `-ts16delta` prints `current ts16 - previous ts16`
2013+
* `-ts32delta` prints `current ts32 - previous ts32`
2014+
2015+
Wraparound is handled naturally:
2016+
2017+
* 16-bit deltas wrap modulo `2^16`
2018+
* 32-bit deltas wrap modulo `2^32`
2019+
2020+
If no previous timestamp of that type exists yet, the delta column shows `-`.
2021+
2022+
`-ts0delta` does not calculate a delta value. It exists only to generate a matching placeholder column for trices without target timestamps, so absolute and delta columns can stay aligned independently.
2023+
2024+
#### Column Order
2025+
2026+
When both absolute and delta timestamps are enabled, the output order is:
2027+
2028+
1. absolute timestamp column
2029+
2. delta timestamp column
2030+
3. message text
2031+
2032+
This applies independently for `ts0`, `ts16`, and `ts32`.
2033+
2034+
#### Independence from `-ts`
2035+
2036+
The general switch `-ts` still sets defaults only for:
2037+
2038+
* `-ts0`
2039+
* `-ts16`
2040+
* `-ts32`
2041+
2042+
It does not set defaults for:
2043+
2044+
* `-ts0delta`
2045+
* `-ts16delta`
2046+
* `-ts32delta`
2047+
2048+
Delta columns are therefore always explicit and opt-in.
2049+
2050+
#### Formatting Rules
2051+
2052+
The delta switches use the same general formatting logic as the corresponding absolute timestamp switches, but independently from them. Examples:
2053+
2054+
* `-ts16delta="ms"`
2055+
* `-ts32delta="time:%8d"`
2056+
* `-ts16delta="dt:%6d"`
2057+
2058+
This means the absolute column and the delta column can use different formats and widths.
2059+
2060+
For the first delta value, `trice` prints `-` instead of a number. If possible, the marker is aligned to the width implied by the format string.
2061+
2062+
#### Special Case: `-ts32 epoch`
2063+
2064+
`-ts32` supports epoch-based formatting for absolute 32-bit timestamps, for example:
2065+
2066+
```bash
2067+
-ts32=epoch
2068+
-ts32=epoch2006-01-02 15:04:05 UTC
2069+
```
2070+
2071+
This is intended only for absolute timestamps.
2072+
2073+
For `-ts32delta`, the input values are still treated as plain numeric 32-bit values, and the delta is computed from those raw values before any epoch formatting would apply. Therefore `-ts32delta` accepts numeric formats, not epoch formats.
2074+
2075+
Typical usage with epoch-based absolute timestamps is:
2076+
2077+
```bash
2078+
trice log -ts32=epoch -ts32delta="dt:%8d"
2079+
```
2080+
2081+
Here the absolute column shows human-readable UTC time, while the delta column shows the difference in seconds between consecutive 32-bit timestamps.
2082+
2083+
#### Automatic `-ts0delta` Placeholder
2084+
2085+
If `-ts0delta` is not passed explicitly, `trice` can derive it automatically.
2086+
2087+
This happens when:
2088+
2089+
* `-ts16delta` and/or `-ts32delta` are enabled, and
2090+
* their implied output widths match
2091+
2092+
In that case `trice` generates a blank `-ts0delta` placeholder of the same width.
2093+
2094+
If the implied widths do not match, no automatic `-ts0delta` is generated and it remains `""`.
2095+
2096+
This mechanism helps keep the delta column aligned for messages without target timestamps.
2097+
2098+
#### Typical Use Cases
2099+
2100+
Show only delta values instead of absolute 16-bit timestamps:
2101+
2102+
```bash
2103+
trice log -ts16="" -ts16delta="dt:%6d"
2104+
```
2105+
2106+
This suppresses absolute `ts16` output and shows only the delta to the previous 16-bit timestamp.
2107+
2108+
Show both absolute and delta 16-bit timestamps in separate columns:
2109+
2110+
```bash
2111+
trice log -ts16="t:%6d " -ts16delta="dt:%6d "
2112+
```
2113+
2114+
Show absolute 32-bit timestamps as UTC epoch time and the delta in seconds:
2115+
2116+
```bash
2117+
trice log -ts32=epoch -ts32delta="dt:%8d "
2118+
```
2119+
2120+
Show absolute timestamps for all messages, but add a delta column only for 32-bit timestamps:
2121+
2122+
```bash
2123+
trice log -ts0="time: " -ts16="time:%6d " -ts32=epoch -ts32delta="dt:%8d "
2124+
```
2125+
2126+
If alignment for messages without target timestamps should follow the delta column too, add `-ts0delta` explicitly:
2127+
2128+
```bash
2129+
trice log -ts0="time: " -ts0delta=" " -ts16="time:%6d " -ts32=epoch -ts32delta="dt:%8d "
2130+
```
2131+
2132+
Use only a delta column and keep no-stamp lines aligned:
2133+
2134+
```bash
2135+
trice log -ts0="" -ts16="" -ts32="" -ts16delta="dt:%6d "
2136+
```
2137+
2138+
If the delta width is unique, `trice` can derive `-ts0delta` automatically.
2139+
2140+
#### Summary
2141+
2142+
The `tsdelta` switches make timestamp display more flexible by separating:
2143+
2144+
* absolute time representation
2145+
* delta time representation
2146+
* alignment of messages without target timestamps
2147+
2148+
This allows `trice` output to be tailored for debugging, profiling, timing analysis, and mixed absolute/delta log views without changing the target-side encoding.
2149+
19872150
<p align="right">(<a href="#top">back to top</a>)</p>
19882151

19892152
## 19. <a id='binary-encoding'></a>Binary Encoding

internal/args/handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func Handler(w io.Writer, fSys *afero.Afero, args []string) error {
134134
decoder.ShowTargetStamp32Passed = isLogFlagPassed("ts32")
135135
decoder.ShowTargetStamp16Passed = isLogFlagPassed("ts16")
136136
decoder.ShowTargetStamp0Passed = isLogFlagPassed("ts0")
137+
decoder.ShowTargetStamp32DeltaPassed = isLogFlagPassed("ts32delta")
138+
decoder.ShowTargetStamp16DeltaPassed = isLogFlagPassed("ts16delta")
139+
decoder.ShowTargetStamp0DeltaPassed = isLogFlagPassed("ts0delta")
137140
w = do.DistributeArgs(w, fSys, LogfileName, Verbose)
138141
logLoop(w, fSys) // endless loop
139142
return nil

internal/args/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ This timestamp switch generates the timestamps on the PC only (reception time),
110110
fsScLog.StringVar(&decoder.TargetStamp32, "ts32", "ms", `32-bit Target stamp format string at start of each line, if 32-bit target stamps existent (configured). Choose between "µs" (or "us"), "ms", "epoch060102150405" like, use "" to suppress or use s.th. like "...%d...". If several trices form a log line only the timestamp of first trice ist displayed.`)
111111
fsScLog.StringVar(&decoder.TargetStamp16, "ts16", "ms", `16-bit Target stamp format string at start of each line, if 16-bit target stamps existent (configured). Choose between "µs" (or "us") and "ms", use "" to suppress or use s.th. like "...%d...". If several trices form a log line only the timestamp of first trice ist displayed.`)
112112
fsScLog.StringVar(&decoder.TargetStamp0, "ts0", translator.DefaultTargetStamp0, `Target stamp format string at start of each line, if no target stamps existent (configured). Use "" to suppress existing target timestamps. If several trices form a log line only the timestamp of first trice ist displayed.`)
113+
fsScLog.StringVar(&decoder.TargetStamp32Delta, "ts32delta", "", `32-bit Target stamp delta format string at start of each line, if 32-bit target stamps existent (configured). Shows the delta to the previous displayed 32-bit target stamp with wraparound support. Use "" to suppress. Use "µs" (or "us"), "ms", or a format string like "...%d...". "epoch..." is not supported for deltas.`)
114+
fsScLog.StringVar(&decoder.TargetStamp16Delta, "ts16delta", "", `16-bit Target stamp delta format string at start of each line, if 16-bit target stamps existent (configured). Shows the delta to the previous displayed 16-bit target stamp with wraparound support. Use "" to suppress. Use "µs" (or "us"), "ms", or a format string like "...%d...".`)
115+
fsScLog.StringVar(&decoder.TargetStamp0Delta, "ts0delta", "", `Target stamp delta placeholder format string at start of each line, if no target stamps existent (configured). Use it to align a separate delta column. If omitted, trice derives a blank placeholder automatically when ts16delta and/or ts32delta imply the same width.`)
113116
fsScLog.BoolVar(&decoder.DebugOut, "debug", false, "Show additional debug information")
114117
fsScLog.StringVar(&translator.TriceEndianness, "triceEndianness", "littleEndian", `Target endianness trice data stream. Option: "bigEndian".`)
115118
fsScLog.StringVar(&emitter.ColorPalette, "color", "default", colorInfo) // flag
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package args
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
"testing"
7+
8+
"github.com/spf13/afero"
9+
)
10+
11+
func TestLogHelpShowsDeltaTimestampFlags(t *testing.T) {
12+
FlagsInit()
13+
14+
var out bytes.Buffer
15+
fSys := &afero.Afero{Fs: afero.NewMemMapFs()}
16+
err := Handler(&out, fSys, []string{"trice", "help", "-log"})
17+
if err != nil {
18+
t.Fatalf("unexpected help error: %v", err)
19+
}
20+
21+
got := out.String()
22+
for _, snippet := range []string{
23+
"-ts0delta string",
24+
"-ts16delta string",
25+
"-ts32delta string",
26+
`"epoch..." is not supported for deltas`,
27+
} {
28+
if !strings.Contains(got, snippet) {
29+
t.Fatalf("help output misses %q\n%s", snippet, got)
30+
}
31+
}
32+
}

internal/decoder/decoder.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,16 @@ var (
121121
TargetStamp32 string // ShowTargetStamp32 is the format string for target timestamps.
122122
TargetStamp16 string // ShowTargetStamp16 is the format string for target timestamps.
123123
TargetStamp0 string // ShowTargetStamp0 is the format string for target timestamps.
124+
TargetStamp32Delta string // TargetStamp32Delta is the format string for 32-bit target timestamp deltas.
125+
TargetStamp16Delta string // TargetStamp16Delta is the format string for 16-bit target timestamp deltas.
126+
TargetStamp0Delta string // TargetStamp0Delta is the format string for delta column alignment without target timestamps.
124127
TargetTimeStampUnitPassed bool // TargetTimeStampUnitPassed is true when flag was TargetTimeStampUnit passed.
125128
ShowTargetStamp32Passed bool // ShowTargetStamp32Passed is true when flag was TargetTimeStamp32 passed.
126129
ShowTargetStamp16Passed bool // ShowTargetStamp16Passed is true when flag was TargetTimeStamp16 passed.
127130
ShowTargetStamp0Passed bool // ShowTargetStamp0Passed is true when flag was TargetTimeStamp0 passed.
131+
ShowTargetStamp32DeltaPassed bool // ShowTargetStamp32DeltaPassed is true when flag TargetStamp32Delta was passed.
132+
ShowTargetStamp16DeltaPassed bool // ShowTargetStamp16DeltaPassed is true when flag TargetStamp16Delta was passed.
133+
ShowTargetStamp0DeltaPassed bool // ShowTargetStamp0DeltaPassed is true when flag TargetStamp0Delta was passed.
128134
LocationInformationFormatString = LiFmtDefault // LocationInformationFormatString is the format string for target location: line number and file name.
129135
LiFmtDefaultLen int // Compute as var from LocationInformationFormatString.
130136
TargetLocationExists bool // TargetLocationExists is set in dependence of p.COBSModeDescriptor. (obsolete)

0 commit comments

Comments
 (0)