c30b6a3023fe9a249c23e0924d3ea27caecdaa0e.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, log, Logger, _crd, LogType, names;
  4. _export("Logger", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. __checkObsolete__ = _cc.__checkObsolete__;
  9. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  10. log = _cc.log;
  11. }],
  12. execute: function () {
  13. _crd = true;
  14. _cclegacy._RF.push({}, "479cdJANP5KaJgU+8z0DdSE", "Logger", undefined);
  15. /** 日志类型 */
  16. __checkObsolete__(['log']);
  17. _export("LogType", LogType = /*#__PURE__*/function (LogType) {
  18. LogType[LogType["Net"] = 1] = "Net";
  19. LogType[LogType["Model"] = 2] = "Model";
  20. LogType[LogType["Business"] = 4] = "Business";
  21. LogType[LogType["View"] = 8] = "View";
  22. LogType[LogType["Config"] = 16] = "Config";
  23. LogType[LogType["Trace"] = 32] = "Trace";
  24. return LogType;
  25. }({}));
  26. names = {
  27. "1": "网络日志",
  28. "2": "数据日志",
  29. "4": "业务日志",
  30. "8": "视图日志",
  31. "16": "配置日志",
  32. "32": "标准日志"
  33. };
  34. /**
  35. * 日志管理
  36. * @help https://gitee.com/dgflash/oops-framework/wikis/pages?sort_id=12037904&doc_id=2873565
  37. * @example
  38. oops.log.trace("默认标准日志");
  39. oops.log.logConfig("灰色配置日志");
  40. oops.log.logNet("橙色网络日志");
  41. oops.log.logModel("紫色数据日志");
  42. oops.log.logBusiness("蓝色业务日志");
  43. oops.log.logView("绿色视图日志");
  44. */
  45. _export("Logger", Logger = class Logger {
  46. constructor() {
  47. this.tags = 0;
  48. this.lc = null;
  49. }
  50. static get instance() {
  51. if (this._instance == null) {
  52. this._instance = new Logger();
  53. this._instance.init();
  54. }
  55. return this._instance;
  56. }
  57. /** 设置界面日志控制台 */
  58. setLoggerConsole(lc) {
  59. this.lc = lc;
  60. }
  61. init() {
  62. this.tags = LogType.Net | LogType.Model | LogType.Business | LogType.View | LogType.Config | LogType.Trace;
  63. }
  64. /**
  65. * 设置显示的日志类型,默认值为不显示任何类型日志
  66. * @example
  67. oops.log.setTags(LogType.View|LogType.Business)
  68. */
  69. setTags(tag = null) {
  70. if (tag) {
  71. this.tags = tag;
  72. }
  73. }
  74. /**
  75. * 记录开始计时
  76. * @param describe 标题描述
  77. * @example
  78. oops.log.start();
  79. ...
  80. 省略N行代码
  81. ...
  82. oops.log.end();
  83. */
  84. start(describe = "Time") {
  85. console.time(describe);
  86. }
  87. /**
  88. * 打印范围内时间消耗
  89. * @param describe 标题描述
  90. * @example
  91. oops.log.start();
  92. ...
  93. 省略N行代码
  94. ...
  95. oops.log.end();
  96. */
  97. end(describe = "Time") {
  98. console.timeEnd(describe);
  99. }
  100. /**
  101. * 打印表格
  102. * @param msg 日志消息
  103. * @param describe 标题描述
  104. * @example
  105. var object:any = {uid:1000, name:"oops"};
  106. oops.log.table(object);
  107. */
  108. table(msg, describe) {
  109. if (!this.isOpen(LogType.Trace)) {
  110. return;
  111. }
  112. console.table(msg);
  113. }
  114. /**
  115. * 打印标准日志
  116. * @param msg 日志消息
  117. */
  118. trace(msg, color = "#000000") {
  119. this.print(LogType.Trace, msg, color);
  120. }
  121. /**
  122. * 打印网络层日志
  123. * @param msg 日志消息
  124. * @param describe 标题描述
  125. */
  126. logNet(msg, describe) {
  127. this.orange(LogType.Net, msg, describe);
  128. }
  129. /**
  130. * 打印数据层日志
  131. * @param msg 日志消息
  132. * @param describe 标题描述
  133. */
  134. logModel(msg, describe) {
  135. this.violet(LogType.Model, msg, describe);
  136. }
  137. /**
  138. * 打印业务层日志
  139. * @param msg 日志消息
  140. * @param describe 标题描述
  141. */
  142. logBusiness(msg, describe) {
  143. this.blue(LogType.Business, msg, describe);
  144. }
  145. /**
  146. * 打印视图日志
  147. * @param msg 日志消息
  148. * @param describe 标题描述
  149. */
  150. logView(msg, describe) {
  151. this.green(LogType.View, msg, describe);
  152. }
  153. /** 打印配置日志 */
  154. logConfig(msg, describe) {
  155. this.gray(LogType.Config, msg, describe);
  156. } // 橙色
  157. orange(tag, msg, describe) {
  158. this.print(tag, msg, "#ee7700", describe);
  159. } // 紫色
  160. violet(tag, msg, describe) {
  161. this.print(tag, msg, "#800080", describe);
  162. } // 蓝色
  163. blue(tag, msg, describe) {
  164. this.print(tag, msg, "#3a5fcd", describe);
  165. } // 绿色
  166. green(tag, msg, describe) {
  167. this.print(tag, msg, "#008000", describe);
  168. } // 灰色
  169. gray(tag, msg, describe) {
  170. this.print(tag, msg, "#808080", describe);
  171. }
  172. isOpen(tag) {
  173. return (this.tags & tag) != 0;
  174. }
  175. /**
  176. * 输出日志
  177. * @param tag 日志类型
  178. * @param msg 日志内容
  179. * @param color 日志文本颜色
  180. * @param describe 日志标题描述
  181. */
  182. print(tag, msg, color, describe) {
  183. // 标记没有打开,不打印该日志
  184. if (!this.isOpen(tag)) {
  185. return;
  186. }
  187. const type = names[tag];
  188. if (this.lc == null) {
  189. const backLog = console.log || log;
  190. color = "color:" + color + ";";
  191. if (describe) {
  192. backLog.call(null, "%c%s%s%s:%s%o", color, this.getDateString(), '[' + type + ']', this.stack(5), describe, msg);
  193. } else {
  194. backLog.call(null, "%c%s%s%s:%o", color, this.getDateString(), '[' + type + ']', this.stack(5), msg);
  195. }
  196. } else {
  197. this.lc.trace(`${this.getDateString()}[${type}]${msg}`, color);
  198. }
  199. }
  200. stack(index) {
  201. const e = new Error();
  202. const lines = e.stack.split("\n");
  203. const result = [];
  204. lines.forEach(line => {
  205. line = line.substring(7);
  206. var lineBreak = line.split(" ");
  207. if (lineBreak.length < 2) {
  208. result.push(lineBreak[0]);
  209. } else {
  210. result.push({
  211. [lineBreak[0]]: lineBreak[1]
  212. });
  213. }
  214. });
  215. let list = [];
  216. let splitList = [];
  217. if (index < result.length - 1) {
  218. let value;
  219. for (let a in result[index]) {
  220. splitList = a.split(".");
  221. if (splitList.length == 2) {
  222. list = splitList.concat();
  223. } else {
  224. value = result[index][a];
  225. const start = value.lastIndexOf("/");
  226. const end = value.lastIndexOf(".");
  227. if (start > -1 && end > -1) {
  228. const r = value.substring(start + 1, end);
  229. list.push(r);
  230. } else {
  231. list.push(value);
  232. }
  233. }
  234. }
  235. }
  236. if (list.length == 1) {
  237. return "[" + list[0] + ".ts]";
  238. } else if (list.length == 2) {
  239. return "[" + list[0] + ".ts->" + list[1] + "]";
  240. }
  241. return "";
  242. }
  243. getDateString() {
  244. let d = new Date();
  245. let str = d.getHours().toString();
  246. let timeStr = "";
  247. timeStr += (str.length == 1 ? "0" + str : str) + ":";
  248. str = d.getMinutes().toString();
  249. timeStr += (str.length == 1 ? "0" + str : str) + ":";
  250. str = d.getSeconds().toString();
  251. timeStr += (str.length == 1 ? "0" + str : str) + ":";
  252. str = d.getMilliseconds().toString();
  253. if (str.length == 1) str = "00" + str;
  254. if (str.length == 2) str = "0" + str;
  255. timeStr += str;
  256. timeStr = "[" + timeStr + "]";
  257. return timeStr;
  258. }
  259. });
  260. Logger._instance = void 0;
  261. _cclegacy._RF.pop();
  262. _crd = false;
  263. }
  264. };
  265. });
  266. //# sourceMappingURL=c30b6a3023fe9a249c23e0924d3ea27caecdaa0e.js.map