| 108 | const pathRegex = /(?:(?:file|https?|global code|[^@]+)@)?(?:file:)?((?:\/[^:/]+){2,})(?::(\d+))?(?::(\d+))?/; |
| 109 | |
| 110 | export function initLogger() { |
| 111 | const t = () => { |
| 112 | const s = new Date().toISOString().split("T")[1].replace("Z", ""); |
| 113 | return `[${s}]`; |
| 114 | }; |
| 115 | |
| 116 | const log = (rawLog: (...args: any[]) => void, ...args: any[]) => { |
| 117 | try { |
| 118 | const fixWidth = 20; |
| 119 | const stack = Error().stack!.split("\n"); |
| 120 | const match = stack[3].match(pathRegex)!; |
| 121 | const fileName = match[1].replace(/\?.*$/, "").split("/").pop() ?? ""; |
| 122 | const spaces = fileName.length < fixWidth ? " ".repeat(fixWidth - fileName.length) : ""; |
| 123 | rawLog(t(), `[${fileName}]${spaces}\t`, ...args); |
| 124 | } catch (e) { |
| 125 | rawLog(t(), ...args); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | // @ts-ignore |
| 130 | console.rawInfo = console.info.bind(console); |
| 131 | // @ts-ignore |
| 132 | console.l = function (...args: any[]) { |
| 133 | // @ts-ignore |
| 134 | log(this.rawInfo, ...args); |
| 135 | }; |
| 136 | } |