头部新增的方法.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. function printStack(){
  2. const stack = new Error().stack;
  3. console.log("调用堆栈:",stack);
  4. }
  5. const originalConsoleLog = console.log;
  6. function isHighSurrogate(codeUnit) {
  7. return codeUnit >= 0xD800 && codeUnit <= 0xDBFF;
  8. }
  9. function isLowSurrogate(codeUnit) {
  10. return codeUnit >= 0xDC00 && codeUnit <= 0xDFFF;
  11. }
  12. function splitAndPrintString(str, maxSize) {
  13. // if(str.indexOf(`{"optionInfo":[{"amount`)!=-1){
  14. // printStack();
  15. // }
  16. let strLength = str.length;
  17. for (let i = 0; i < strLength; i += maxSize) {
  18. let end = i + maxSize;
  19. // 确保不要在代理对中间拆分
  20. if (end < str.length && isHighSurrogate(str.charCodeAt(end - 1)) && isLowSurrogate(str.charCodeAt(end))) {
  21. end++;
  22. }
  23. if (strLength > maxSize) {//超过1页
  24. if (i === 0) { // 第一段
  25. originalConsoleLog(str.slice(i, end), "__>>__");
  26. } else if (end >= strLength) { // 最后一段
  27. originalConsoleLog("__<<__", str.slice(i, end));
  28. } else { // 中间的段
  29. originalConsoleLog("__<<__", str.slice(i, end), "__>>__");
  30. }
  31. } else {
  32. originalConsoleLog(str.slice(i, end));
  33. }
  34. }
  35. }
  36. //重新定义console.log方法
  37. console.log = function (...args) {
  38. const maxChunkSize = 800; // 设定最大字符数,可根据需要调整
  39. // 将所有参数转换为字符串并拼接
  40. const combinedString = args.map(arg => {
  41. if (typeof arg === 'object') {
  42. try {
  43. return JSON.stringify(arg);
  44. } catch (e) {
  45. return "啊啊啊啊啊,解析失败了" + e.message;
  46. }
  47. } else {
  48. return String(arg);
  49. }
  50. }).join(' ');
  51. // 分段打印处理过的字符串
  52. splitAndPrintString(combinedString, maxChunkSize);
  53. };