LogUtils.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * /* eslint-disable no-console
  3. *
  4. * @format
  5. */
  6. import {ConstValue} from '../data/ConstValue'
  7. // tslint:disable: no-console
  8. enum LogType {
  9. normal = 1,
  10. info,
  11. warn,
  12. err,
  13. }
  14. class LogUtils {
  15. public static getDateString(): string {
  16. const d = new Date()
  17. let str = d.getHours().toString()
  18. let timeStr = ''
  19. timeStr += `${str.length === 1 ? `0${str}` : str}:`
  20. str = d.getMinutes().toString()
  21. timeStr += `${str.length === 1 ? `0${str}` : str}:`
  22. str = d.getSeconds().toString()
  23. timeStr += `${str.length === 1 ? `0${str}` : str}:`
  24. str = d.getMilliseconds().toString()
  25. if (str.length === 1) {
  26. str = `00${str}`
  27. }
  28. if (str.length === 2) {
  29. str = `0${str}`
  30. }
  31. timeStr += str
  32. timeStr = `[${timeStr}]`
  33. return timeStr
  34. }
  35. public static stack(index): string {
  36. const e = new Error()
  37. const lines = e.stack.split('\n')
  38. lines.shift()
  39. const result = []
  40. lines.forEach((lineArg: string) => {
  41. const line = lineArg.substring(7)
  42. const lineBreak = line.split(' ')
  43. if (lineBreak.length < 2) {
  44. result.push(lineBreak[0])
  45. } else {
  46. result.push(lineBreak[1])
  47. }
  48. })
  49. if (result.length > 0 && result[index]) {
  50. const list = result[index]
  51. const splitList = list.split('/')
  52. return `${splitList[splitList.length - 1].replace(')', '')}->: `
  53. } else {
  54. return ''
  55. }
  56. }
  57. public static getFormatStr(args: any[]): string {
  58. if (!args) return
  59. let temp = '%c'
  60. for (let i = 0; i < args.length; i++) {
  61. if (typeof args[i] == 'object') {
  62. temp += '%o , '
  63. } else {
  64. args[i] = args[i].toString()
  65. temp += '%s , '
  66. }
  67. }
  68. temp += '%s%s'
  69. return temp
  70. }
  71. public curLogLevel: LogType = LogType.normal
  72. public log(...args) {
  73. console.log(...args)
  74. }
  75. public info(...args) {
  76. const backLog = console.log || cc.log // || log;
  77. if (this.curLogLevel <= LogType.info) {
  78. backLog.call(
  79. this,
  80. LogUtils.getFormatStr(args),
  81. 'color:#000000;',
  82. ...args,
  83. LogUtils.stack(2),
  84. LogUtils.getDateString(),
  85. )
  86. }
  87. }
  88. public warn(...args) {
  89. const backLog = console.log || cc.log // || log;
  90. if (this.curLogLevel <= LogType.warn) {
  91. backLog.call(
  92. this,
  93. LogUtils.getFormatStr(args),
  94. 'color:#ee7700;',
  95. ...args,
  96. LogUtils.stack(2),
  97. LogUtils.getDateString(),
  98. )
  99. }
  100. }
  101. public error(...args) {
  102. const backLog = console.log || cc.log // || log;
  103. if (this.curLogLevel <= LogType.err) {
  104. backLog.call(
  105. this,
  106. LogUtils.getFormatStr(args),
  107. 'color:red',
  108. ...args,
  109. LogUtils.stack(2),
  110. LogUtils.getDateString(),
  111. )
  112. }
  113. }
  114. /**
  115. * 网络协议日志
  116. * @param args
  117. */
  118. public net(...args) {
  119. const backLog = console.log || cc.log // || log;
  120. if (this.curLogLevel <= LogType.info) {
  121. backLog.call(
  122. this,
  123. LogUtils.getFormatStr(args),
  124. 'color:#00CD00;',
  125. ...args,
  126. LogUtils.stack(2),
  127. LogUtils.getDateString(),
  128. )
  129. }
  130. }
  131. }
  132. export let Log = new LogUtils()
  133. if (ConstValue.DEBUG) {
  134. console.log('test')
  135. Log.curLogLevel = LogType.normal
  136. } else {
  137. Log.curLogLevel = LogType.info
  138. }