Log.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. var t = require;
  2. var e = module;
  3. var i =
  4. (this && this.__awaiter) ||
  5. function (t, n, r, l) {
  6. return new (r = r || Promise)(function (i, e) {
  7. function s(t) {
  8. try {
  9. a(l.next(t));
  10. } catch (t) {
  11. e(t);
  12. }
  13. }
  14. function o(t) {
  15. try {
  16. a(l.throw(t));
  17. } catch (t) {
  18. e(t);
  19. }
  20. }
  21. function a(t) {
  22. var e;
  23. t.done
  24. ? i(t.value)
  25. : ((e = t.value) instanceof r
  26. ? e
  27. : new r(function (t) {
  28. t(e);
  29. })
  30. ).then(s, o);
  31. }
  32. a((l = l.apply(t, n || [])).next());
  33. });
  34. };
  35. Date.prototype.Format = function (t) {
  36. var e,
  37. i = {
  38. "M+": this.getMonth() + 1,
  39. "d+": this.getDate(),
  40. "h+": this.getHours(),
  41. "m+": this.getMinutes(),
  42. "s+": this.getSeconds(),
  43. "q+": Math.floor((this.getMonth() + 3) / 3),
  44. S: this.getMilliseconds()
  45. };
  46. for (e in (/(y+)/.test(t) && (t = t.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length))), i))
  47. new RegExp("(" + e + ")").test(t) &&
  48. (t = t.replace(RegExp.$1, 1 == RegExp.$1.length ? i[e] : ("00" + i[e]).substr(("" + i[e]).length)));
  49. return t;
  50. };
  51. class n {
  52. constructor() {
  53. (this.max_size = 512e3),
  54. (this.str = ""),
  55. (this.warnStr = ""),
  56. (this._date = new Date()),
  57. (this.saveIndex = 0),
  58. (this.maxCache = 10);
  59. var t = cc.sys.localStorage.getItem("saveIndex");
  60. t &&
  61. ((this.saveIndex = parseInt(t)),
  62. (t = "logCache" + this.getCurIndex()),
  63. (t = cc.sys.localStorage.getItem(t)) &&
  64. (this.str = `${t}\n\n/--------------------\u767b\u5f55${this._date.Format("MM-dd hh:mm:ss: ")}----/`)),
  65. (n._instance = this).init();
  66. }
  67. static getInstance() {
  68. return this._instance;
  69. }
  70. static trace(t, ...e) {
  71. console.log(t, ...e);
  72. }
  73. static warn(t, ...e) {
  74. console.warn(t, ...e);
  75. }
  76. static error(t, ...e) {
  77. cc.error(t, ...e);
  78. }
  79. static debug(t) {}
  80. onOutputArchive(t) {
  81. var e = document.createElement("a");
  82. (e.download = new Date().Format("错误日志-(MM-dd hh:mm:ss)") + ".log"), (e.style.display = "none");
  83. t = new Blob([JSON.stringify(t, null, 4)]);
  84. (e.href = URL.createObjectURL(t)), document.body.appendChild(e), e.click(), document.body.removeChild(e);
  85. }
  86. sendToNative(o) {
  87. return i(this, void 0, void 0, function* () {
  88. let t = cc.sys.localStorage.getItem("bdlm_game");
  89. t || ((t = Date.now() + "" + Math.floor(1e5 * Math.random())), cc.sys.localStorage.setItem("bdlm_game", t));
  90. let e = `/-------------\u7cbe\u7075\u5408\u6210 id:${t}, pId:${o}-----------/`;
  91. if (0 <= this.saveIndex) {
  92. for (let t = Math.max(this.saveIndex - this.maxCache + 1, 0); t <= this.saveIndex; t++) {
  93. var i = "logCache" + (t % this.maxCache),
  94. s = cc.sys.localStorage.getItem(i);
  95. s && ((e += s), cc.sys.localStorage.setItem(i, ""));
  96. }
  97. this.saveIndex = 0;
  98. }
  99. return this.onOutputArchive(e), (this.str = ""), t;
  100. });
  101. }
  102. requestAsync(t, e, a = 1e4) {
  103. return new Promise((i, s) => {
  104. var o = new XMLHttpRequest();
  105. (o.timeout = a),
  106. (o.onerror = t => {
  107. s(t), (s = null);
  108. }),
  109. (o.ontimeout = () => {
  110. s("请求超时"), (s = null);
  111. }),
  112. (o.onreadystatechange = function () {
  113. var t, e;
  114. 4 == o.readyState &&
  115. ((t = 400 <= o.status || 0 == o.status),
  116. (e = o.responseText),
  117. t ? (n.error(o.status), s(o.status)) : i(e));
  118. }),
  119. o.open("POST", t, !0),
  120. o.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"),
  121. o.send(e);
  122. });
  123. }
  124. onConnect() {
  125. this._log("onConnect"), (this.isConnect = !0);
  126. }
  127. onClose() {}
  128. onError() {
  129. this._log("onError");
  130. }
  131. onSocketData(t) {}
  132. initWebSocket() {}
  133. static init() {}
  134. init() {
  135. {
  136. let t = function (t, e, i) {
  137. console.log("错误");
  138. i = JSON.stringify({file: e, line: i, err: t}, null, 4);
  139. return (
  140. n._instance.addLog(i),
  141. console.error("捕捉到错误", t),
  142. s.cacheCurLog(),
  143. alert("捕捉到错误!!!!!,请联系开发者"),
  144. !0
  145. );
  146. },
  147. s = this;
  148. cc.sys.isNative
  149. ? (jsb.onError((t, e, i) => {
  150. i = JSON.stringify({line: t, err: e, file: i}, null, 4);
  151. return n._instance.addLog(i), s.cacheCurLog(), !0;
  152. }),
  153. (window.__errorHandler = function (t, e, i) {
  154. i = JSON.stringify({file: t, line: e, err: i}, null, 4);
  155. return n._instance.addLog(i), s.cacheCurLog(), !0;
  156. }))
  157. : ((window.onerror = t),
  158. window.addEventListener("unhandledrejection", function (t) {
  159. t = JSON.stringify({line: t.reason.message, err: t.reason.stack}, null, 4);
  160. return n._instance.addLog(t), cc.error(t), alert("捕捉到错误!!!!!,请联系开发者"), !0;
  161. }));
  162. }
  163. cc.game.on(cc.game.EVENT_HIDE, this.onHide, this), cc.game.on(cc.game.EVENT_SHOW, this.onShow, this);
  164. }
  165. onHide() {
  166. this.cacheCurLog();
  167. }
  168. onShow() {}
  169. sendLog(t) {
  170. this._date.setTime(Date.now()),
  171. this.requestAsync(
  172. "https://www.gzqidong.cn/test/SaveLog1.php",
  173. `info=${encodeURIComponent(this._date.Format("hh:mm:ss: ") + t)}`
  174. );
  175. }
  176. getCurIndex() {
  177. return this.saveIndex % this.maxCache;
  178. }
  179. cacheCurLog() {
  180. console.log("缓存log"),
  181. cc.sys.localStorage.setItem("logCache" + this.getCurIndex(), this.str),
  182. cc.sys.localStorage.setItem("saveIndex", this.saveIndex + "");
  183. }
  184. addLog(t) {}
  185. }
  186. window.Log = n;