Showing
1 changed file
with
61 additions
and
0 deletions
utils/realTimeLog.js
0 → 100644
| 1 | +//ascii 返回长度 | ||
| 2 | +function ascLength(codeat){ | ||
| 3 | + let len=1 | ||
| 4 | + if(codeat>0x4000000){ | ||
| 5 | + len=6 | ||
| 6 | + }else if(codeat>0x200000){ | ||
| 7 | + len=5 | ||
| 8 | + }else if(codeat>65536){ | ||
| 9 | + len=4 | ||
| 10 | + }else if(codeat>2048){ | ||
| 11 | + len=3 | ||
| 12 | + }else{ | ||
| 13 | + len=2 | ||
| 14 | + } | ||
| 15 | + return len | ||
| 16 | +} | ||
| 17 | +//简单计算字节长度 | ||
| 18 | +String.getBlength = function (str) { | ||
| 19 | + for (var i = str.length, n = 0; i--; ) { | ||
| 20 | + n += ascLength(str.charCodeAt(i)); | ||
| 21 | + } | ||
| 22 | + return n; | ||
| 23 | +} | ||
| 24 | +//按指定字节截取字符串 | ||
| 25 | +String.cutByte = function(str,len,endstr){ | ||
| 26 | + var len = +len | ||
| 27 | + ,endstr = typeof(endstr) == 'undefined' ? "..." : endstr.toString(); | ||
| 28 | + function n2(a){ var n = a / 2 | 0; return (n > 0 ? n : 1)} //用于二分法查找 | ||
| 29 | + if(!(str+"").length || !len || len<=0){return "";} | ||
| 30 | + if(this.getBlength(str) <= len){return str;} //整个函数中最耗时的一个判断,欢迎优化 | ||
| 31 | + var lenS = len - this.getBlength(endstr) | ||
| 32 | + ,_lenS = 0 | ||
| 33 | + , _strl = 0 | ||
| 34 | + while (_strl <= lenS){ | ||
| 35 | + var _lenS1 = n2(lenS -_strl) | ||
| 36 | + _strl += this.getBlength(str.substr(_lenS,_lenS1)) | ||
| 37 | + _lenS += _lenS1 | ||
| 38 | + } | ||
| 39 | + return str.substr(0,_lenS-1) + endstr | ||
| 40 | +} | ||
| 41 | +if (wx.getRealtimeLogManager) { | ||
| 42 | + const logger = wx.getRealtimeLogManager() | ||
| 43 | + let consoleLog=console.log | ||
| 44 | + function wlog(){ | ||
| 45 | + let argumentsX=[...arguments] | ||
| 46 | + return new Promise(function (success,fail) { | ||
| 47 | + let params="" | ||
| 48 | + argumentsX.forEach((val,i)=>{ | ||
| 49 | + let str=JSON.stringify(val); | ||
| 50 | + if(!str) return; | ||
| 51 | + params+=str | ||
| 52 | + }) | ||
| 53 | + logger.info(String.cutByte(params,5000)); | ||
| 54 | + success() | ||
| 55 | + }) | ||
| 56 | + } | ||
| 57 | + console.log=function () { | ||
| 58 | + consoleLog(...arguments) | ||
| 59 | + wlog(...arguments); | ||
| 60 | + } | ||
| 61 | +} |
-
Please register or login to post a comment