123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /**
- * /* eslint-disable no-console
- *
- * @format
- */
- import {ConstValue} from '../data/ConstValue'
- // tslint:disable: no-console
- enum LogType {
- normal = 1,
- info,
- warn,
- err,
- }
- class LogUtils {
- public static getDateString(): string {
- const d = new Date()
- let str = d.getHours().toString()
- let timeStr = ''
- timeStr += `${str.length === 1 ? `0${str}` : str}:`
- str = d.getMinutes().toString()
- timeStr += `${str.length === 1 ? `0${str}` : str}:`
- str = d.getSeconds().toString()
- timeStr += `${str.length === 1 ? `0${str}` : str}:`
- str = d.getMilliseconds().toString()
- if (str.length === 1) {
- str = `00${str}`
- }
- if (str.length === 2) {
- str = `0${str}`
- }
- timeStr += str
- timeStr = `[${timeStr}]`
- return timeStr
- }
- public static stack(index): string {
- const e = new Error()
- const lines = e.stack.split('\n')
- lines.shift()
- const result = []
- lines.forEach((lineArg: string) => {
- const line = lineArg.substring(7)
- const lineBreak = line.split(' ')
- if (lineBreak.length < 2) {
- result.push(lineBreak[0])
- } else {
- result.push(lineBreak[1])
- }
- })
- if (result.length > 0 && result[index]) {
- const list = result[index]
- const splitList = list.split('/')
- return `${splitList[splitList.length - 1].replace(')', '')}->: `
- } else {
- return ''
- }
- }
- public static getFormatStr(args: any[]): string {
- if (!args) return
- let temp = '%c'
- for (let i = 0; i < args.length; i++) {
- if (typeof args[i] == 'object') {
- temp += '%o , '
- } else {
- args[i] = args[i]?.toString()
- temp += '%s , '
- }
- }
- temp += '%s%s'
- return temp
- }
- public curLogLevel: LogType = LogType.normal
- public log(...args) {
- console.log(...args)
- }
- public info(...args) {
- const backLog = console.log || cc.log // || log;
- if (this.curLogLevel <= LogType.info) {
- backLog.call(
- this,
- LogUtils.getFormatStr(args),
- 'color:#000000;',
- ...args,
- LogUtils.stack(2),
- LogUtils.getDateString(),
- )
- }
- }
- public warn(...args) {
- const backLog = console.log || cc.log // || log;
- if (this.curLogLevel <= LogType.warn) {
- backLog.call(
- this,
- LogUtils.getFormatStr(args),
- 'color:#ee7700;',
- ...args,
- LogUtils.stack(2),
- LogUtils.getDateString(),
- )
- }
- }
- public error(...args) {
- const backLog = console.log || cc.log // || log;
- if (this.curLogLevel <= LogType.err) {
- backLog.call(
- this,
- LogUtils.getFormatStr(args),
- 'color:red',
- ...args,
- LogUtils.stack(2),
- LogUtils.getDateString(),
- )
- }
- }
- /**
- * 网络协议日志
- * @param args
- */
- public net(...args) {
- const backLog = console.log || cc.log // || log;
- if (this.curLogLevel <= LogType.info) {
- backLog.call(
- this,
- LogUtils.getFormatStr(args),
- 'color:#00CD00;',
- ...args,
- LogUtils.stack(2),
- LogUtils.getDateString(),
- )
- }
- }
- }
- export let Log = new LogUtils()
- if (ConstValue.DEBUG) {
- console.log('test')
- Log.curLogLevel = LogType.normal
- } else {
- Log.curLogLevel = LogType.info
- }
|