Skip to content

Commit 6ebfb1d

Browse files
author
Zach Vonler
authored
Added --timestamp option to monitor command. (#2336)
1 parent 8b36949 commit 6ebfb1d

File tree

2 files changed

+84
-8
lines changed

2 files changed

+84
-8
lines changed

Diff for: internal/cli/monitor/monitor.go

+47-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"sort"
2626
"strings"
27+
"time"
2728

2829
"github.com/arduino/arduino-cli/commands/monitor"
2930
"github.com/arduino/arduino-cli/configuration"
@@ -44,12 +45,13 @@ var tr = i18n.Tr
4445
// NewCommand created a new `monitor` command
4546
func NewCommand() *cobra.Command {
4647
var (
47-
raw bool
48-
portArgs arguments.Port
49-
describe bool
50-
configs []string
51-
quiet bool
52-
fqbn arguments.Fqbn
48+
raw bool
49+
portArgs arguments.Port
50+
describe bool
51+
configs []string
52+
quiet bool
53+
timestamp bool
54+
fqbn arguments.Fqbn
5355
)
5456
monitorCommand := &cobra.Command{
5557
Use: "monitor",
@@ -59,20 +61,21 @@ func NewCommand() *cobra.Command {
5961
" " + os.Args[0] + " monitor -p /dev/ttyACM0\n" +
6062
" " + os.Args[0] + " monitor -p /dev/ttyACM0 --describe",
6163
Run: func(cmd *cobra.Command, args []string) {
62-
runMonitorCmd(&portArgs, &fqbn, configs, describe, quiet, raw)
64+
runMonitorCmd(&portArgs, &fqbn, configs, describe, timestamp, quiet, raw)
6365
},
6466
}
6567
portArgs.AddToCommand(monitorCommand)
6668
monitorCommand.Flags().BoolVar(&raw, "raw", false, tr("Set terminal in raw mode (unbuffered)."))
6769
monitorCommand.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port."))
6870
monitorCommand.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configure communication port settings. The format is <ID>=<value>[,<ID>=<value>]..."))
6971
monitorCommand.Flags().BoolVarP(&quiet, "quiet", "q", false, tr("Run in silent mode, show only monitor input and output."))
72+
monitorCommand.Flags().BoolVar(&timestamp, "timestamp", false, tr("Timestamp each incoming line."))
7073
fqbn.AddToCommand(monitorCommand)
7174
monitorCommand.MarkFlagRequired("port")
7275
return monitorCommand
7376
}
7477

75-
func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, quiet, raw bool) {
78+
func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, timestamp, quiet, raw bool) {
7679
instance := instance.CreateAndInit()
7780
logrus.Info("Executing `arduino-cli monitor`")
7881

@@ -160,6 +163,10 @@ func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []str
160163
feedback.FatalError(err, feedback.ErrGeneric)
161164
}
162165

166+
if timestamp {
167+
ttyOut = newTimeStampWriter(ttyOut)
168+
}
169+
163170
ctx, cancel := cleanup.InterruptableContext(context.Background())
164171
if raw {
165172
feedback.SetRawModeStdin()
@@ -241,3 +248,35 @@ func contains(s []string, searchterm string) bool {
241248
}
242249
return false
243250
}
251+
252+
type timeStampWriter struct {
253+
writer io.Writer
254+
sendTimeStampNext bool
255+
}
256+
257+
func newTimeStampWriter(writer io.Writer) *timeStampWriter {
258+
return &timeStampWriter{
259+
writer: writer,
260+
sendTimeStampNext: true,
261+
}
262+
}
263+
264+
func (t *timeStampWriter) Write(p []byte) (int, error) {
265+
written := 0
266+
for _, b := range p {
267+
if t.sendTimeStampNext {
268+
_, err := t.writer.Write([]byte(time.Now().Format("[2006-01-02 15:04:05] ")))
269+
if err != nil {
270+
return written, err
271+
}
272+
t.sendTimeStampNext = false
273+
}
274+
n, err := t.writer.Write([]byte{b})
275+
written += n
276+
if err != nil {
277+
return written, err
278+
}
279+
t.sendTimeStampNext = b == '\n'
280+
}
281+
return written, nil
282+
}

Diff for: internal/cli/monitor/monitor_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2023 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package monitor
17+
18+
import (
19+
"bytes"
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestTimeStampWriter(t *testing.T) {
26+
buf := &bytes.Buffer{}
27+
writer := newTimeStampWriter(buf)
28+
29+
writer.Write([]byte("foo"))
30+
// The first received bytes get a timestamp prepended
31+
require.Regexp(t, `^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] foo$`, buf)
32+
33+
buf.Reset()
34+
writer.Write([]byte("\nbar\n"))
35+
// A timestamp should be inserted before the first char of the next line
36+
require.Regexp(t, "^\n"+`\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] bar`+"\n$", buf)
37+
}

0 commit comments

Comments
 (0)