/******************************************************************************* * * Nx日志管理器 * * * * 2021.12.10 ******************************************************************************/ // 日志等级 const LOG_LEVEL = { All: 0, // 全部 Info: 1, // 普通+警告+错误 Warn: 2, // 警告+错误 Error: 3 // 错误 }; // 美化颜色 const PCS = [ 'color: #8F8F8F', 'color: #009C9C', 'color: #Violet', 'color: #FF0000', ]; // 等级前缀 const LEVEL_KEYS = [ "LOG", "LOG", "WARN", "ERROR" ]; // 缓存键值 const LOG_CACHE = "LOG_CACHE"; // 缓存条数 const CACHE_LIMIT = 100; // 路由 const UPLOAD_ROUTER = "api/upload/save"; var NxLogger = cc.Class( { name: "NxLogger", // 创建 ctor: function() { 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", () => { self.saveCaches(); } ); } // 全局服务开启 this.setLevel( CC_DEBUG ? 0 : 2 ); }, // 销毁 destroy: function() { // 缓存 this.saveCaches(); }, // 设置等级 setLevel: function( _lv ) { this.level = _lv; if( cc.debug ) { let dL = ( _lv == LOG_LEVEL.All ) ? 1 : 2; cc.debug._resetDebugSetting( dL ) } }, // 调试 debug: function() { this._output( LOG_LEVEL.All, this.formatS( arguments ) ); }, // 普通 info: function() { this._output( LOG_LEVEL.Info, this.formatS( arguments ) ); }, // 警告 warn: function() { this._output( LOG_LEVEL.Warn, this.formatS( arguments ) ); }, // 错误 error: function() { this._output( LOG_LEVEL.Error, this.formatS( arguments ) ); }, // 输出日志 _output: function( _type, _text ) { // let head = LEVEL_KEYS[_type] || LEVEL_KEYS[0]; // let text = cc.js.formatStr( "%s[%s]%s", this.getTime(), head, _text ); let text = cc.js.formatStr( "%s%s", this.getTime(), _text ); // 缓存 if( _type != LOG_LEVEL.All ) { // 长度剪裁 let short = ""; let 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() { // if( cc.sys.localStorage ) { // cc.sys.localStorage.setItem( LOG_CACHE, nx.dt.enjson( this.cache ) ); // } }, // 是否存在错误日志 haveError: function() { for( let i = 0; i < this.cache.length; ++i ) { var line = this.cache[ i ]; if( line.substr( 0, 7 ) == "[ERROR]" ) { return true; } } return false; }, // 上传日志 bugUpload: function( _url, _error ) { // 间隔十分钟可以上传一次 let dt = Date.now() - this.stUpload; if( dt < 600000 ) { console.log( "上传日志太频繁,这次忽略..." ); return; } // 上传地址无效 let url = _url || this.upURL; if( nx.dt.strEmpty( url ) ) { console.warn( "上传日志失败,地址为空..." ); return; } // 数据归纳 let to = Math.max( 0, this.cache.length - 1 ); let from = Math.max( 0, to - 30 ); let params = { logs: this.cache.slice( from, to ), bugs: _error || "none", } // 上传 let data = nx.dt.enjson( params ); nx.web.upload( url, {}, data, ( _code, _params ) => { console.log( "BUG提交完毕." + _code ); this.stUpload = Date.now(); } ); }, // 获取时间 getTime: function() { var f2 = function( v ) { return ( v < 10 ) ? ( "0" + v ) : v; }; var f3 = function( 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( 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( _loc, _message, _stack ) { this._output( 1, "EXCEPTION:" ); this._output( 2, " :" + _message ); this._output( 1, " :" + _loc ); }, } ); // 模块导出 module.exports = NxLogger;