Skip to content

Commit c7f2c28

Browse files
committed
fix:修复日志tab栏以及日志读取
1 parent a7db4ac commit c7f2c28

File tree

8 files changed

+126
-174
lines changed

8 files changed

+126
-174
lines changed
19.6 MB
Binary file not shown.

jniLibs/README.md

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -69,70 +69,3 @@ cp ../ecs/goecs-linux-amd64 jniLibs/x86_64/libgoecs.so && \
6969
chmod 755 jniLibs/*/libgoecs.so && \
7070
ls -lh jniLibs/*/libgoecs.so
7171
```
72-
73-
## 测试验证
74-
75-
### 1. 验证 APK 包含 .so 文件
76-
77-
```bash
78-
unzip -l your-app.apk | grep "lib/"
79-
# 应该看到:
80-
# lib/arm64-v8a/libgoecs.so
81-
# lib/x86_64/libgoecs.so
82-
```
83-
84-
### 2. 在设备上验证安装位置
85-
86-
```bash
87-
# 安装 APK
88-
adb install your-app.apk
89-
90-
# 查看安装位置
91-
adb shell pm path com.oneclickvirt.goecs
92-
93-
# 查看 native library 目录
94-
adb shell ls -l /data/app/com.oneclickvirt.goecs-*/lib/arm64/
95-
96-
# 测试执行
97-
adb shell
98-
run-as com.oneclickvirt.goecs
99-
cd /data/app/com.oneclickvirt.goecs-*/lib/arm64/
100-
./libgoecs.so --help
101-
```
102-
103-
### 3. 查看应用日志
104-
105-
```bash
106-
# 实时查看日志
107-
adb logcat | grep -E "(goecs|oneclickvirt)"
108-
109-
# 只看错误
110-
adb logcat *:E | grep goecs
111-
```
112-
113-
## 故障排除
114-
115-
如果看到 "fork/exec" 错误:
116-
117-
1. **检查文件是否存在**
118-
```bash
119-
adb shell run-as com.oneclickvirt.goecs find /data/app -name "libgoecs.so"
120-
```
121-
122-
2. **检查编译架构是否匹配设备**
123-
```bash
124-
adb shell getprop ro.product.cpu.abi
125-
# arm64-v8a -> 需要 jniLibs/arm64-v8a/libgoecs.so
126-
# x86_64 -> 需要 jniLibs/x86_64/libgoecs.so
127-
```
128-
129-
3. **验证文件不是空的**
130-
```bash
131-
ls -lh jniLibs/*/libgoecs.so
132-
# 文件应该有合理的大小(几MB到几十MB)
133-
```
134-
135-
4. **检查应用日志中的详细错误信息**
136-
- embedding 代码会输出所有检查过的路径
137-
- 查看 logcat 获取详细信息
138-

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func runGUIMode() {
4242
myApp := app.NewWithID("com.oneclickvirt.goecs")
4343
myApp.Settings().SetTheme(&ui.CustomTheme{})
4444

45+
// 设置为浅色主题
46+
myApp.Settings().SetTheme(&ui.CustomTheme{})
47+
4548
testUI := ui.NewTestUI(myApp)
4649
testUI.Window.ShowAndRun()
4750
}

ui/theme.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ type CustomTheme struct{}
1212
var _ fyne.Theme = (*CustomTheme)(nil)
1313

1414
func (m *CustomTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
15-
return theme.DefaultTheme().Color(name, variant)
15+
// 强制使用浅色主题
16+
return theme.DefaultTheme().Color(name, theme.VariantLight)
1617
}
1718

1819
func (m *CustomTheme) Icon(name fyne.ThemeIconName) fyne.Resource {

ui/ui_actions.go

Lines changed: 111 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package ui
22

33
import (
44
"context"
5+
"fmt"
56
"os"
6-
"path/filepath"
7-
"strings"
8-
"time"
97

108
"fyne.io/fyne/v2"
119
"fyne.io/fyne/v2/container"
@@ -136,13 +134,6 @@ func (ui *TestUI) startTests() {
136134
ui.ProgressBar.Show()
137135
ui.StatusLabel.SetText("测试运行中...")
138136

139-
// 如果启用了日志,显示日志标签页
140-
if ui.LogCheck != nil && ui.LogCheck.Checked {
141-
ui.showLogTab()
142-
} else {
143-
ui.hideLogTab()
144-
}
145-
146137
// 清空终端输出
147138
if ui.Terminal != nil {
148139
ui.Terminal.Clear()
@@ -204,119 +195,162 @@ func (ui *TestUI) exportResults() {
204195
}, ui.Window)
205196
}
206197

207-
// showLogTab 显示日志标签页
208-
func (ui *TestUI) showLogTab() {
209-
// 如果日志标签页还不存在,创建它
210-
if ui.LogTab == nil {
211-
ui.LogTab = ui.createLogTab()
212-
logTabItem := container.NewTabItem("日志查看", ui.LogTab)
213-
ui.MainTabs.Append(logTabItem)
198+
// onLogCheckChanged 当日志复选框状态改变时调用
199+
func (ui *TestUI) onLogCheckChanged(checked bool) {
200+
if checked {
201+
// 勾选时添加日志标签页
202+
ui.addLogTab()
203+
} else {
204+
// 取消勾选时移除日志标签页
205+
ui.removeLogTab()
214206
}
215-
216-
// 切换到日志标签页
217-
ui.MainTabs.SelectIndex(2) // 0=配置, 1=结果, 2=日志
218207
}
219208

220-
// hideLogTab 隐藏日志标签页
221-
func (ui *TestUI) hideLogTab() {
222-
// 如果有日志标签页,移除它
223-
if ui.LogTab != nil && len(ui.MainTabs.Items) > 2 {
224-
ui.MainTabs.Remove(ui.MainTabs.Items[2])
225-
ui.LogTab = nil
226-
ui.LogViewer = nil
209+
// addLogTab 添加日志标签页
210+
func (ui *TestUI) addLogTab() {
211+
// 如果日志标签页已存在,不重复添加
212+
if ui.LogTab != nil {
213+
return
227214
}
228-
}
229215

230-
// createLogTab 创建日志查看标签页
231-
func (ui *TestUI) createLogTab() *fyne.Container {
232216
// 创建日志查看器
233217
ui.LogViewer = widget.NewMultiLineEntry()
218+
ui.LogViewer.SetPlaceHolder("日志内容将在测试运行时显示...")
234219
ui.LogViewer.Wrapping = fyne.TextWrapWord
235-
ui.LogViewer.SetText("日志文件内容将在这里显示...")
220+
ui.LogViewer.Disable() // 只读
236221

237-
// 刷新按钮
238-
refreshButton := widget.NewButton("刷新日志", ui.refreshLog)
222+
// 刷新日志按钮
223+
refreshButton := widget.NewButton("刷新日志", func() {
224+
ui.refreshLogFromFile()
225+
})
239226

240-
// 清空按钮
241-
clearLogButton := widget.NewButton("清空显示", func() {
242-
if ui.LogViewer != nil {
243-
ui.LogViewer.SetText("")
244-
}
227+
// 清空日志按钮
228+
clearLogButton := widget.NewButton("清空日志", func() {
229+
ui.LogContent = ""
230+
ui.LogViewer.SetText("")
245231
})
246232

247-
topBar := container.NewHBox(
233+
// 导出日志按钮
234+
exportLogButton := widget.NewButton("导出日志", ui.exportLogContent)
235+
236+
// 按钮栏
237+
buttonBar := container.NewHBox(
248238
refreshButton,
249239
clearLogButton,
240+
exportLogButton,
250241
)
251242

243+
// 日志内容区域
252244
logScroll := container.NewScroll(ui.LogViewer)
253245

254-
return container.NewBorder(
255-
topBar, // Top: 操作按钮
246+
// 组合布局
247+
logContent := container.NewBorder(
248+
buttonBar, // Top: 按钮栏
256249
nil, // Bottom
257250
nil, // Left
258251
nil, // Right
259252
logScroll, // Center: 日志内容
260253
)
254+
255+
// 创建并添加日志标签页
256+
ui.LogTab = container.NewTabItem("日志", logContent)
257+
ui.MainTabs.Append(ui.LogTab)
258+
259+
// 初始化日志内容
260+
ui.LogContent = ""
261261
}
262262

263-
// refreshLog 刷新日志内容
264-
func (ui *TestUI) refreshLog() {
263+
// removeLogTab 移除日志标签页
264+
func (ui *TestUI) removeLogTab() {
265+
if ui.LogTab == nil {
266+
return
267+
}
268+
269+
// 从标签页容器中移除
270+
ui.MainTabs.Remove(ui.LogTab)
271+
ui.LogTab = nil
272+
ui.LogViewer = nil
273+
ui.LogContent = ""
274+
}
275+
276+
// refreshLogContent 刷新日志内容
277+
func (ui *TestUI) refreshLogContent() {
265278
if ui.LogViewer == nil {
266279
return
267280
}
268281

269-
// 获取当前目录
270-
currentDir, err := os.Getwd()
271-
if err != nil {
272-
ui.LogViewer.SetText("错误: 无法获取当前目录\n" + err.Error())
282+
// 显示存储的日志内容
283+
if ui.LogContent != "" {
284+
ui.LogViewer.SetText(ui.LogContent)
285+
} else {
286+
ui.LogViewer.SetText("暂无日志内容\n\n日志将在测试运行时自动更新。")
287+
}
288+
}
289+
290+
// refreshLogFromFile 从 ecs.log 文件读取日志内容
291+
func (ui *TestUI) refreshLogFromFile() {
292+
if ui.LogViewer == nil {
273293
return
274294
}
275295

276-
// 查找所有 .log 文件
277-
logFiles, err := filepath.Glob(filepath.Join(currentDir, "*.log"))
296+
// ecs.log 文件应该在当前工作目录下
297+
logFilePath := "ecs.log"
298+
299+
// 尝试读取日志文件
300+
content, err := os.ReadFile(logFilePath)
278301
if err != nil {
279-
ui.LogViewer.SetText("错误: 无法搜索日志文件\n" + err.Error())
302+
// 如果文件不存在或无法读取,显示错误信息
303+
if os.IsNotExist(err) {
304+
ui.LogViewer.SetText("日志文件 ecs.log 不存在\n\n可能测试未生成日志文件,或文件已被删除。")
305+
} else {
306+
ui.LogViewer.SetText(fmt.Sprintf("无法读取日志文件: %v", err))
307+
}
280308
return
281309
}
282310

283-
if len(logFiles) == 0 {
284-
ui.LogViewer.SetText("当前目录下没有找到 .log 文件\n\n请确保已启用日志记录并运行测试。")
311+
// 更新日志内容
312+
ui.LogContent = string(content)
313+
ui.LogViewer.SetText(ui.LogContent)
314+
}
315+
316+
// exportLogContent 导出日志内容
317+
func (ui *TestUI) exportLogContent() {
318+
if ui.LogViewer == nil || ui.LogViewer.Text == "" {
319+
dialog.ShowInformation("提示", "没有可导出的日志内容", ui.Window)
285320
return
286321
}
287322

288-
// 找到最新的日志文件
289-
var latestLog string
290-
var latestTime time.Time
291-
292-
for _, logFile := range logFiles {
293-
info, err := os.Stat(logFile)
323+
// 使用文件保存对话框
324+
dialog.ShowFileSave(func(writer fyne.URIWriteCloser, err error) {
294325
if err != nil {
295-
continue
326+
dialog.ShowError(err, ui.Window)
327+
return
296328
}
297-
if latestLog == "" || info.ModTime().After(latestTime) {
298-
latestLog = logFile
299-
latestTime = info.ModTime()
329+
if writer == nil {
330+
return
300331
}
301-
}
332+
defer writer.Close()
302333

303-
if latestLog == "" {
304-
ui.LogViewer.SetText("没有找到有效的日志文件")
305-
return
306-
}
334+
// 写入日志内容
335+
_, err = writer.Write([]byte(ui.LogViewer.Text))
336+
if err != nil {
337+
dialog.ShowError(err, ui.Window)
338+
return
339+
}
307340

308-
// 读取日志文件内容
309-
content, err := os.ReadFile(latestLog)
310-
if err != nil {
311-
ui.LogViewer.SetText("错误: 无法读取日志文件 " + latestLog + "\n" + err.Error())
341+
dialog.ShowInformation("成功", "日志已成功导出", ui.Window)
342+
}, ui.Window)
343+
}
344+
345+
// AppendLog 向日志内容追加文本
346+
func (ui *TestUI) AppendLog(text string) {
347+
if !ui.LogCheck.Checked || ui.LogViewer == nil {
312348
return
313349
}
314350

315-
// 显示日志内容
316-
logContent := "日志文件: " + filepath.Base(latestLog) + "\n"
317-
logContent += "修改时间: " + latestTime.Format("2006-01-02 15:04:05") + "\n"
318-
logContent += strings.Repeat("=", 60) + "\n\n"
319-
logContent += string(content)
351+
ui.Mu.Lock()
352+
defer ui.Mu.Unlock()
320353

321-
ui.LogViewer.SetText(logContent)
354+
ui.LogContent += text
355+
ui.LogViewer.SetText(ui.LogContent)
322356
}

0 commit comments

Comments
 (0)