index.uts 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/* 引入 interface.uts 文件中定义的变量 */
import { GetLog } from '../interface.uts';
import File from 'java.io.File';


export const getLog : GetLog = function (param : Any|null) : string {
  let allLog = console.getLogV2(param);
  /**
   * 移除掉干扰因素,仅返回打印对象本身的序列化结果
   */
  if(!allLog.startsWith("---BEGIN:CONSOLE---")){
	  return ""
  }
  if(!allLog.endsWith("---END:CONSOLE---")){
  	  return ""
  }
  let jsonLog = allLog
  jsonLog = jsonLog.replace("---BEGIN:CONSOLE---", "")
  jsonLog = jsonLog.replace("---END:CONSOLE---", "")
  let jsonArrayObj = JSON.parse<UTSArray<UTSJSONObject>>(jsonLog)
  return jsonArrayObj![0].toJSONString()
}

type ParamOptions = {
	title : string,
	array : Array<string>
}

class C {
  name:string = "default"
  sayBye = function(word:string){
    console.log("sayBye",word)
  }
  sayHello(word:string){
    console.log("sayHello",word)
  }
}

export function logObjectTest():string{
	let ret : ParamOptions = {
		title: "logObjectTest",
		array: ['1', '2', '3']
	}
  let typeLogRet = getLog(ret)
  let typeLogObj = JSON.parseObject(typeLogRet)!
  
  let typeLogObjPos = typeLogObj.getJSON("__$originalPosition")!
  if("ParamOptions" != typeLogObjPos['name']){
    return ""
  }
  /**
   * 编译出来的位置信息可能有差异,排除单独验证后,排除掉这个字段
   */
  typeLogObj.set("__$originalPosition","")
	return typeLogObj.toJSONString()
}


export function logFunctionTest():string{
	
	let testFun = function(){
		console.log("testFun")
	}
	return getLog(testFun)
}

export function logFileTest():string{
  return getLog(new File("/sdcard/temp/1.txt"))
}

export function logDateTest():string{
  return getLog(new Date('1998-08-08'))
}

export function logClassTest():string{
  let c = new C()
  c.name="ccc"
  
  let classLogRet = getLog(c)
  let classLogObj = JSON.parseObject(classLogRet)!
  
  let classLogObjPos = classLogObj.getJSON("__$originalPosition")!
  
  if("C" != classLogObjPos['name']){
    return ""
  }
  /**
   * 编译出来的位置信息可能有差异,排除单独验证后,排除掉这个字段
   */
  classLogObj.set("__$originalPosition","")
  return classLogObj.toJSONString()
}