9
9
"strings"
10
10
"sync"
11
11
"syscall"
12
+ "time"
12
13
"unsafe"
13
14
)
14
15
@@ -17,7 +18,7 @@ const devCli = "/dev/cli"
17
18
type lineCallback func (line string , cmd * exec.Cmd ) error
18
19
19
20
func streamLog (cb lineCallback ) error {
20
- cmd := exec .Command ("logcat" , "-bmain" , "-s" , "-T1" , " MTK_KL" )
21
+ cmd := exec .Command ("logcat" , "-bmain" , "-s" , "MTK_KL" )
21
22
pipe , err := cmd .StdoutPipe ()
22
23
if err != nil {
23
24
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 {
107
108
108
109
func streamCommand (f * os.File , command string ) error {
109
110
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 )
112
114
putSentinel := func (value string ) error {
113
115
for _ , c := range []string {fmt .Sprintf ("alias %s %s" , sentinelKey , value ), "alias" } {
114
116
if err := sendString (f , c ); err != nil {
@@ -134,18 +136,24 @@ func streamCommand(f *os.File, command string) error {
134
136
if state == stateWaitStart {
135
137
if hasSentinel (line , sentinelValStart ) {
136
138
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
+ }
140
144
}
141
- }
145
+ }()
142
146
}
143
147
} else if state == stateWaitEnd {
144
148
if hasSentinel (line , sentinelValEnd ) {
145
149
cmd .Process .Kill ()
146
150
state = stateDone
147
151
} 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 )
149
157
}
150
158
}
151
159
0 commit comments