Skip to content

Commit 8bdf446

Browse files
committed
Added --timestamp option to monitor command to fix arduino#2316.
1 parent 0e6133f commit 8bdf446

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-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+
}

0 commit comments

Comments
 (0)