Skip to content

Commit fb30598

Browse files
committed
Run asynchronously and format output nicer.
1 parent 7748fb8 commit fb30598

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

cli.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"sync"
1111
"syscall"
12+
"time"
1213
"unsafe"
1314
)
1415

@@ -17,7 +18,7 @@ const devCli = "/dev/cli"
1718
type lineCallback func(line string, cmd *exec.Cmd) error
1819

1920
func streamLog(cb lineCallback) error {
20-
cmd := exec.Command("logcat", "-bmain", "-s", "-T1", "MTK_KL")
21+
cmd := exec.Command("logcat", "-bmain", "-s", "MTK_KL")
2122
pipe, err := cmd.StdoutPipe()
2223
if err != nil {
2324
return fmt.Errorf("can't get a pipe for logcat child: %v", err)
@@ -107,8 +108,9 @@ func sendStringAsChars(f *os.File, cmd string) error {
107108

108109
func streamCommand(f *os.File, command string) error {
109110
const sentinelKey = "cl1w4sh3re"
110-
const sentinelValStart = "sTaRT"
111-
const sentinelValEnd = "TheenD"
111+
now := time.Now().UnixNano()
112+
sentinelValStart := fmt.Sprintf("sTaRT.%d", now)
113+
sentinelValEnd := fmt.Sprintf("enD.%d", now)
112114
putSentinel := func(value string) error {
113115
for _, c := range []string{fmt.Sprintf("alias %s %s", sentinelKey, value), "alias"} {
114116
if err := sendString(f, c); err != nil {
@@ -134,18 +136,24 @@ func streamCommand(f *os.File, command string) error {
134136
if state == stateWaitStart {
135137
if hasSentinel(line, sentinelValStart) {
136138
state = stateWaitEnd
137-
for _, err := range []error{sendString(f, command), putSentinel(sentinelValEnd)} {
138-
if err != nil {
139-
return fmt.Errorf("Error while sending: %v", err)
139+
go func() { // Need to run asynchronously; the ioctl() blocks while the handler runs.
140+
for _, err := range []error{sendString(f, command), putSentinel(sentinelValEnd)} {
141+
if err != nil {
142+
log.Fatalf("Error while sending: %v", err)
143+
}
140144
}
141-
}
145+
}()
142146
}
143147
} else if state == stateWaitEnd {
144148
if hasSentinel(line, sentinelValEnd) {
145149
cmd.Process.Kill()
146150
state = stateDone
147151
} else {
148-
log.Print(line)
152+
if idx := strings.Index(line, "] "); idx > 0 {
153+
line = line[idx+2:]
154+
}
155+
line = strings.Replace(line, "\r", "", -1)
156+
fmt.Println(line)
149157
}
150158
}
151159

0 commit comments

Comments
 (0)