fix: catch logcat by callback

This commit is contained in:
余泓铮
2024-05-06 19:44:59 +08:00
parent 5ccc8d00aa
commit e903d2be8e
4 changed files with 51 additions and 17 deletions
+1 -1
View File
@@ -1 +1 @@
v4.5.0.20240425 v4.5.0.202405061137
+1 -1
View File
@@ -351,7 +351,7 @@ func TestDevice_ListPackages(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
t.Log(res) t.Log(res)
installed, err := dev.IsPackagesInstalled("io.appium.uiautomator2.server") installed := dev.IsPackagesInstalled("io.appium.uiautomator2.server")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
+18 -8
View File
@@ -1,6 +1,7 @@
package uixt package uixt
import ( import (
"bufio"
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
@@ -603,12 +604,6 @@ func (ad *adbDriver) IsHealthy() (healthy bool, err error) {
func (ad *adbDriver) StartCaptureLog(identifier ...string) (err error) { func (ad *adbDriver) StartCaptureLog(identifier ...string) (err error) {
log.Info().Msg("start adb log recording") log.Info().Msg("start adb log recording")
// clear logcat
if _, err = ad.adbClient.RunShellCommand("logcat", "-c"); err != nil {
return err
}
// start logcat // start logcat
err = ad.logcat.CatchLogcat("iesqaMonitor:V") err = ad.logcat.CatchLogcat("iesqaMonitor:V")
if err != nil { if err != nil {
@@ -627,7 +622,10 @@ func (ad *adbDriver) StopCaptureLog() (result interface{}, err error) {
log.Error().Err(err).Msg("failed to get adb log recording") log.Error().Err(err).Msg("failed to get adb log recording")
} }
}() }()
pointRes := ConvertPoints(ad.logcat.reader) if err != nil {
log.Error().Err(err).Msg("failed to close adb log writer")
}
pointRes := ConvertPoints(ad.logcat.logs)
// 没有解析到打点日志,走兜底逻辑 // 没有解析到打点日志,走兜底逻辑
if len(pointRes) == 0 { if len(pointRes) == 0 {
@@ -659,7 +657,19 @@ func (ad *adbDriver) StopCaptureLog() (result interface{}, err error) {
return pointRes, nil return pointRes, nil
} }
pointRes = ConvertPoints(reader) var lines []string // 创建一个空的字符串数组来存储文件的每一行
// 使用 bufio.NewScanner 读取文件
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
lines = append(lines, scanner.Text()) // 将每行文本添加到字符串数组
}
if err := scanner.Err(); err != nil {
return pointRes, nil
}
pointRes = ConvertPoints(lines)
} }
return pointRes, nil return pointRes, nil
} }
+31 -7
View File
@@ -399,6 +399,8 @@ func getFreePort() (int, error) {
return l.Addr().(*net.TCPAddr).Port, nil return l.Addr().(*net.TCPAddr).Port, nil
} }
type LineCallback func(string)
type AdbLogcat struct { type AdbLogcat struct {
serial string serial string
// logBuffer *bytes.Buffer // logBuffer *bytes.Buffer
@@ -406,7 +408,19 @@ type AdbLogcat struct {
stopping chan struct{} stopping chan struct{}
done chan struct{} done chan struct{}
cmd *exec.Cmd cmd *exec.Cmd
reader io.Reader callback LineCallback
logs []string
}
func NewAdbLogcatWithCallback(serial string, callback LineCallback) *AdbLogcat {
return &AdbLogcat{
serial: serial,
// logBuffer: new(bytes.Buffer),
stopping: make(chan struct{}),
done: make(chan struct{}),
callback: callback,
logs: make([]string, 0),
}
} }
func NewAdbLogcat(serial string) *AdbLogcat { func NewAdbLogcat(serial string) *AdbLogcat {
@@ -415,6 +429,7 @@ func NewAdbLogcat(serial string) *AdbLogcat {
// logBuffer: new(bytes.Buffer), // logBuffer: new(bytes.Buffer),
stopping: make(chan struct{}), stopping: make(chan struct{}),
done: make(chan struct{}), done: make(chan struct{}),
logs: make([]string, 0),
} }
} }
@@ -477,10 +492,20 @@ func (l *AdbLogcat) CatchLogcat(filter string) (err error) {
if err != nil { if err != nil {
return err return err
} }
l.reader = reader
if err = l.cmd.Start(); err != nil { if err = l.cmd.Start(); err != nil {
return return
} }
go func() {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
if l.callback != nil {
l.callback(line) // Process each line with callback
} else {
l.logs = append(l.logs, line) // Store line if no callback
}
}
}()
go func() { go func() {
<-l.stopping <-l.stopping
if e := myexec.KillProcessesByGpid(l.cmd); e != nil { if e := myexec.KillProcessesByGpid(l.cmd); e != nil {
@@ -502,11 +527,9 @@ type ExportPoint struct {
RunTime int `json:"run_time,omitempty" yaml:"run_time,omitempty"` RunTime int `json:"run_time,omitempty" yaml:"run_time,omitempty"`
} }
func ConvertPoints(reader io.Reader) (eps []ExportPoint) { func ConvertPoints(lines []string) (eps []ExportPoint) {
scanner := bufio.NewScanner(reader) log.Info().Msg("ConvertPoints")
for scanner.Scan() { for _, line := range lines {
line := scanner.Text()
log.Info().Str("logcat content", line)
if strings.Contains(line, "ext") { if strings.Contains(line, "ext") {
idx := strings.Index(line, "{") idx := strings.Index(line, "{")
if idx == -1 { if idx == -1 {
@@ -519,6 +542,7 @@ func ConvertPoints(reader io.Reader) (eps []ExportPoint) {
log.Error().Msg("failed to parse point data") log.Error().Msg("failed to parse point data")
continue continue
} }
log.Info().Msg(line)
eps = append(eps, p) eps = append(eps, p)
} }
} }