245 lines
5.9 KiB
JavaScript
245 lines
5.9 KiB
JavaScript
"use strict";
|
|
cc._RF.push(module, '8fe04nPXdREIIqC3WPQ7ho2', 'nx.log');
|
|
// Scripts/nx/kernel/nx.log.js
|
|
|
|
"use strict";
|
|
|
|
/*******************************************************************************
|
|
*
|
|
* Nx日志管理器
|
|
*
|
|
*
|
|
*
|
|
* 2021.12.10
|
|
******************************************************************************/
|
|
|
|
// 日志等级
|
|
var LOG_LEVEL = {
|
|
All: 0,
|
|
// 全部
|
|
Info: 1,
|
|
// 普通+警告+错误
|
|
Warn: 2,
|
|
// 警告+错误
|
|
Error: 3 // 错误
|
|
};
|
|
|
|
// 美化颜色
|
|
var PCS = ['color: #8F8F8F', 'color: #009C9C', 'color: #Violet', 'color: #FF0000'];
|
|
|
|
// 等级前缀
|
|
var LEVEL_KEYS = ["LOG", "LOG", "WARN", "ERROR"];
|
|
|
|
// 缓存键值
|
|
var LOG_CACHE = "LOG_CACHE";
|
|
|
|
// 缓存条数
|
|
var CACHE_LIMIT = 100;
|
|
|
|
// 路由
|
|
var UPLOAD_ROUTER = "api/upload/save";
|
|
var NxLogger = cc.Class({
|
|
name: "NxLogger",
|
|
// 创建
|
|
ctor: function ctor() {
|
|
this.level = LOG_LEVEL.All;
|
|
this.upURL = "";
|
|
this.stUpload = 0;
|
|
|
|
// 缓存拉取
|
|
this.cache = [];
|
|
// if( cc.sys.localStorage ) {
|
|
// var s = cc.sys.localStorage.getItem( LOG_CACHE );
|
|
// if( typeof s == "string" && s != "" ) {
|
|
// this.cache = nx.dt.dejson( s );
|
|
// }
|
|
// }
|
|
|
|
// 接受浏览器消息
|
|
var self = this;
|
|
if (cc.sys.isBrowser && window) {
|
|
window.addEventListener("unload", function () {
|
|
self.saveCaches();
|
|
});
|
|
}
|
|
|
|
// 全局服务开启
|
|
this.setLevel(CC_DEBUG ? 0 : 2);
|
|
},
|
|
// 销毁
|
|
destroy: function destroy() {
|
|
// 缓存
|
|
this.saveCaches();
|
|
},
|
|
// 设置等级
|
|
setLevel: function setLevel(_lv) {
|
|
this.level = _lv;
|
|
if (cc.debug) {
|
|
var dL = _lv == LOG_LEVEL.All ? 1 : 2;
|
|
cc.debug._resetDebugSetting(dL);
|
|
}
|
|
},
|
|
// 调试
|
|
debug: function debug() {
|
|
this._output(LOG_LEVEL.All, this.formatS(arguments));
|
|
},
|
|
// 普通
|
|
info: function info() {
|
|
this._output(LOG_LEVEL.Info, this.formatS(arguments));
|
|
},
|
|
// 警告
|
|
warn: function warn() {
|
|
this._output(LOG_LEVEL.Warn, this.formatS(arguments));
|
|
},
|
|
// 错误
|
|
error: function error() {
|
|
this._output(LOG_LEVEL.Error, this.formatS(arguments));
|
|
},
|
|
// 输出日志
|
|
_output: function _output(_type, _text) {
|
|
// let head = LEVEL_KEYS[_type] || LEVEL_KEYS[0];
|
|
// let text = cc.js.formatStr( "%s[%s]%s", this.getTime(), head, _text );
|
|
var text = cc.js.formatStr("%s%s", this.getTime(), _text);
|
|
|
|
// 缓存
|
|
if (_type != LOG_LEVEL.All) {
|
|
// 长度剪裁
|
|
var _short = "";
|
|
var len = text.length;
|
|
if (len > 100) {
|
|
_short = text.substr(0, 100) + ("...(len" + len + ")");
|
|
} else {
|
|
_short = text;
|
|
}
|
|
this.cache.push(_short);
|
|
if (this.cache.length > CACHE_LIMIT) {
|
|
this.cache.splice(0, CACHE_LIMIT / 2);
|
|
}
|
|
}
|
|
|
|
// 过滤
|
|
if (_type < this.level) {
|
|
return;
|
|
}
|
|
|
|
// 输出
|
|
switch (_type) {
|
|
case 2:
|
|
cc.warn(text);
|
|
break;
|
|
case 3:
|
|
cc.error(text);
|
|
break;
|
|
default:
|
|
cc.log("%c" + text, PCS[_type]);
|
|
break;
|
|
}
|
|
},
|
|
// 日志保存
|
|
saveCaches: function saveCaches() {
|
|
// if( cc.sys.localStorage ) {
|
|
// cc.sys.localStorage.setItem( LOG_CACHE, nx.dt.enjson( this.cache ) );
|
|
// }
|
|
},
|
|
// 是否存在错误日志
|
|
haveError: function haveError() {
|
|
for (var i = 0; i < this.cache.length; ++i) {
|
|
var line = this.cache[i];
|
|
if (line.substr(0, 7) == "[ERROR]") {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
},
|
|
// 上传日志
|
|
bugUpload: function bugUpload(_url, _error) {
|
|
var _this = this;
|
|
// 间隔十分钟可以上传一次
|
|
var dt = Date.now() - this.stUpload;
|
|
if (dt < 600000) {
|
|
console.log("上传日志太频繁,这次忽略...");
|
|
return;
|
|
}
|
|
|
|
// 上传地址无效
|
|
var url = _url || this.upURL;
|
|
if (nx.dt.strEmpty(url)) {
|
|
console.warn("上传日志失败,地址为空...");
|
|
return;
|
|
}
|
|
|
|
// 数据归纳
|
|
var to = Math.max(0, this.cache.length - 1);
|
|
var from = Math.max(0, to - 30);
|
|
var params = {
|
|
logs: this.cache.slice(from, to),
|
|
bugs: _error || "none"
|
|
};
|
|
|
|
// 上传
|
|
var data = nx.dt.enjson(params);
|
|
nx.web.upload(url, {}, data, function (_code, _params) {
|
|
console.log("BUG提交完毕." + _code);
|
|
_this.stUpload = Date.now();
|
|
});
|
|
},
|
|
// 获取时间
|
|
getTime: function getTime() {
|
|
var f2 = function f2(v) {
|
|
return v < 10 ? "0" + v : v;
|
|
};
|
|
var f3 = function f3(v) {
|
|
if (v < 10) {
|
|
return "00" + v;
|
|
}
|
|
return v < 100 ? "0" + v : v;
|
|
};
|
|
var d = new Date();
|
|
var str = cc.js.formatStr("[%s:%s:%s:%s]",
|
|
// d.getMonth() + 1,
|
|
// d.getDate(),
|
|
f2(d.getHours()), f2(d.getMinutes()), f2(d.getSeconds()), f3(d.getMilliseconds()));
|
|
return str;
|
|
},
|
|
// 直接格式化
|
|
formatS: function formatS(args) {
|
|
var REGEXP_NUM_OR_STR = /(%d)|(%s)/;
|
|
var REGEXP_STR = /%s/;
|
|
var argLen = args.length;
|
|
if (0 === argLen) {
|
|
return "";
|
|
}
|
|
var msg = args[0];
|
|
if (typeof msg != "string" || msg == "") {
|
|
// cc.warn( "取文本失败,参数无效:" + msg );
|
|
return "";
|
|
}
|
|
if (1 === argLen) {
|
|
return "" + msg;
|
|
}
|
|
var hasSubstitution = "string" === typeof msg && REGEXP_NUM_OR_STR.test(msg);
|
|
if (hasSubstitution) {
|
|
for (var i = 1; i < argLen; ++i) {
|
|
var arg = args[i];
|
|
var regExpToTest = "number" === typeof arg ? REGEXP_NUM_OR_STR : REGEXP_STR;
|
|
regExpToTest.test(msg) ? msg = msg.replace(regExpToTest, arg) : msg += " " + arg;
|
|
}
|
|
} else {
|
|
for (var _i = 1; _i < argLen; ++_i) {
|
|
msg += " " + args[_i];
|
|
}
|
|
}
|
|
return msg;
|
|
},
|
|
// JS异常输出
|
|
onExceptionJS: function onExceptionJS(_loc, _message, _stack) {
|
|
this._output(1, "EXCEPTION:");
|
|
this._output(2, " :" + _message);
|
|
this._output(1, " :" + _loc);
|
|
}
|
|
});
|
|
|
|
// 模块导出
|
|
module.exports = NxLogger;
|
|
|
|
cc._RF.pop(); |