Skip to content

Commit f8b3d6b

Browse files
author
Federico Fissore
committed
Flushing remaining output from BufferedUntilNewLineWriter once process has
ended. Also printing raw bytes instead of converting them into chars. Signed-off-by: Federico Fissore <[email protected]>
1 parent 5abb51e commit f8b3d6b

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

src/arduino.cc/builder/builder_utils/utils.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -319,17 +319,19 @@ func ExecRecipe(properties map[string]string, recipe string, removeUnsetProperti
319319
}
320320

321321
if echoOutput {
322-
printToStdOut := func(s string) {
323-
logger.UnformattedFprintln(os.Stdout, s)
322+
printToStdOut := func(data []byte) {
323+
logger.UnformattedWrite(os.Stdout, data)
324324
}
325325
stdout := &types.BufferedUntilNewLineWriter{PrintFunc: printToStdOut, Buffer: bytes.Buffer{}}
326+
defer stdout.Flush()
326327
command.Stdout = stdout
327328
}
328329

329-
printToStdErr := func(s string) {
330-
logger.UnformattedFprintln(os.Stderr, s)
330+
printToStdErr := func(data []byte) {
331+
logger.UnformattedWrite(os.Stderr, data)
331332
}
332333
stderr := &types.BufferedUntilNewLineWriter{PrintFunc: printToStdErr, Buffer: bytes.Buffer{}}
334+
defer stderr.Flush()
333335
command.Stderr = stderr
334336

335337
if echoOutput {

src/arduino.cc/builder/i18n/i18n.go

+17
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var PLACEHOLDER = regexp.MustCompile("{(\\d)}")
4646
type Logger interface {
4747
Fprintln(w io.Writer, level string, format string, a ...interface{})
4848
UnformattedFprintln(w io.Writer, s string)
49+
UnformattedWrite(w io.Writer, data []byte)
4950
Println(level string, format string, a ...interface{})
5051
Name() string
5152
}
@@ -56,6 +57,8 @@ func (s NoopLogger) Fprintln(w io.Writer, level string, format string, a ...inte
5657

5758
func (s NoopLogger) UnformattedFprintln(w io.Writer, str string) {}
5859

60+
func (s NoopLogger) UnformattedWrite(w io.Writer, data []byte) {}
61+
5962
func (s NoopLogger) Println(level string, format string, a ...interface{}) {}
6063

6164
func (s NoopLogger) Name() string {
@@ -76,6 +79,10 @@ func (s HumanLogger) Println(format string, level string, a ...interface{}) {
7679
s.Fprintln(os.Stdout, level, Format(format, a...))
7780
}
7881

82+
func (s HumanLogger) UnformattedWrite(w io.Writer, data []byte) {
83+
write(w, data)
84+
}
85+
7986
func (s HumanLogger) Name() string {
8087
return "human"
8188
}
@@ -98,6 +105,10 @@ func (s MachineLogger) Name() string {
98105
return "machine"
99106
}
100107

108+
func (s MachineLogger) UnformattedWrite(w io.Writer, data []byte) {
109+
write(w, data)
110+
}
111+
101112
func printMachineFormattedLogLine(w io.Writer, level string, format string, a []interface{}) {
102113
a = append([]interface{}(nil), a...)
103114
for idx, value := range a {
@@ -117,6 +128,12 @@ func fprintln(w io.Writer, s string) {
117128
fmt.Fprintln(w, s)
118129
}
119130

131+
func write(w io.Writer, data []byte) {
132+
lock.Lock()
133+
defer lock.Unlock()
134+
w.Write(data)
135+
}
136+
120137
func fprintf(w io.Writer, format string, a ...interface{}) {
121138
lock.Lock()
122139
defer lock.Unlock()

src/arduino.cc/builder/test/builder_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ package test
3232
import (
3333
"arduino.cc/builder"
3434
"arduino.cc/builder/constants"
35+
"arduino.cc/builder/i18n"
3536
"github.com/stretchr/testify/require"
3637
"os"
3738
"os/exec"
@@ -92,6 +93,7 @@ func TestBuilderBridge(t *testing.T) {
9293
context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"}
9394
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
9495
context[constants.CTX_VERBOSE] = true
96+
context[constants.CTX_LOGGER] = i18n.MachineLogger{}
9597

9698
command := builder.Builder{}
9799
err := command.Run(context)
@@ -127,6 +129,8 @@ func TestBuilderSketchWithConfig(t *testing.T) {
127129
context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"}
128130
context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"}
129131
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
132+
context[constants.CTX_VERBOSE] = true
133+
context[constants.CTX_LOGGER] = i18n.NoopLogger{}
130134

131135
command := builder.Builder{}
132136
err := command.Run(context)

src/arduino.cc/builder/types/accessories.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type BufferedUntilNewLineWriter struct {
8686
lock sync.Mutex
8787
}
8888

89-
type PrintFunc func(string)
89+
type PrintFunc func([]byte)
9090

9191
func (w *BufferedUntilNewLineWriter) Write(p []byte) (n int, err error) {
9292
w.lock.Lock()
@@ -98,10 +98,22 @@ func (w *BufferedUntilNewLineWriter) Write(p []byte) (n int, err error) {
9898
}
9999

100100
bytesUntilNewLine, err := w.Buffer.ReadBytes('\n')
101-
if err != nil && len(bytesUntilNewLine) > 0 {
102-
bytesUntilNewLine = bytesUntilNewLine[:len(bytesUntilNewLine)-1]
103-
w.PrintFunc(string(bytesUntilNewLine))
101+
if err == nil {
102+
w.PrintFunc(bytesUntilNewLine)
104103
}
105104

106105
return writtenToBuffer, err
107106
}
107+
108+
func (w *BufferedUntilNewLineWriter) Flush() {
109+
w.lock.Lock()
110+
defer w.lock.Unlock()
111+
112+
remainingBytes := w.Buffer.Bytes()
113+
if len(remainingBytes) > 0 {
114+
if remainingBytes[len(remainingBytes)-1] != '\n' {
115+
remainingBytes = append(remainingBytes, '\n')
116+
}
117+
w.PrintFunc(remainingBytes)
118+
}
119+
}

0 commit comments

Comments
 (0)