| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- import Felgo
- import QtQuick
- import QtQuick.Controls
- import "../controls"
- import "../common"
- /**
- * 日志查看页(系统设置 -> 查看日志)
- *
- * 需求:
- * 1) 两类日志:App 运行日志(run_log*.txt)、MCU 串口日志(mcu_console*.txt);
- * 2) tail -f 实时刷新;
- * 3) 打开(选择)某个日志文件查看内容,包括轮转出来的历史文件;
- * 4) 选择日志文件上传(FTP,与图片同一套凭据/远端目录)。
- *
- * 数据来源全部是 C++ 侧 LogViewer(上下文属性名 logViewer),本页只做展示与交互。
- * 读写全部异步(子线程读文件,主线程只渲染),所以调用都是"发请求 + 收信号":
- * listLogs(kind) 同步枚举文件(很快,只列目录)
- * openLog(path, bytes) 打开文件尾部,结果 → logLoaded(path, text)
- * requestPoll(path) tail -f 一轮,结果 → logAppended(path, chunk)
- * resetTail(path) / uploadLog(path) 清偏移 / 上传(结果 → uploadFinished)
- *
- * 为什么不在 QML 里直接读文件:
- * QML 的 XMLHttpRequest 读不了 file://,也没有"从某个偏移量继续读"的能力,
- * 没法做 tail -f;日志还在被 spdlog 写,必须按行边界切分,否则会看到半行中文乱码。
- * 这些都在 LogViewer 里做掉了。
- *
- * 性能要点(日志文件 10MB 时曾出现界面卡死):
- * 1) 读文件在子线程,主线程不阻塞,加载期间显示 BusyIndicator;
- * 2) 只读尾部 64KB,不做全量读取;
- * 3) 追加用 logArea.append(),不整体重设 text(否则每次刷新都要重新排版十几万字符);
- * 4) 界面上最多保留 12 万字符,超出丢弃最旧的内容。
- */
- AppPage {
- id: page
- title: qsTr("日志查看")
- backgroundColor: app.theme_backgroundColor
- // 本页是 ProfilePage 用 navigationStack.push() 压入的第二层页面,
- // 必须保留导航栏,否则没有返回入口(与 PermissionPage 一致)
- navigationBarHidden: false
- // ---------------- 状态 ----------------
- property int logKind: 0 // 0=App 运行日志 1=MCU 串口日志
- property var fileList: [] // [{name,path,size,sizeText,time,isCurrent}]
- property string currentPath: "" // 当前打开的日志文件
- property string currentName: ""
- property bool follow: false // tail -f 开关
- property string logText: "" // 右侧文本内容
- property bool logLoading: false // 正在读文件(显示加载动画)
- property bool uploading: false
- property real uploadPercent: 0
- property string statusText: ""
- // 界面里最多保留的字符数:TextArea 每次改 text 都要重新排版整段内容,
- // 文本越长越慢(几十万字符时明显卡顿),这里压到 12 万字符
- // 更早的内容请看轮转出来的历史文件(run_log.1.txt …)
- readonly property int maxTextChars: 120000
- // 首次打开只读文件尾部这么多字节
- readonly property int loadBytes: 64 * 1024
- // ---------------- 方法 ----------------
- function kindName() {
- return logKind === 0 ? "App 运行日志" : "MCU 串口日志"
- }
- // 重新枚举某类日志的所有文件(含 run_log.1.txt 这类轮转文件)
- function reloadFiles() {
- if (!logViewer) return
- fileList = logViewer.listLogs(logKind)
- dirText.text = "日志目录: " + logViewer.logDir(logKind)
- if (fileList.length === 0)
- page.statusText = "该目录下还没有日志文件"
- }
- // 打开文件:读文件尾部(异步,读到后由 onLogLoaded 填内容)
- // 一定要异步:10MB 日志在 UI 线程读+渲染会把界面整段卡死,
- // 现在主线程只发请求,同时把加载动画显示出来
- function openFile(path, name, keepFollow) {
- if (!logViewer || !path) return
- page.currentPath = path
- page.currentName = name
- if (!keepFollow) { // "重新加载"要保持实时刷新开关不变
- page.follow = false
- followSwitch.checked = false
- }
- page.logText = ""
- logArea.text = "" // 不靠 text 绑定(见 appendText 说明)
- page.logLoading = true
- page.statusText = "正在加载: " + name
- logViewer.resetTail(path)
- logViewer.openLog(path, page.loadBytes)
- }
- // 追加文本(tail -f 每轮调用)
- // 关键:用 logArea.append() 只排版新增的那几行,不要整体重设 text
- // (TextArea 每次重设 text 都会把整段内容重新排版,十几万字符时肉眼可见地卡)
- function appendText(s) {
- if (!s) return
- var t = page.logText + s
- if (t.length > page.maxTextChars) {
- // 超长:整体替换成最近内容,这种情况很少(只有日志刷得特别猛时)
- t = "……(内容过多,仅保留最近 " + Math.round(page.maxTextChars / 1000) + "K 字符)\n"
- + t.substring(t.length - page.maxTextChars)
- page.logText = t
- logArea.text = t
- } else {
- page.logText = t
- logArea.append(s)
- }
- // 只在实时刷新时自动跟到底部;关闭后用户可以自由往回滚看历史
- if (page.follow || s.indexOf("日志已轮转") >= 0)
- Qt.callLater(page.scrollToEnd)
- }
- function scrollToEnd() {
- var fi = logScroll.contentItem
- if (fi && fi.contentHeight > fi.height)
- fi.contentY = fi.contentHeight - fi.height
- }
- // 上传:LogViewer 内部会先复制一份临时副本再传(日志正在写,直传可能读到半行;
- // 上传器失败时会删文件,不能让它碰原始日志)
- function startUpload(path) {
- if (!logViewer || !path) return
- if (page.uploading) {
- nativeUtils.displayMessageBox("请稍候", "已有日志正在上传", 1)
- return
- }
- page.uploading = true
- page.uploadPercent = 0
- page.statusText = "正在上传: " + path
- // 注意:失败时 uploadLog 会在返回前同步发 uploadFinished,
- // 所以这里不要在调用之后再改 uploading
- logViewer.uploadLog(path)
- }
- // ---------------- 顶部操作条 ----------------
- Rectangle {
- id: topBar
- anchors.left: parent.left
- anchors.right: parent.right
- anchors.top: parent.top
- anchors.margins: dp(12)
- height: dp(48)
- radius: dp(8)
- color: app.cardColor
- Row {
- anchors.left: parent.left
- anchors.leftMargin: dp(10)
- anchors.verticalCenter: parent.verticalCenter
- spacing: dp(10)
- // 日志类型切换
- AppButton {
- id: btnAppLog
- text: "App 运行日志"
- flat: false
- textColor: "white"
- backgroundColor: page.logKind === 0 ? "#1a73e8" : "#9aa0a6"
- onClicked: {
- if (page.logKind === 0) return
- page.logKind = 0
- page.currentPath = ""
- page.currentName = ""
- page.follow = false
- followSwitch.checked = false
- page.logText = ""
- logArea.text = ""
- page.reloadFiles()
- }
- }
- AppButton {
- id: btnMcuLog
- text: "MCU 串口日志"
- flat: false
- textColor: "white"
- backgroundColor: page.logKind === 1 ? "#1a73e8" : "#9aa0a6"
- onClicked: {
- if (page.logKind === 1) return
- page.logKind = 1
- page.currentPath = ""
- page.currentName = ""
- page.follow = false
- followSwitch.checked = false
- page.logText = ""
- logArea.text = ""
- page.reloadFiles()
- }
- }
- AppButton {
- text: "刷新列表"
- flat: false
- textColor: "white"
- backgroundColor: "#5f6368"
- onClicked: page.reloadFiles()
- }
- AppButton {
- text: "上传当前文件"
- enabled: page.currentPath !== "" && !page.uploading
- flat: false
- textColor: "white"
- backgroundColor: enabled ? "#0aa26b" : "#9aa0a6"
- onClicked: page.startUpload(page.currentPath)
- }
- }
- AppText {
- anchors.right: parent.right
- anchors.rightMargin: dp(12)
- anchors.verticalCenter: parent.verticalCenter
- text: page.statusText
- color: "#666666"
- fontSize: 12
- elide: Text.ElideRight
- // 固定占右侧 30%:不用 implicitWidth 做 Math.min,避免与自身宽度形成绑定循环
- width: parent.width * 0.3
- horizontalAlignment: Text.AlignRight
- }
- }
- // ---------------- 主体:左文件列表 / 右日志内容 ----------------
- Row {
- anchors.left: parent.left
- anchors.right: parent.right
- anchors.top: topBar.bottom
- anchors.bottom: parent.bottom
- anchors.margins: dp(12)
- anchors.topMargin: dp(8)
- spacing: dp(12)
- // -------- 左侧:文件列表 --------
- Rectangle {
- width: dp(340)
- height: parent.height
- radius: dp(8)
- color: app.cardColor
- Column {
- anchors.fill: parent
- anchors.margins: dp(10)
- spacing: dp(8)
- AppText {
- text: page.kindName() + "(" + page.fileList.length + " 个文件)"
- color: app.fontColor
- font.bold: true
- fontSize: 14
- }
- // 日志目录:现场可直接用文件管理器按这个路径取日志
- AppText {
- id: dirText
- width: parent.width
- text: "日志目录: "
- color: "#888888"
- fontSize: 10
- wrapMode: Text.WrapAtWordBoundaryOrAnywhere
- }
- // 注意:id 不能叫 fileList,会和 page.fileList 属性同名造成遮蔽
- Item {
- width: parent.width
- height: parent.height - dp(72)
- ListView {
- id: logFileList
- anchors.fill: parent
- clip: true
- spacing: dp(6)
- model: page.fileList
- boundsBehavior: Flickable.StopAtBounds
- delegate: Rectangle {
- id: fileRow
- width: logFileList.width
- height: dp(56)
- radius: dp(6)
- color: page.currentPath === modelData.path ? "#e8f0fe" : "#fafafa"
- border.width: 1
- border.color: page.currentPath === modelData.path ? "#1a73e8" : "#e0e0e0"
- Column {
- anchors.left: parent.left
- anchors.leftMargin: dp(8)
- anchors.right: btnRowUpload.left
- anchors.rightMargin: dp(6)
- anchors.verticalCenter: parent.verticalCenter
- spacing: dp(2)
- AppText {
- width: parent.width
- // "●" 标记正在写入的那个文件(run_log.txt / mcu_console.txt)
- text: (modelData.isCurrent ? "● " : "") + modelData.name
- color: app.fontColor
- fontSize: 13
- font.bold: modelData.isCurrent
- elide: Text.ElideMiddle
- }
- AppText {
- text: modelData.sizeText + " " + modelData.time
- color: "#888888"
- fontSize: 10
- }
- }
- AppButton {
- id: btnRowUpload
- anchors.right: parent.right
- anchors.rightMargin: dp(6)
- anchors.verticalCenter: parent.verticalCenter
- text: "上传"
- flat: false
- textColor: "white"
- backgroundColor: "#0aa26b"
- onClicked: page.startUpload(modelData.path)
- }
- // 点击整行打开查看;右侧给"上传"按钮留出位置
- MouseArea {
- anchors.fill: parent
- anchors.rightMargin: dp(74)
- onClicked: page.openFile(modelData.path, modelData.name)
- }
- }
- }
- // 空目录提示:放在 ListView 外面(ListView 的非 delegate 子项会被
- // 当作内容处理,位置不可控)
- AppText {
- visible: page.fileList.length === 0
- anchors.centerIn: parent
- text: "没有找到日志文件"
- color: "#999999"
- fontSize: 13
- }
- }
- }
- }
- // -------- 右侧:日志内容 --------
- Rectangle {
- width: parent.width - dp(340) - dp(12)
- height: parent.height
- radius: dp(8)
- color: "#1e1e1e"
- Column {
- anchors.fill: parent
- anchors.margins: dp(8)
- spacing: dp(6)
- // 内容区工具条
- Row {
- width: parent.width
- spacing: dp(10)
- AppText {
- width: parent.width - dp(430)
- anchors.verticalCenter: parent.verticalCenter
- text: page.currentName === "" ? "未选择日志文件" : page.currentName
- color: "#dddddd"
- fontSize: 13
- font.bold: true
- elide: Text.ElideMiddle
- }
- // tail -f:勾上后每 800ms 拉一次增量,只显示完整行
- AppText {
- anchors.verticalCenter: parent.verticalCenter
- text: "实时刷新"
- color: "#dddddd"
- fontSize: 12
- }
- AppSwitch {
- id: followSwitch
- anchors.verticalCenter: parent.verticalCenter
- enabled: page.currentPath !== ""
- onToggled: {
- page.follow = checked
- if (checked && page.currentPath !== "")
- logViewer.requestPoll(page.currentPath)
- page.statusText = checked ? "实时刷新中(tail -f)" : "已暂停刷新"
- }
- }
- AppButton {
- anchors.verticalCenter: parent.verticalCenter
- text: "重新加载"
- enabled: page.currentPath !== ""
- flat: false
- textColor: "white"
- backgroundColor: "#5f6368"
- onClicked: {
- if (!page.currentPath) return
- page.openFile(page.currentPath, page.currentName, true)
- page.statusText = "已重新加载"
- }
- }
- AppButton {
- anchors.verticalCenter: parent.verticalCenter
- text: "到底部"
- enabled: page.currentPath !== ""
- flat: false
- textColor: "white"
- backgroundColor: "#5f6368"
- onClicked: page.scrollToEnd()
- }
- }
- // 上传进度条
- Rectangle {
- width: parent.width
- height: page.uploading ? dp(4) : 0
- color: "#333333"
- Rectangle {
- height: parent.height
- width: parent.width * Math.max(0, Math.min(page.uploadPercent, 100)) / 100
- color: "#0aa26b"
- }
- }
- ScrollView {
- id: logScroll
- width: parent.width
- // 减去工具条 + 上传进度条,否则正文会被顶出可视区
- height: parent.height - dp(34)
- - (page.uploading ? dp(10) : 0)
- clip: true
- TextArea {
- id: logArea
- readOnly: true
- // 注意:这里故意不绑 text: page.logText。
- // 绑定会让每次刷新都重设整段文本,TextArea 要把十几万字符
- // 全部重新排版(日志文件一大,界面就卡住)。
- // 现在内容由 openFile 整体赋值 / appendText 用 append() 增量追加。
- wrapMode: TextArea.NoWrap
- selectByMouse: true
- color: "#d4d4d4"
- selectionColor: "#264f78"
- font.pixelSize: sp(11)
- // 不要写死 font.family: "monospace"!
- // Android 上 monospace(Droid/Roboto Mono) 没有中日韩字形,
- // MCU 串口日志里的中文会整片显示成"□/豆腐块"或错字,
- // 看起来就像日志里冒出"异常字符"。交给系统默认字体做字形回退。
- background: Rectangle { color: "transparent" }
- }
- }
- }
- // ---- 加载动画:读文件/首次渲染期间盖住内容区 ----
- // 日志文件可能有 10MB,读+渲染需要点时间,没有反馈现场会以为死机了
- Rectangle {
- anchors.fill: parent
- radius: dp(8)
- visible: page.logLoading
- color: "#cc1e1e1e"
- Column {
- anchors.centerIn: parent
- spacing: dp(10)
- BusyIndicator {
- anchors.horizontalCenter: parent.horizontalCenter
- width: dp(46)
- height: dp(46)
- running: page.logLoading
- }
- AppText {
- anchors.horizontalCenter: parent.horizontalCenter
- text: "正在加载日志…"
- color: "#dddddd"
- fontSize: 13
- }
- }
- }
- }
- }
- // ---------------- tail -f 定时器 ----------------
- // 800ms 一轮:requestPoll 只读新增字节,且只返回凑齐的整行,
- // 所以不会出现"半行中文"或重复内容;读取在子线程做,不会卡界面
- Timer {
- id: followTimer
- interval: 800
- repeat: true
- running: page.follow && page.currentPath !== ""
- onTriggered: {
- if (!logViewer || page.currentPath === "") return
- logViewer.requestPoll(page.currentPath)
- }
- }
- // ---------------- 上传/读取回调 ----------------
- Connections {
- target: logViewer
- // 首次打开/重新加载完成(子线程读完再回来,主线程不再被 IO 卡住)
- function onLogLoaded(path, text) {
- // 加载过程中用户又点了别的文件,丢弃这次迟到的结果
- if (path !== page.currentPath) return
- page.logLoading = false
- page.logText = text
- // 一次性赋值(这里必须整段设置,不是追加)
- logArea.text = text
- page.statusText = "已打开: " + page.currentName
- Qt.callLater(page.scrollToEnd)
- }
- // tail -f 的新增内容
- function onLogAppended(path, chunk) {
- if (path !== page.currentPath) return
- page.appendText(chunk)
- }
- function onUploadProgress(percent, path) {
- page.uploadPercent = percent
- }
- function onUploadFinished(success, path, error) {
- page.uploading = false
- page.uploadPercent = 0
- if (success) {
- page.statusText = "上传成功"
- nativeUtils.displayMessageBox("上传成功", "日志已上传:\n" + path, 1)
- } else {
- page.statusText = "上传失败: " + error
- nativeUtils.displayMessageBox("上传失败", "文件: " + path + "\n原因: " + error, 1)
- }
- }
- }
- Component.onCompleted: {
- page.reloadFiles()
- // 默认打开最新的那个(正在写的)文件,省一次点击
- if (page.fileList.length > 0)
- page.openFile(page.fileList[0].path, page.fileList[0].name)
- }
- // 离开页面时停掉刷新,避免后台一直读文件
- onVisibleChanged: {
- if (!visible) {
- page.follow = false
- followSwitch.checked = false
- }
- }
- }
|