logger.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**********
  2. * 日志功能
  3. */
  4. /**
  5. * 获取当前时间的字符串
  6. * @returns String
  7. */
  8. function GetCurrentLocalTime() {
  9. var d = new Date()
  10. return `${d.getFullYear()}-${
  11. d.getMonth() + 1
  12. }-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}:${d.getMilliseconds()}`
  13. }
  14. function GetCNTimeString(date) {
  15. var d = date || new Date()
  16. return (
  17. d.toLocaleString('zh-CN', { hour12: false }).replace(/\//gi, '-') +
  18. ':' +
  19. d.getMilliseconds()
  20. )
  21. }
  22. /**
  23. * 记录日志
  24. * @param {*} msg
  25. */
  26. function loginfo(msg) {
  27. const _msg = `${GetCNTimeString()}:Info:${msg}`
  28. console.groupCollapsed(msg)
  29. console.trace()
  30. console.groupEnd()
  31. LogDebugInfoToStorage(_msg)
  32. }
  33. function logerr(data) {
  34. var _msg = `${GetCNTimeString()}:Error:${data}`
  35. if (data.stack) _msg = `${GetCNTimeString()}:Error\n\r${data.stack}`
  36. LogDebugInfoToStorage(_msg)
  37. }
  38. function IfLocalDebugInfo() {
  39. return wps.PluginStorage.getItem(constStrEnum.SaveLocalDebugInfo) || false
  40. }
  41. /**
  42. * 记录日志开关
  43. * @param {*} ifSave
  44. */
  45. function SetLocalDebug(ifSave) {
  46. wps.PluginStorage.setItem(constStrEnum.SaveLocalDebugInfo, ifSave)
  47. }
  48. //注意wps.FileSystem.readFileString 不能带有目录路径
  49. function GetLocalLogFilePath() {
  50. var d = new Date()
  51. var filepath = `IndiDocX.${d.getMonth() + 1}-${d.getDate()}.log`
  52. return filepath
  53. }
  54. function ClearLocalDebug() {
  55. let filepath = GetLocalLogFilePath()
  56. wps.FileSystem.writeFileString(filepath, `--清空日志--`)
  57. wps.FileSystem.Remove(filepath)
  58. }
  59. /**
  60. *
  61. * @param {*} msg
  62. */
  63. function LogDebugInfoToStorage(msg) {
  64. let filepath = GetLocalLogFilePath()
  65. const content = wps.FileSystem.readFileString(filepath)
  66. wps.FileSystem.writeFileString(filepath, content + '\n\r' + msg)
  67. }
  68. /**
  69. * 获取本地日志
  70. */
  71. function GetLocalDebugInfo() {
  72. let filepath = GetLocalLogFilePath()
  73. return wps.FileSystem.readFileString(filepath)
  74. }
  75. /**
  76. * 监控wps报错
  77. */
  78. window.onerror = function (msg, url, line, col, error) {
  79. //采用异步的方式
  80. setTimeout(function () {
  81. var data = { msg }
  82. //不一定所有浏览器都支持col参数
  83. col = col || (window.event && window.event.errorCharacter) || 0
  84. data.url = url
  85. data.line = line
  86. data.col = col
  87. if (!!error && !!error.stack) {
  88. //如果浏览器有堆栈信息
  89. //直接使用
  90. data.stack = error.stack.toString()
  91. } else if (!!arguments.callee) {
  92. //尝试通过callee拿堆栈信息
  93. var ext = []
  94. var f = arguments.callee.caller,
  95. c = 3
  96. //这里只拿三层堆栈信息
  97. while (f && --c > 0) {
  98. ext.push(f.toString())
  99. if (f === f.caller) {
  100. break //如果有环
  101. }
  102. f = f.caller
  103. }
  104. ext = ext.join(',')
  105. data.stack = ext
  106. }
  107. //把data上报到后台!
  108. logerr(data)
  109. }, 0)
  110. return false //返回true 则控制台隐藏默认打印
  111. }