/********** * 日志功能 */ /** * 获取当前时间的字符串 * @returns String */ function GetCurrentLocalTime() { var d = new Date() return `${d.getFullYear()}-${ d.getMonth() + 1 }-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}:${d.getMilliseconds()}` } function GetCNTimeString(date) { var d = date || new Date() return ( d.toLocaleString('zh-CN', { hour12: false }).replace(/\//gi, '-') + ':' + d.getMilliseconds() ) } /** * 记录日志 * @param {*} msg */ function loginfo(msg) { const _msg = `${GetCNTimeString()}:Info:${msg}` console.groupCollapsed(msg) console.trace() console.groupEnd() LogDebugInfoToStorage(_msg) } function logerr(data) { var _msg = `${GetCNTimeString()}:Error:${data}` if (data.stack) _msg = `${GetCNTimeString()}:Error\n\r${data.stack}` LogDebugInfoToStorage(_msg) } function IfLocalDebugInfo() { return wps.PluginStorage.getItem(constStrEnum.SaveLocalDebugInfo) || false } /** * 记录日志开关 * @param {*} ifSave */ function SetLocalDebug(ifSave) { wps.PluginStorage.setItem(constStrEnum.SaveLocalDebugInfo, ifSave) } //注意wps.FileSystem.readFileString 不能带有目录路径 function GetLocalLogFilePath() { var d = new Date() var filepath = `IndiDocX.${d.getMonth() + 1}-${d.getDate()}.log` return filepath } function ClearLocalDebug() { let filepath = GetLocalLogFilePath() wps.FileSystem.writeFileString(filepath, `--清空日志--`) wps.FileSystem.Remove(filepath) } /** * * @param {*} msg */ function LogDebugInfoToStorage(msg) { let filepath = GetLocalLogFilePath() const content = wps.FileSystem.readFileString(filepath) wps.FileSystem.writeFileString(filepath, content + '\n\r' + msg) } /** * 获取本地日志 */ function GetLocalDebugInfo() { let filepath = GetLocalLogFilePath() return wps.FileSystem.readFileString(filepath) } /** * 监控wps报错 */ window.onerror = function (msg, url, line, col, error) { //采用异步的方式 setTimeout(function () { var data = { msg } //不一定所有浏览器都支持col参数 col = col || (window.event && window.event.errorCharacter) || 0 data.url = url data.line = line data.col = col if (!!error && !!error.stack) { //如果浏览器有堆栈信息 //直接使用 data.stack = error.stack.toString() } else if (!!arguments.callee) { //尝试通过callee拿堆栈信息 var ext = [] var f = arguments.callee.caller, c = 3 //这里只拿三层堆栈信息 while (f && --c > 0) { ext.push(f.toString()) if (f === f.caller) { break //如果有环 } f = f.caller } ext = ext.join(',') data.stack = ext } //把data上报到后台! logerr(data) }, 0) return false //返回true 则控制台隐藏默认打印 }