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

View File

@@ -1 +1 @@
v4.5.0.20240425
v4.5.0.202405061137

View File

@@ -351,7 +351,7 @@ func TestDevice_ListPackages(t *testing.T) {
t.Fatal(err)
}
t.Log(res)
installed, err := dev.IsPackagesInstalled("io.appium.uiautomator2.server")
installed := dev.IsPackagesInstalled("io.appium.uiautomator2.server")
if err != nil {
t.Fatal(err)
}

View File

@@ -1,6 +1,7 @@
package uixt
import (
"bufio"
"bytes"
"encoding/xml"
"fmt"
@@ -603,12 +604,6 @@ func (ad *adbDriver) IsHealthy() (healthy bool, err error) {
func (ad *adbDriver) StartCaptureLog(identifier ...string) (err error) {
log.Info().Msg("start adb log recording")
// clear logcat
if _, err = ad.adbClient.RunShellCommand("logcat", "-c"); err != nil {
return err
}
// start logcat
err = ad.logcat.CatchLogcat("iesqaMonitor:V")
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")
}
}()
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 {
@@ -659,7 +657,19 @@ func (ad *adbDriver) StopCaptureLog() (result interface{}, err error) {
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
}

View File

@@ -399,6 +399,8 @@ func getFreePort() (int, error) {
return l.Addr().(*net.TCPAddr).Port, nil
}
type LineCallback func(string)
type AdbLogcat struct {
serial string
// logBuffer *bytes.Buffer
@@ -406,7 +408,19 @@ type AdbLogcat struct {
stopping chan struct{}
done chan struct{}
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 {
@@ -415,6 +429,7 @@ func NewAdbLogcat(serial string) *AdbLogcat {
// logBuffer: new(bytes.Buffer),
stopping: 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 {
return err
}
l.reader = reader
if err = l.cmd.Start(); err != nil {
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() {
<-l.stopping
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"`
}
func ConvertPoints(reader io.Reader) (eps []ExportPoint) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
log.Info().Str("logcat content", line)
func ConvertPoints(lines []string) (eps []ExportPoint) {
log.Info().Msg("ConvertPoints")
for _, line := range lines {
if strings.Contains(line, "ext") {
idx := strings.Index(line, "{")
if idx == -1 {
@@ -519,6 +542,7 @@ func ConvertPoints(reader io.Reader) (eps []ExportPoint) {
log.Error().Msg("failed to parse point data")
continue
}
log.Info().Msg(line)
eps = append(eps, p)
}
}