|
| 1 | +import { window } from 'vscode'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +export const loggerName = 'Manim Notebook'; |
| 5 | +const logger = window.createOutputChannel(loggerName, { log: true }); |
| 6 | + |
| 7 | +export default class Logger { |
| 8 | + |
| 9 | + public static trace(message: string) { |
| 10 | + logger.trace(`${Logger.getFormattedCallerInformation()} ${message}`); |
| 11 | + } |
| 12 | + |
| 13 | + public static debug(message: string) { |
| 14 | + logger.debug(`${Logger.getFormattedCallerInformation()} ${message}`); |
| 15 | + } |
| 16 | + |
| 17 | + public static info(message: string) { |
| 18 | + logger.info(`${Logger.getFormattedCallerInformation()} ${message}`); |
| 19 | + } |
| 20 | + |
| 21 | + public static warn(message: string) { |
| 22 | + logger.warn(`${Logger.getFormattedCallerInformation()} ${message}`); |
| 23 | + } |
| 24 | + |
| 25 | + public static error(message: string) { |
| 26 | + logger.error(`${Logger.getFormattedCallerInformation()} ${message}`); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Returns formatted caller information in the form of |
| 31 | + * "[filename] [methodname]". |
| 32 | + * |
| 33 | + * It works by creating a stack trace and extracting the file name from the |
| 34 | + * third line of the stack trace, e.g. |
| 35 | + * |
| 36 | + * Error: |
| 37 | + * at Logger.getCurrentFileName (manim-notebook/out/logger.js:32:19) |
| 38 | + * at Logger.info (manim-notebook/out/logger.js:46:39) |
| 39 | + * at activate (manim-notebook/out/extension.js:37:21) |
| 40 | + * ... |
| 41 | + * |
| 42 | + * where "extension.js:37:21" is the file that called the logger method |
| 43 | + * and "activate" is the respective method. |
| 44 | + * |
| 45 | + * Another example where the Logger is called in a Promise might be: |
| 46 | + * |
| 47 | + * Error: |
| 48 | + * at Function.getFormattedCallerInformation (manim-notebook/src/logger.ts:46:23) |
| 49 | + * at Function.info (manim-notebook/src/logger.ts:18:31) |
| 50 | + * at manim-notebook/src/extension.ts:199:12 |
| 51 | + * |
| 52 | + * where "extension.ts:199:12" is the file that called the logger method |
| 53 | + * and the method is unknown. |
| 54 | + */ |
| 55 | + private static getFormattedCallerInformation(): string { |
| 56 | + const error = new Error(); |
| 57 | + const stack = error.stack; |
| 58 | + |
| 59 | + const unknownString = "[unknown] [unknown]"; |
| 60 | + |
| 61 | + if (!stack) { |
| 62 | + return unknownString; |
| 63 | + } |
| 64 | + |
| 65 | + const stackLines = stack.split('\n'); |
| 66 | + if (stackLines.length < 4) { |
| 67 | + return unknownString; |
| 68 | + } |
| 69 | + |
| 70 | + const callerLine = stackLines[3]; |
| 71 | + if (!callerLine) { |
| 72 | + return unknownString; |
| 73 | + } |
| 74 | + |
| 75 | + const fileMatch = callerLine.match(/(?:[^\(\s])*?:\d+:\d+/); |
| 76 | + let fileName = 'unknown'; |
| 77 | + if (fileMatch && fileMatch[0]) { |
| 78 | + fileName = path.basename(fileMatch[0]); |
| 79 | + } |
| 80 | + |
| 81 | + const methodMatch = callerLine.match(/at (\w+) \(/); |
| 82 | + let methodName = 'unknown'; |
| 83 | + if (methodMatch && methodMatch[1]) { |
| 84 | + methodName = methodMatch[1]; |
| 85 | + } |
| 86 | + |
| 87 | + return `[${fileName}] [${methodName}]`; |
| 88 | + } |
| 89 | +} |
0 commit comments