LogViewerPage.qml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. import Felgo
  2. import QtQuick
  3. import QtQuick.Controls
  4. import "../controls"
  5. import "../common"
  6. /**
  7. * 日志查看页(系统设置 -> 查看日志)
  8. *
  9. * 需求:
  10. * 1) 两类日志:App 运行日志(run_log*.txt)、MCU 串口日志(mcu_console*.txt);
  11. * 2) tail -f 实时刷新;
  12. * 3) 打开(选择)某个日志文件查看内容,包括轮转出来的历史文件;
  13. * 4) 选择日志文件上传(FTP,与图片同一套凭据/远端目录)。
  14. *
  15. * 数据来源全部是 C++ 侧 LogViewer(上下文属性名 logViewer),本页只做展示与交互。
  16. * 读写全部异步(子线程读文件,主线程只渲染),所以调用都是"发请求 + 收信号":
  17. * listLogs(kind) 同步枚举文件(很快,只列目录)
  18. * openLog(path, bytes) 打开文件尾部,结果 → logLoaded(path, text)
  19. * requestPoll(path) tail -f 一轮,结果 → logAppended(path, chunk)
  20. * resetTail(path) / uploadLog(path) 清偏移 / 上传(结果 → uploadFinished)
  21. *
  22. * 为什么不在 QML 里直接读文件:
  23. * QML 的 XMLHttpRequest 读不了 file://,也没有"从某个偏移量继续读"的能力,
  24. * 没法做 tail -f;日志还在被 spdlog 写,必须按行边界切分,否则会看到半行中文乱码。
  25. * 这些都在 LogViewer 里做掉了。
  26. *
  27. * 性能要点(日志文件 10MB 时曾出现界面卡死):
  28. * 1) 读文件在子线程,主线程不阻塞,加载期间显示 BusyIndicator;
  29. * 2) 只读尾部 64KB,不做全量读取;
  30. * 3) 追加用 logArea.append(),不整体重设 text(否则每次刷新都要重新排版十几万字符);
  31. * 4) 界面上最多保留 12 万字符,超出丢弃最旧的内容。
  32. */
  33. AppPage {
  34. id: page
  35. title: qsTr("日志查看")
  36. backgroundColor: app.theme_backgroundColor
  37. // 本页是 ProfilePage 用 navigationStack.push() 压入的第二层页面,
  38. // 必须保留导航栏,否则没有返回入口(与 PermissionPage 一致)
  39. navigationBarHidden: false
  40. // ---------------- 状态 ----------------
  41. property int logKind: 0 // 0=App 运行日志 1=MCU 串口日志
  42. property var fileList: [] // [{name,path,size,sizeText,time,isCurrent}]
  43. property string currentPath: "" // 当前打开的日志文件
  44. property string currentName: ""
  45. property bool follow: false // tail -f 开关
  46. property string logText: "" // 右侧文本内容
  47. property bool logLoading: false // 正在读文件(显示加载动画)
  48. property bool uploading: false
  49. property real uploadPercent: 0
  50. property string statusText: ""
  51. // 界面里最多保留的字符数:TextArea 每次改 text 都要重新排版整段内容,
  52. // 文本越长越慢(几十万字符时明显卡顿),这里压到 12 万字符
  53. // 更早的内容请看轮转出来的历史文件(run_log.1.txt …)
  54. readonly property int maxTextChars: 120000
  55. // 首次打开只读文件尾部这么多字节
  56. readonly property int loadBytes: 64 * 1024
  57. // ---------------- 方法 ----------------
  58. function kindName() {
  59. return logKind === 0 ? "App 运行日志" : "MCU 串口日志"
  60. }
  61. // 重新枚举某类日志的所有文件(含 run_log.1.txt 这类轮转文件)
  62. function reloadFiles() {
  63. if (!logViewer) return
  64. fileList = logViewer.listLogs(logKind)
  65. dirText.text = "日志目录: " + logViewer.logDir(logKind)
  66. if (fileList.length === 0)
  67. page.statusText = "该目录下还没有日志文件"
  68. }
  69. // 打开文件:读文件尾部(异步,读到后由 onLogLoaded 填内容)
  70. // 一定要异步:10MB 日志在 UI 线程读+渲染会把界面整段卡死,
  71. // 现在主线程只发请求,同时把加载动画显示出来
  72. function openFile(path, name, keepFollow) {
  73. if (!logViewer || !path) return
  74. page.currentPath = path
  75. page.currentName = name
  76. if (!keepFollow) { // "重新加载"要保持实时刷新开关不变
  77. page.follow = false
  78. followSwitch.checked = false
  79. }
  80. page.logText = ""
  81. logArea.text = "" // 不靠 text 绑定(见 appendText 说明)
  82. page.logLoading = true
  83. page.statusText = "正在加载: " + name
  84. logViewer.resetTail(path)
  85. logViewer.openLog(path, page.loadBytes)
  86. }
  87. // 追加文本(tail -f 每轮调用)
  88. // 关键:用 logArea.append() 只排版新增的那几行,不要整体重设 text
  89. // (TextArea 每次重设 text 都会把整段内容重新排版,十几万字符时肉眼可见地卡)
  90. function appendText(s) {
  91. if (!s) return
  92. var t = page.logText + s
  93. if (t.length > page.maxTextChars) {
  94. // 超长:整体替换成最近内容,这种情况很少(只有日志刷得特别猛时)
  95. t = "……(内容过多,仅保留最近 " + Math.round(page.maxTextChars / 1000) + "K 字符)\n"
  96. + t.substring(t.length - page.maxTextChars)
  97. page.logText = t
  98. logArea.text = t
  99. } else {
  100. page.logText = t
  101. logArea.append(s)
  102. }
  103. // 只在实时刷新时自动跟到底部;关闭后用户可以自由往回滚看历史
  104. if (page.follow || s.indexOf("日志已轮转") >= 0)
  105. Qt.callLater(page.scrollToEnd)
  106. }
  107. function scrollToEnd() {
  108. var fi = logScroll.contentItem
  109. if (fi && fi.contentHeight > fi.height)
  110. fi.contentY = fi.contentHeight - fi.height
  111. }
  112. // 上传:LogViewer 内部会先复制一份临时副本再传(日志正在写,直传可能读到半行;
  113. // 上传器失败时会删文件,不能让它碰原始日志)
  114. function startUpload(path) {
  115. if (!logViewer || !path) return
  116. if (page.uploading) {
  117. nativeUtils.displayMessageBox("请稍候", "已有日志正在上传", 1)
  118. return
  119. }
  120. page.uploading = true
  121. page.uploadPercent = 0
  122. page.statusText = "正在上传: " + path
  123. // 注意:失败时 uploadLog 会在返回前同步发 uploadFinished,
  124. // 所以这里不要在调用之后再改 uploading
  125. logViewer.uploadLog(path)
  126. }
  127. // ---------------- 顶部操作条 ----------------
  128. Rectangle {
  129. id: topBar
  130. anchors.left: parent.left
  131. anchors.right: parent.right
  132. anchors.top: parent.top
  133. anchors.margins: dp(12)
  134. height: dp(48)
  135. radius: dp(8)
  136. color: app.cardColor
  137. Row {
  138. anchors.left: parent.left
  139. anchors.leftMargin: dp(10)
  140. anchors.verticalCenter: parent.verticalCenter
  141. spacing: dp(10)
  142. // 日志类型切换
  143. AppButton {
  144. id: btnAppLog
  145. text: "App 运行日志"
  146. flat: false
  147. textColor: "white"
  148. backgroundColor: page.logKind === 0 ? "#1a73e8" : "#9aa0a6"
  149. onClicked: {
  150. if (page.logKind === 0) return
  151. page.logKind = 0
  152. page.currentPath = ""
  153. page.currentName = ""
  154. page.follow = false
  155. followSwitch.checked = false
  156. page.logText = ""
  157. logArea.text = ""
  158. page.reloadFiles()
  159. }
  160. }
  161. AppButton {
  162. id: btnMcuLog
  163. text: "MCU 串口日志"
  164. flat: false
  165. textColor: "white"
  166. backgroundColor: page.logKind === 1 ? "#1a73e8" : "#9aa0a6"
  167. onClicked: {
  168. if (page.logKind === 1) return
  169. page.logKind = 1
  170. page.currentPath = ""
  171. page.currentName = ""
  172. page.follow = false
  173. followSwitch.checked = false
  174. page.logText = ""
  175. logArea.text = ""
  176. page.reloadFiles()
  177. }
  178. }
  179. AppButton {
  180. text: "刷新列表"
  181. flat: false
  182. textColor: "white"
  183. backgroundColor: "#5f6368"
  184. onClicked: page.reloadFiles()
  185. }
  186. AppButton {
  187. text: "上传当前文件"
  188. enabled: page.currentPath !== "" && !page.uploading
  189. flat: false
  190. textColor: "white"
  191. backgroundColor: enabled ? "#0aa26b" : "#9aa0a6"
  192. onClicked: page.startUpload(page.currentPath)
  193. }
  194. }
  195. AppText {
  196. anchors.right: parent.right
  197. anchors.rightMargin: dp(12)
  198. anchors.verticalCenter: parent.verticalCenter
  199. text: page.statusText
  200. color: "#666666"
  201. fontSize: 12
  202. elide: Text.ElideRight
  203. // 固定占右侧 30%:不用 implicitWidth 做 Math.min,避免与自身宽度形成绑定循环
  204. width: parent.width * 0.3
  205. horizontalAlignment: Text.AlignRight
  206. }
  207. }
  208. // ---------------- 主体:左文件列表 / 右日志内容 ----------------
  209. Row {
  210. anchors.left: parent.left
  211. anchors.right: parent.right
  212. anchors.top: topBar.bottom
  213. anchors.bottom: parent.bottom
  214. anchors.margins: dp(12)
  215. anchors.topMargin: dp(8)
  216. spacing: dp(12)
  217. // -------- 左侧:文件列表 --------
  218. Rectangle {
  219. width: dp(340)
  220. height: parent.height
  221. radius: dp(8)
  222. color: app.cardColor
  223. Column {
  224. anchors.fill: parent
  225. anchors.margins: dp(10)
  226. spacing: dp(8)
  227. AppText {
  228. text: page.kindName() + "(" + page.fileList.length + " 个文件)"
  229. color: app.fontColor
  230. font.bold: true
  231. fontSize: 14
  232. }
  233. // 日志目录:现场可直接用文件管理器按这个路径取日志
  234. AppText {
  235. id: dirText
  236. width: parent.width
  237. text: "日志目录: "
  238. color: "#888888"
  239. fontSize: 10
  240. wrapMode: Text.WrapAtWordBoundaryOrAnywhere
  241. }
  242. // 注意:id 不能叫 fileList,会和 page.fileList 属性同名造成遮蔽
  243. Item {
  244. width: parent.width
  245. height: parent.height - dp(72)
  246. ListView {
  247. id: logFileList
  248. anchors.fill: parent
  249. clip: true
  250. spacing: dp(6)
  251. model: page.fileList
  252. boundsBehavior: Flickable.StopAtBounds
  253. delegate: Rectangle {
  254. id: fileRow
  255. width: logFileList.width
  256. height: dp(56)
  257. radius: dp(6)
  258. color: page.currentPath === modelData.path ? "#e8f0fe" : "#fafafa"
  259. border.width: 1
  260. border.color: page.currentPath === modelData.path ? "#1a73e8" : "#e0e0e0"
  261. Column {
  262. anchors.left: parent.left
  263. anchors.leftMargin: dp(8)
  264. anchors.right: btnRowUpload.left
  265. anchors.rightMargin: dp(6)
  266. anchors.verticalCenter: parent.verticalCenter
  267. spacing: dp(2)
  268. AppText {
  269. width: parent.width
  270. // "●" 标记正在写入的那个文件(run_log.txt / mcu_console.txt)
  271. text: (modelData.isCurrent ? "● " : "") + modelData.name
  272. color: app.fontColor
  273. fontSize: 13
  274. font.bold: modelData.isCurrent
  275. elide: Text.ElideMiddle
  276. }
  277. AppText {
  278. text: modelData.sizeText + " " + modelData.time
  279. color: "#888888"
  280. fontSize: 10
  281. }
  282. }
  283. AppButton {
  284. id: btnRowUpload
  285. anchors.right: parent.right
  286. anchors.rightMargin: dp(6)
  287. anchors.verticalCenter: parent.verticalCenter
  288. text: "上传"
  289. flat: false
  290. textColor: "white"
  291. backgroundColor: "#0aa26b"
  292. onClicked: page.startUpload(modelData.path)
  293. }
  294. // 点击整行打开查看;右侧给"上传"按钮留出位置
  295. MouseArea {
  296. anchors.fill: parent
  297. anchors.rightMargin: dp(74)
  298. onClicked: page.openFile(modelData.path, modelData.name)
  299. }
  300. }
  301. }
  302. // 空目录提示:放在 ListView 外面(ListView 的非 delegate 子项会被
  303. // 当作内容处理,位置不可控)
  304. AppText {
  305. visible: page.fileList.length === 0
  306. anchors.centerIn: parent
  307. text: "没有找到日志文件"
  308. color: "#999999"
  309. fontSize: 13
  310. }
  311. }
  312. }
  313. }
  314. // -------- 右侧:日志内容 --------
  315. Rectangle {
  316. width: parent.width - dp(340) - dp(12)
  317. height: parent.height
  318. radius: dp(8)
  319. color: "#1e1e1e"
  320. Column {
  321. anchors.fill: parent
  322. anchors.margins: dp(8)
  323. spacing: dp(6)
  324. // 内容区工具条
  325. Row {
  326. width: parent.width
  327. spacing: dp(10)
  328. AppText {
  329. width: parent.width - dp(430)
  330. anchors.verticalCenter: parent.verticalCenter
  331. text: page.currentName === "" ? "未选择日志文件" : page.currentName
  332. color: "#dddddd"
  333. fontSize: 13
  334. font.bold: true
  335. elide: Text.ElideMiddle
  336. }
  337. // tail -f:勾上后每 800ms 拉一次增量,只显示完整行
  338. AppText {
  339. anchors.verticalCenter: parent.verticalCenter
  340. text: "实时刷新"
  341. color: "#dddddd"
  342. fontSize: 12
  343. }
  344. AppSwitch {
  345. id: followSwitch
  346. anchors.verticalCenter: parent.verticalCenter
  347. enabled: page.currentPath !== ""
  348. onToggled: {
  349. page.follow = checked
  350. if (checked && page.currentPath !== "")
  351. logViewer.requestPoll(page.currentPath)
  352. page.statusText = checked ? "实时刷新中(tail -f)" : "已暂停刷新"
  353. }
  354. }
  355. AppButton {
  356. anchors.verticalCenter: parent.verticalCenter
  357. text: "重新加载"
  358. enabled: page.currentPath !== ""
  359. flat: false
  360. textColor: "white"
  361. backgroundColor: "#5f6368"
  362. onClicked: {
  363. if (!page.currentPath) return
  364. page.openFile(page.currentPath, page.currentName, true)
  365. page.statusText = "已重新加载"
  366. }
  367. }
  368. AppButton {
  369. anchors.verticalCenter: parent.verticalCenter
  370. text: "到底部"
  371. enabled: page.currentPath !== ""
  372. flat: false
  373. textColor: "white"
  374. backgroundColor: "#5f6368"
  375. onClicked: page.scrollToEnd()
  376. }
  377. }
  378. // 上传进度条
  379. Rectangle {
  380. width: parent.width
  381. height: page.uploading ? dp(4) : 0
  382. color: "#333333"
  383. Rectangle {
  384. height: parent.height
  385. width: parent.width * Math.max(0, Math.min(page.uploadPercent, 100)) / 100
  386. color: "#0aa26b"
  387. }
  388. }
  389. ScrollView {
  390. id: logScroll
  391. width: parent.width
  392. // 减去工具条 + 上传进度条,否则正文会被顶出可视区
  393. height: parent.height - dp(34)
  394. - (page.uploading ? dp(10) : 0)
  395. clip: true
  396. TextArea {
  397. id: logArea
  398. readOnly: true
  399. // 注意:这里故意不绑 text: page.logText。
  400. // 绑定会让每次刷新都重设整段文本,TextArea 要把十几万字符
  401. // 全部重新排版(日志文件一大,界面就卡住)。
  402. // 现在内容由 openFile 整体赋值 / appendText 用 append() 增量追加。
  403. wrapMode: TextArea.NoWrap
  404. selectByMouse: true
  405. color: "#d4d4d4"
  406. selectionColor: "#264f78"
  407. font.pixelSize: sp(11)
  408. // 不要写死 font.family: "monospace"!
  409. // Android 上 monospace(Droid/Roboto Mono) 没有中日韩字形,
  410. // MCU 串口日志里的中文会整片显示成"□/豆腐块"或错字,
  411. // 看起来就像日志里冒出"异常字符"。交给系统默认字体做字形回退。
  412. background: Rectangle { color: "transparent" }
  413. }
  414. }
  415. }
  416. // ---- 加载动画:读文件/首次渲染期间盖住内容区 ----
  417. // 日志文件可能有 10MB,读+渲染需要点时间,没有反馈现场会以为死机了
  418. Rectangle {
  419. anchors.fill: parent
  420. radius: dp(8)
  421. visible: page.logLoading
  422. color: "#cc1e1e1e"
  423. Column {
  424. anchors.centerIn: parent
  425. spacing: dp(10)
  426. BusyIndicator {
  427. anchors.horizontalCenter: parent.horizontalCenter
  428. width: dp(46)
  429. height: dp(46)
  430. running: page.logLoading
  431. }
  432. AppText {
  433. anchors.horizontalCenter: parent.horizontalCenter
  434. text: "正在加载日志…"
  435. color: "#dddddd"
  436. fontSize: 13
  437. }
  438. }
  439. }
  440. }
  441. }
  442. // ---------------- tail -f 定时器 ----------------
  443. // 800ms 一轮:requestPoll 只读新增字节,且只返回凑齐的整行,
  444. // 所以不会出现"半行中文"或重复内容;读取在子线程做,不会卡界面
  445. Timer {
  446. id: followTimer
  447. interval: 800
  448. repeat: true
  449. running: page.follow && page.currentPath !== ""
  450. onTriggered: {
  451. if (!logViewer || page.currentPath === "") return
  452. logViewer.requestPoll(page.currentPath)
  453. }
  454. }
  455. // ---------------- 上传/读取回调 ----------------
  456. Connections {
  457. target: logViewer
  458. // 首次打开/重新加载完成(子线程读完再回来,主线程不再被 IO 卡住)
  459. function onLogLoaded(path, text) {
  460. // 加载过程中用户又点了别的文件,丢弃这次迟到的结果
  461. if (path !== page.currentPath) return
  462. page.logLoading = false
  463. page.logText = text
  464. // 一次性赋值(这里必须整段设置,不是追加)
  465. logArea.text = text
  466. page.statusText = "已打开: " + page.currentName
  467. Qt.callLater(page.scrollToEnd)
  468. }
  469. // tail -f 的新增内容
  470. function onLogAppended(path, chunk) {
  471. if (path !== page.currentPath) return
  472. page.appendText(chunk)
  473. }
  474. function onUploadProgress(percent, path) {
  475. page.uploadPercent = percent
  476. }
  477. function onUploadFinished(success, path, error) {
  478. page.uploading = false
  479. page.uploadPercent = 0
  480. if (success) {
  481. page.statusText = "上传成功"
  482. nativeUtils.displayMessageBox("上传成功", "日志已上传:\n" + path, 1)
  483. } else {
  484. page.statusText = "上传失败: " + error
  485. nativeUtils.displayMessageBox("上传失败", "文件: " + path + "\n原因: " + error, 1)
  486. }
  487. }
  488. }
  489. Component.onCompleted: {
  490. page.reloadFiles()
  491. // 默认打开最新的那个(正在写的)文件,省一次点击
  492. if (page.fileList.length > 0)
  493. page.openFile(page.fileList[0].path, page.fileList[0].name)
  494. }
  495. // 离开页面时停掉刷新,避免后台一直读文件
  496. onVisibleChanged: {
  497. if (!visible) {
  498. page.follow = false
  499. followSwitch.checked = false
  500. }
  501. }
  502. }