From 98f4236c354947f0830be65bd6f81a49777900d8 Mon Sep 17 00:00:00 2001 From: MrKBear Date: Fri, 31 Dec 2021 13:43:23 +0800 Subject: [PATCH] Merge Logger Module into one File! --- miniprogram/app.ts | 3 +- miniprogram/core/Api.ts | 7 +- miniprogram/core/LogLabel.ts | 227 --------- miniprogram/core/Logger.ts | 566 ++++++++++++++++++++++- miniprogram/core/Module.ts | 4 +- miniprogram/core/PresetLogLabel.ts | 347 -------------- miniprogram/core/Storage.ts | 4 +- miniprogram/core/TestCase.ts | 2 +- miniprogram/pages/Timetable/StatusBar.ts | 4 +- 9 files changed, 570 insertions(+), 594 deletions(-) delete mode 100644 miniprogram/core/LogLabel.ts delete mode 100644 miniprogram/core/PresetLogLabel.ts diff --git a/miniprogram/app.ts b/miniprogram/app.ts index ba8fa89..f2661d8 100644 --- a/miniprogram/app.ts +++ b/miniprogram/app.ts @@ -1,6 +1,5 @@ import { IAppAPIParam } from "./core/Api"; -import { Logger } from "./core/Logger"; -import { LevelLogLabel, LifeCycleLogLabel } from "./core/PresetLogLabel"; +import { Logger, LevelLogLabel, LifeCycleLogLabel } from "./core/Logger"; App({ diff --git a/miniprogram/core/Api.ts b/miniprogram/core/Api.ts index 44ad978..7ecf7f8 100644 --- a/miniprogram/core/Api.ts +++ b/miniprogram/core/Api.ts @@ -1,8 +1,5 @@ import { EventEmitter } from "./EventEmitter"; -import { LogLabel } from "./LogLabel"; -import { Logger } from "./Logger"; -import { LevelLogLabel, colorRadio, StatusLabel } from "./PresetLogLabel"; - +import { Logger, LogLabel, LevelLogLabel, colorRadio, StatusLabel } from "./Logger"; interface IAppAPIParam { api: { @@ -518,4 +515,4 @@ enum HTTPMethod { } export default API; -export { API, IParamSetting, IAppAPIParam, HTTPMethod } \ No newline at end of file +export { API, IParamSetting, IAppAPIParam, HTTPMethod, RequestPolicy } \ No newline at end of file diff --git a/miniprogram/core/LogLabel.ts b/miniprogram/core/LogLabel.ts deleted file mode 100644 index a4d3424..0000000 --- a/miniprogram/core/LogLabel.ts +++ /dev/null @@ -1,227 +0,0 @@ - -/** - * 调试输出样式 - */ -class LogStyle { - - /** - * 日志文字颜色 - */ - private color:string | undefined; - - /** - * 日志背景颜色 - */ - private backgroundColor:string | undefined; - - /** - * 日志文字粗细 - */ - private weight:string | undefined; - - /** - * 日志文字大小 - */ - private size:string | undefined; - - /** - * 日志文字字体 - */ - private family:string | undefined; - - /** - * 日志文字圆角 - */ - private borderRadius:string | undefined; - - /** - * 日志文字边框 - */ - private border:string | undefined; - - /** - * 日志文字外边距 - */ - private margin:string | undefined; - - /** - * 日志文字内边距 - */ - private padding:string | undefined; - - /** - * 设置颜色 - * @param color 日志文字颜色 - * @param backgroundColor 日志背景颜色 - */ - public setColor(color?:string, backgroundColor?:string):LogStyle { - this.color = color ?? this.color; - this.backgroundColor = backgroundColor ?? this.backgroundColor; - return this; - } - - /** - * 设置边框 - * @param borderRadius 日志文字圆角 - * @param border 日志文字边框 - */ - public setBorder(borderRadius?:string, border?:string):LogStyle { - this.borderRadius = borderRadius ?? this.borderRadius; - this.border = border ?? this.border; - return this; - } - - /** - * 设置文字 - * @param weight 日志文字粗细 - * @param family 日志文字字体 - */ - public setFont(weight?:string, family?:string):LogStyle { - this.weight = weight ?? this.weight; - this.family = family ?? this.family; - return this; - } - - /** - * 设置文字大小 - * @param size 日志文字大小 - */ - public setSize(size?:string):LogStyle { - this.size = size ?? this.size; - return this; - } - - /** - * 设置内边距外边距 - * @param padding 内边距 - * @param margin 外边距 - */ - public setBlank(padding?:string, margin?:string):LogStyle { - this.padding = padding ?? this.padding; - this.margin = margin ?? this.margin; - return this; - } - - /** - * 字符化转义样式 - */ - public stringify():string { - let stringArr:string[] = []; - - this.color && stringArr.push(`color:${ this.color }`); - this.backgroundColor && stringArr.push(`background-color:${ this.backgroundColor }`); - this.weight && stringArr.push(`font-weight:${ this.weight }`); - this.family && stringArr.push(`font-family:${ this.family }`); - this.borderRadius && stringArr.push(`border-radius:${ this.borderRadius }`); - this.border && stringArr.push(`border:${ this.border }`); - this.size && stringArr.push(`font-size:${ this.size }`); - this.padding && stringArr.push(`padding:${ this.padding }`); - this.margin && stringArr.push(`margin:${ this.margin }`); - - return stringArr.join(";"); - } - - /** - * 克隆一个新的 LogStyle - */ - public clone():LogStyle { - return new LogStyle() - .setColor(this.color, this.backgroundColor) - .setBorder(this.borderRadius, this.border) - .setFont(this.weight, this.family) - .setBlank(this.padding, this.margin) - .setSize(this.size) - } -} - -/** - * 日志标签 - */ -class LogLabel { - - /** - * 关键字 - * 用于标识这个类别 - */ - public key:string; - - /** - * 文字样式 - */ - public style:LogStyle; - - /** - * 是否受到过滤器影响 - */ - public checked:boolean; - - /** - * 是否输出 - */ - public display:boolean; - - /** - * 是否为附件标签 - * 例如回车、时间、代码位置 - */ - public attach:boolean; - - /** - * @param key 关键字 - * @param style 文字样式 - */ - constructor(key:string, style:LogStyle, - checked?:boolean, display?:boolean, attach?:boolean) { - this.key = key; - this.style = style; - this.checked = checked ?? true; - this.display = display ?? true; - this.attach = attach ?? false; - } - - /** - * 获得 Logger 输出使用的内容 - */ - public getLoggerOutput():string { - if(!this.display) return ""; - return `%c${ this.key }`; - } - - /** - * 获得 Text 输出内容 - */ - public getTextOutput():string { - if(!this.display) return ""; - return `[${ this.key }]`; - } - - /** - * 获得 style 格式化 - */ - public getStyleOutput():string { - if(!this.display) return ""; - return this.style.stringify(); - } - - /** - * 校验 - */ - public checking(src:RegExp | string):boolean { - - let pass = false; - - // 关闭校验 - if(!this.checked) return pass; - - if(src instanceof RegExp) { - pass = (src as RegExp).test(this.key) - } else { - pass = (src as string) === this.key; - } - - return pass; - } -} - -export default LogLabel; -export {LogLabel, LogStyle} \ No newline at end of file diff --git a/miniprogram/core/Logger.ts b/miniprogram/core/Logger.ts index c1c5b0a..5b745bb 100644 --- a/miniprogram/core/Logger.ts +++ b/miniprogram/core/Logger.ts @@ -1,7 +1,228 @@ import { LOGGER_FILTER, LOGGER_CONSOLE, LOGGER_STYLE } from "./Config"; -import { StackLogLabel } from "./PresetLogLabel"; -import { LogLabel } from "./LogLabel"; +/** + * 调试输出样式 + */ +class LogStyle { + + /** + * 日志文字颜色 + */ + private color:string | undefined; + + /** + * 日志背景颜色 + */ + private backgroundColor:string | undefined; + + /** + * 日志文字粗细 + */ + private weight:string | undefined; + + /** + * 日志文字大小 + */ + private size:string | undefined; + + /** + * 日志文字字体 + */ + private family:string | undefined; + + /** + * 日志文字圆角 + */ + private borderRadius:string | undefined; + + /** + * 日志文字边框 + */ + private border:string | undefined; + + /** + * 日志文字外边距 + */ + private margin:string | undefined; + + /** + * 日志文字内边距 + */ + private padding:string | undefined; + + /** + * 设置颜色 + * @param color 日志文字颜色 + * @param backgroundColor 日志背景颜色 + */ + public setColor(color?:string, backgroundColor?:string):LogStyle { + this.color = color ?? this.color; + this.backgroundColor = backgroundColor ?? this.backgroundColor; + return this; + } + + /** + * 设置边框 + * @param borderRadius 日志文字圆角 + * @param border 日志文字边框 + */ + public setBorder(borderRadius?:string, border?:string):LogStyle { + this.borderRadius = borderRadius ?? this.borderRadius; + this.border = border ?? this.border; + return this; + } + + /** + * 设置文字 + * @param weight 日志文字粗细 + * @param family 日志文字字体 + */ + public setFont(weight?:string, family?:string):LogStyle { + this.weight = weight ?? this.weight; + this.family = family ?? this.family; + return this; + } + + /** + * 设置文字大小 + * @param size 日志文字大小 + */ + public setSize(size?:string):LogStyle { + this.size = size ?? this.size; + return this; + } + + /** + * 设置内边距外边距 + * @param padding 内边距 + * @param margin 外边距 + */ + public setBlank(padding?:string, margin?:string):LogStyle { + this.padding = padding ?? this.padding; + this.margin = margin ?? this.margin; + return this; + } + + /** + * 字符化转义样式 + */ + public stringify():string { + let stringArr:string[] = []; + + this.color && stringArr.push(`color:${ this.color }`); + this.backgroundColor && stringArr.push(`background-color:${ this.backgroundColor }`); + this.weight && stringArr.push(`font-weight:${ this.weight }`); + this.family && stringArr.push(`font-family:${ this.family }`); + this.borderRadius && stringArr.push(`border-radius:${ this.borderRadius }`); + this.border && stringArr.push(`border:${ this.border }`); + this.size && stringArr.push(`font-size:${ this.size }`); + this.padding && stringArr.push(`padding:${ this.padding }`); + this.margin && stringArr.push(`margin:${ this.margin }`); + + return stringArr.join(";"); + } + + /** + * 克隆一个新的 LogStyle + */ + public clone():LogStyle { + return new LogStyle() + .setColor(this.color, this.backgroundColor) + .setBorder(this.borderRadius, this.border) + .setFont(this.weight, this.family) + .setBlank(this.padding, this.margin) + .setSize(this.size) + } +} + +/** + * 日志标签 + */ +class LogLabel { + + /** + * 关键字 + * 用于标识这个类别 + */ + public key:string; + + /** + * 文字样式 + */ + public style:LogStyle; + + /** + * 是否受到过滤器影响 + */ + public checked:boolean; + + /** + * 是否输出 + */ + public display:boolean; + + /** + * 是否为附件标签 + * 例如回车、时间、代码位置 + */ + public attach:boolean; + + /** + * @param key 关键字 + * @param style 文字样式 + */ + constructor(key:string, style:LogStyle, + checked?:boolean, display?:boolean, attach?:boolean) { + this.key = key; + this.style = style; + this.checked = checked ?? true; + this.display = display ?? true; + this.attach = attach ?? false; + } + + /** + * 获得 Logger 输出使用的内容 + */ + public getLoggerOutput():string { + if(!this.display) return ""; + return `%c${ this.key }`; + } + + /** + * 获得 Text 输出内容 + */ + public getTextOutput():string { + if(!this.display) return ""; + return `[${ this.key }]`; + } + + /** + * 获得 style 格式化 + */ + public getStyleOutput():string { + if(!this.display) return ""; + return this.style.stringify(); + } + + /** + * 校验 + */ + public checking(src:RegExp | string):boolean { + + let pass = false; + + // 关闭校验 + if(!this.checked) return pass; + + if(src instanceof RegExp) { + pass = (src as RegExp).test(this.key) + } else { + pass = (src as string) === this.key; + } + + return pass; + } +} /** * 多重内容捆绑 @@ -231,5 +452,344 @@ class Logger { } +/** + * 栈信息 + */ + class StackInfo { + + /** + * 函数名 + */ + public functionName:string | undefined; + + /** + * 文件名 + */ + public fileName:string | undefined; + + /** + * 文件路径 + */ + public url:string | undefined; + + /** + * 文件名和行号 + */ + public fileNameLine: string | undefined; + + /** + * 设置信息 + * @param functionName 函数名 + * @param fileName 文件名 + * @param url 文件路径 + */ + public setInfo(functionName:string, fileNameLine:string, url:string):StackInfo { + this.functionName = functionName; + this.fileNameLine = fileNameLine; + this.url = url; + return this; + } + + /** + * 计算文件名 + */ + public calcFileName():string | undefined { + + let replaceToTs = this.fileNameLine?.replace(".js", ".ts"); + let matched = replaceToTs?.match(/^(.+\.(js|ts)):\d+:\d+$/); + + return matched ? matched[1] : undefined; + } + + /** + * 计算路径名 + */ + public calcPathName():string | undefined { + + let replaceToTs = this.url?.replace(".js", ".ts"); + let matched = replaceToTs?.match(/^https?:\/\/(\d+\.){3}\d+:\d+\/(.+):\d+:\d+$/); + + return matched ? matched[2] : undefined; + } + + /** + * 获取函数调用栈列表 + */ + public static getCallStack():StackInfo[] { + + // 获取堆栈信息 + let stack:string | undefined = new Error().stack; + + if (stack === void 0) return []; + + // 去除 Error + stack = stack.replace(/^(Error)\s/, ""); + + // 获取堆栈信息 + let stackList:string[] = stack.split(/\n/); + + let callStack:StackInfo[] = []; + + for(let i = 0; i < stackList.length; i++) { + + let matcher = stackList[i].match(/^\s+at\s+(.+)\s(\(.+\))/); + if (matcher === null || matcher.length < 3) continue; + + let fileName = matcher[2].match(/.+\/(.+\..+:\d+:\d+)\)/); + if (fileName === null || matcher.length < 2) continue; + + callStack.push(new StackInfo().setInfo( + matcher[1], fileName[1], matcher[2]?.replace(/(\(|\))/g, "") + )) + } + + // console.log(callStack); + + return callStack; + } + + /** + * 排除的 + */ + public static readonly excludeFile:RegExp = /^.*(\\|\/)core(\\|\/)(.*Log.*).js:\d+:\d+/; + + /** + * 获取第一个调用栈 + */ + public static getFirstStack():StackInfo | undefined { + + let callStack = this.getCallStack(); + + for(let i = 0; i < callStack.length; i++) { + + if(!callStack[i].url) continue; + + if(!StackInfo.excludeFile.test(callStack[i].url ?? "")) { + return callStack[i]; + } + } + + return; + } +} + +/** + * 内部预定义的 LogLabel + */ +class StackLogLabel { + + /** + * 堆栈路径样式 + */ + public static readonly normalStyle:LogStyle = new LogStyle() + .setColor("#979797").setBorder("4px", "1px solid #979797").setBlank("0 5px"); + + /** + * 一个回车 + */ + public static readonly blankLabel = new LogLabel("\n\r", + new LogStyle(), false, true, true); + + /** + * 包含文件名和行号的 label + */ + public static get fileNameLabel():LogLabel { + + // 获得调用堆栈 + let stack = StackInfo.getFirstStack(); + + return new LogLabel(stack?.calcFileName() ?? "Unknown file name", + StackLogLabel.normalStyle, false, true, true); + } + + /** + * 包含 URL 链接的 label + */ + public static get urlLabel():LogLabel { + + // 获得调用堆栈 + let stack = StackInfo.getFirstStack(); + + return new LogLabel(stack?.calcPathName() ?? "Unknown url", + StackLogLabel.normalStyle, false, true, true); + } + + /** + * 仅仅用来 filter 的 URL 链接的 label + */ + public static get filterUrlLabel():LogLabel { + + // 获得调用堆栈 + let stack = StackInfo.getFirstStack(); + + return new LogLabel(stack?.calcPathName() ?? "Unknown url", + StackLogLabel.normalStyle, true, false, true); + } + +} + +/** + * 生成圆角颜色标签样式 + */ +const normalLevelStyleGen = (color:string):LogStyle => { + return new LogStyle().setBorder("4px", `1px solid ${color}`) + .setColor(color).setBlank("0 5px"); +} + +/** + * 调试等级标签 + */ +class LevelLogLabel { + + /** + * 致命 + */ + static readonly FatalLabel = new LogLabel( + "FATAL", normalLevelStyleGen("#FF00CC") + ); + + /** + * 错误 + */ + static readonly ErrorLabel = new LogLabel( + "ERROR", normalLevelStyleGen("#FF0000") + ); + + /** + * 警告 + */ + static readonly WarnLabel = new LogLabel( + "WARN", normalLevelStyleGen("#FF9900") + ); + + /** + * 消息 + */ + static readonly InfoLabel = new LogLabel( + "INFO", normalLevelStyleGen("#99FF00") + ); + + /** + * 调试 + */ + static readonly DebugLabel = new LogLabel( + "DEBUG", normalLevelStyleGen("#00FF99") + ); + + /** + * 追踪 + */ + static readonly TraceLabel = new LogLabel( + "TRACE", normalLevelStyleGen("#00CCFF") + ); +} + +/** + * 生成圆角颜色标签样式 + */ +const normalLifeStyleGen = (r:number, g:number, b:number):LogStyle => { + return new LogStyle().setBorder("4px", `1px solid rgb(${ r }, ${ g }, ${ b })`) + .setColor(`rgb(${ r }, ${ g }, ${ b })`, `rgba(${ r }, ${ g }, ${ b }, .1)`) + .setBlank("0 5px"); +} + +/** + * 生命周期标签 + */ +class LifeCycleLogLabel { + + /** + * 小程序加载时 + */ + static readonly OnLaunchLabel = new LogLabel( + "onLaunch", normalLifeStyleGen(160, 32, 240) + ); + + /** + * 生命周期函数--监听页面加载 + */ + static readonly OnLoadLabel = new LogLabel( + "onLoad", normalLifeStyleGen(255, 140, 105) + ); + + /** + * 生命周期函数--监听页面初次渲染完成 + */ + static readonly OnReadyLabel = new LogLabel( + "onReady", normalLifeStyleGen(255, 127, 36) + ); + + /** + * 生命周期函数--监听页面显示 + */ + static readonly OnShowLabel = new LogLabel( + "onShow", normalLifeStyleGen(255, 215, 0) + ) + + /** + * 生命周期函数--监听页面隐藏 + */ + static readonly OnHideLabel = new LogLabel( + "onHide", normalLifeStyleGen(173, 255, 47) + ); + + /** + * 生命周期函数--监听页面卸载 + */ + static readonly OnUnloadLabel = new LogLabel( + "onUnload", normalLifeStyleGen(127, 255, 212) + ); + + /** + * 页面相关事件处理函数--监听用户下拉动作 + */ + static readonly OnPullDownRefreshLabel = new LogLabel( + "onPullDownRefresh", normalLifeStyleGen(0, 191, 255) + ); + + /** + * 页面上拉触底事件的处理函数 + */ + static readonly OnReachBottomLabel = new LogLabel( + "onReachBottom", normalLifeStyleGen(84, 255, 159) + ); + + /** + * 用户点击右上角分享 + */ + static readonly OnShareAppMessageLabel = new LogLabel( + "onShareAppMessage", normalLifeStyleGen(147, 112, 219) + ); +} + +class StatusLabel { + + /** + * 等待 + */ + static readonly Pending = new LogLabel( + "◉", new LogStyle().setBlank("0 2px").setBorder("1000px", "1px solid lightblue").setColor("lightblue") + ); + + /** + * 成功 + */ + static readonly Success = new LogLabel( + "√", new LogStyle().setBlank("0 4px").setBorder("1000px", "1px solid lightgreen").setColor("lightgreen") + ); + + /** + * 失败 + */ + static readonly Failed = new LogLabel( + "×", new LogStyle().setBlank("0 4px").setBorder("1000px", "1px solid red").setColor("red") + ); +} + +const NormalStyle = StackLogLabel.normalStyle; + export default Logger; -export { Logger }; \ No newline at end of file +export { + Logger, LogLabel, LevelLogLabel, StackLogLabel, LifeCycleLogLabel, LogStyle, + StackInfo, StatusLabel, NormalStyle, normalLifeStyleGen as colorRadio +}; \ No newline at end of file diff --git a/miniprogram/core/Module.ts b/miniprogram/core/Module.ts index dfeed61..59f62a5 100644 --- a/miniprogram/core/Module.ts +++ b/miniprogram/core/Module.ts @@ -1,7 +1,5 @@ import { EventEmitter, EventType } from "./EventEmitter"; -import { LogLabel, LogStyle } from "./LogLabel"; -import { Logger } from "./Logger"; -import { LevelLogLabel } from "./PresetLogLabel"; +import { Logger, LogLabel, LogStyle, LevelLogLabel } from "./Logger"; /** * 自定义对象类型 diff --git a/miniprogram/core/PresetLogLabel.ts b/miniprogram/core/PresetLogLabel.ts deleted file mode 100644 index 46d0daa..0000000 --- a/miniprogram/core/PresetLogLabel.ts +++ /dev/null @@ -1,347 +0,0 @@ -import { LogLabel, LogStyle } from "./LogLabel"; - -/** - * 栈信息 - */ -class StackInfo { - - /** - * 函数名 - */ - public functionName:string | undefined; - - /** - * 文件名 - */ - public fileName:string | undefined; - - /** - * 文件路径 - */ - public url:string | undefined; - - /** - * 文件名和行号 - */ - public fileNameLine: string | undefined; - - /** - * 设置信息 - * @param functionName 函数名 - * @param fileName 文件名 - * @param url 文件路径 - */ - public setInfo(functionName:string, fileNameLine:string, url:string):StackInfo { - this.functionName = functionName; - this.fileNameLine = fileNameLine; - this.url = url; - return this; - } - - /** - * 计算文件名 - */ - public calcFileName():string | undefined { - - let replaceToTs = this.fileNameLine?.replace(".js", ".ts"); - let matched = replaceToTs?.match(/^(.+\.(js|ts)):\d+:\d+$/); - - return matched ? matched[1] : undefined; - } - - /** - * 计算路径名 - */ - public calcPathName():string | undefined { - - let replaceToTs = this.url?.replace(".js", ".ts"); - let matched = replaceToTs?.match(/^https?:\/\/(\d+\.){3}\d+:\d+\/(.+):\d+:\d+$/); - - return matched ? matched[2] : undefined; - } - - /** - * 获取函数调用栈列表 - */ - public static getCallStack():StackInfo[] { - - // 获取堆栈信息 - let stack:string | undefined = new Error().stack; - - if (stack === void 0) return []; - - // 去除 Error - stack = stack.replace(/^(Error)\s/, ""); - - // 获取堆栈信息 - let stackList:string[] = stack.split(/\n/); - - let callStack:StackInfo[] = []; - - for(let i = 0; i < stackList.length; i++) { - - let matcher = stackList[i].match(/^\s+at\s+(.+)\s(\(.+\))/); - if (matcher === null || matcher.length < 3) continue; - - let fileName = matcher[2].match(/.+\/(.+\..+:\d+:\d+)\)/); - if (fileName === null || matcher.length < 2) continue; - - callStack.push(new StackInfo().setInfo( - matcher[1], fileName[1], matcher[2]?.replace(/(\(|\))/g, "") - )) - } - - // console.log(callStack); - - return callStack; - } - - /** - * 排除的 - */ - public static readonly excludeFile:RegExp = /^.*(\\|\/)core(\\|\/)(.*Log.*).js:\d+:\d+/; - - /** - * 获取第一个调用栈 - */ - public static getFirstStack():StackInfo | undefined { - - let callStack = this.getCallStack(); - - for(let i = 0; i < callStack.length; i++) { - - if(!callStack[i].url) continue; - - if(!StackInfo.excludeFile.test(callStack[i].url ?? "")) { - return callStack[i]; - } - } - - return; - } -} - -/** - * 内部预定义的 LogLabel - */ -class StackLogLabel { - - /** - * 堆栈路径样式 - */ - public static readonly normalStyle:LogStyle = new LogStyle() - .setColor("#979797").setBorder("4px", "1px solid #979797").setBlank("0 5px"); - - /** - * 一个回车 - */ - public static readonly blankLabel = new LogLabel("\n\r", - new LogStyle(), false, true, true); - - /** - * 包含文件名和行号的 label - */ - public static get fileNameLabel():LogLabel { - - // 获得调用堆栈 - let stack = StackInfo.getFirstStack(); - - return new LogLabel(stack?.calcFileName() ?? "Unknown file name", - StackLogLabel.normalStyle, false, true, true); - } - - /** - * 包含 URL 链接的 label - */ - public static get urlLabel():LogLabel { - - // 获得调用堆栈 - let stack = StackInfo.getFirstStack(); - - return new LogLabel(stack?.calcPathName() ?? "Unknown url", - StackLogLabel.normalStyle, false, true, true); - } - - /** - * 仅仅用来 filter 的 URL 链接的 label - */ - public static get filterUrlLabel():LogLabel { - - // 获得调用堆栈 - let stack = StackInfo.getFirstStack(); - - return new LogLabel(stack?.calcPathName() ?? "Unknown url", - StackLogLabel.normalStyle, true, false, true); - } - -} - -/** - * 生成圆角颜色标签样式 - */ -const normalLevelStyleGen = (color:string):LogStyle => { - return new LogStyle().setBorder("4px", `1px solid ${color}`) - .setColor(color).setBlank("0 5px"); -} - -/** - * 调试等级标签 - */ -class LevelLogLabel { - - /** - * 致命 - */ - static readonly FatalLabel = new LogLabel( - "FATAL", normalLevelStyleGen("#FF00CC") - ); - - /** - * 错误 - */ - static readonly ErrorLabel = new LogLabel( - "ERROR", normalLevelStyleGen("#FF0000") - ); - - /** - * 警告 - */ - static readonly WarnLabel = new LogLabel( - "WARN", normalLevelStyleGen("#FF9900") - ); - - /** - * 消息 - */ - static readonly InfoLabel = new LogLabel( - "INFO", normalLevelStyleGen("#99FF00") - ); - - /** - * 调试 - */ - static readonly DebugLabel = new LogLabel( - "DEBUG", normalLevelStyleGen("#00FF99") - ); - - /** - * 追踪 - */ - static readonly TraceLabel = new LogLabel( - "TRACE", normalLevelStyleGen("#00CCFF") - ); -} - -/** - * 生成圆角颜色标签样式 - */ -const normalLifeStyleGen = (r:number, g:number, b:number):LogStyle => { - return new LogStyle().setBorder("4px", `1px solid rgb(${ r }, ${ g }, ${ b })`) - .setColor(`rgb(${ r }, ${ g }, ${ b })`, `rgba(${ r }, ${ g }, ${ b }, .1)`) - .setBlank("0 5px"); -} - -/** - * 生命周期标签 - */ -class LifeCycleLogLabel { - - /** - * 小程序加载时 - */ - static readonly OnLaunchLabel = new LogLabel( - "onLaunch", normalLifeStyleGen(160, 32, 240) - ); - - /** - * 生命周期函数--监听页面加载 - */ - static readonly OnLoadLabel = new LogLabel( - "onLoad", normalLifeStyleGen(255, 140, 105) - ); - - /** - * 生命周期函数--监听页面初次渲染完成 - */ - static readonly OnReadyLabel = new LogLabel( - "onReady", normalLifeStyleGen(255, 127, 36) - ); - - /** - * 生命周期函数--监听页面显示 - */ - static readonly OnShowLabel = new LogLabel( - "onShow", normalLifeStyleGen(255, 215, 0) - ) - - /** - * 生命周期函数--监听页面隐藏 - */ - static readonly OnHideLabel = new LogLabel( - "onHide", normalLifeStyleGen(173, 255, 47) - ); - - /** - * 生命周期函数--监听页面卸载 - */ - static readonly OnUnloadLabel = new LogLabel( - "onUnload", normalLifeStyleGen(127, 255, 212) - ); - - /** - * 页面相关事件处理函数--监听用户下拉动作 - */ - static readonly OnPullDownRefreshLabel = new LogLabel( - "onPullDownRefresh", normalLifeStyleGen(0, 191, 255) - ); - - /** - * 页面上拉触底事件的处理函数 - */ - static readonly OnReachBottomLabel = new LogLabel( - "onReachBottom", normalLifeStyleGen(84, 255, 159) - ); - - /** - * 用户点击右上角分享 - */ - static readonly OnShareAppMessageLabel = new LogLabel( - "onShareAppMessage", normalLifeStyleGen(147, 112, 219) - ); -} - -class StatusLabel { - - /** - * 等待 - */ - static readonly Pending = new LogLabel( - "◉", new LogStyle().setBlank("0 2px").setBorder("1000px", "1px solid lightblue").setColor("lightblue") - ); - - /** - * 成功 - */ - static readonly Success = new LogLabel( - "√", new LogStyle().setBlank("0 4px").setBorder("1000px", "1px solid lightgreen").setColor("lightgreen") - ); - - /** - * 失败 - */ - static readonly Failed = new LogLabel( - "×", new LogStyle().setBlank("0 4px").setBorder("1000px", "1px solid red").setColor("red") - ); -} - -const NormalStyle = StackLogLabel.normalStyle; - -export { - NormalStyle, - StackInfo, - StackLogLabel, - StatusLabel, - LevelLogLabel, - LifeCycleLogLabel, - normalLifeStyleGen as colorRadio, -}; \ No newline at end of file diff --git a/miniprogram/core/Storage.ts b/miniprogram/core/Storage.ts index 7fc07cf..1d36a19 100644 --- a/miniprogram/core/Storage.ts +++ b/miniprogram/core/Storage.ts @@ -1,6 +1,4 @@ -import { LogLabel } from "./LogLabel"; -import { Logger } from "./Logger"; -import { LevelLogLabel, colorRadio } from "./PresetLogLabel"; +import { Logger, LogLabel, LevelLogLabel, colorRadio } from "./Logger"; /** * 状态 diff --git a/miniprogram/core/TestCase.ts b/miniprogram/core/TestCase.ts index 499324d..42e0e3b 100644 --- a/miniprogram/core/TestCase.ts +++ b/miniprogram/core/TestCase.ts @@ -1,5 +1,5 @@ // import { Logger } from "../logger/Logger"; -import { LogStyle, LogLabel } from "./LogLabel"; +import { LogStyle, LogLabel } from "./Logger"; /** * 测试结果 diff --git a/miniprogram/pages/Timetable/StatusBar.ts b/miniprogram/pages/Timetable/StatusBar.ts index 1a9812d..d248d9a 100644 --- a/miniprogram/pages/Timetable/StatusBar.ts +++ b/miniprogram/pages/Timetable/StatusBar.ts @@ -1,7 +1,5 @@ import { Modular, Manager, ILifetime } from "../../core/Module"; -import { LevelLogLabel, LifeCycleLogLabel, NormalStyle } from "../../core/PresetLogLabel"; -import { LogLabel } from "../../core/LogLabel"; -import { Logger } from "../../core/Logger"; +import { Logger, LogLabel, LevelLogLabel, LifeCycleLogLabel, NormalStyle } from "../../core/Logger"; /** * 在 UI 中显示的数据