From 97504b3fb2fc91a917f0371eb134991ba0131ca3 Mon Sep 17 00:00:00 2001 From: Splines Date: Tue, 29 Oct 2024 02:17:00 +0100 Subject: [PATCH 1/8] Init logger and "Open log file" command --- package.json | 5 +++++ src/extension.ts | 40 +++++++++++++++++++++++++++++++++++++++- src/logger.ts | 4 ++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/logger.ts diff --git a/package.json b/package.json index e25bdc45..c4d9a9d5 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,11 @@ "command": "manim-notebook.clearScene", "title": "Remove all objects from scene", "category": "Manim Notebook" + }, + { + "command": "manim-notebook.openLogFile", + "title": "Open log file", + "category": "Manim Notebook" } ], "keybindings": [ diff --git a/src/extension.ts b/src/extension.ts index 35bd6567..6c93837f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -5,6 +5,7 @@ import { ManimCell } from './manimCell'; import { ManimCellRanges } from './manimCellRanges'; import { previewCode } from './previewCode'; import { startScene, exitScene } from './startStopScene'; +import { logger, loggerName } from './logger'; export function activate(context: vscode.ExtensionContext) { @@ -40,12 +41,18 @@ export function activate(context: vscode.ExtensionContext) { } ); + const openLogFileCommand = vscode.commands.registerCommand( + 'manim-notebook.openLogFile', async () => { + openLogFile(context); + }); + context.subscriptions.push( previewManimCellCommand, previewSelectionCommand, startSceneCommand, exitSceneCommand, - clearSceneCommand + clearSceneCommand, + openLogFileCommand ); registerManimCellProviders(context); } @@ -167,3 +174,34 @@ function registerManimCellProviders(context: vscode.ExtensionContext) { manimCell.applyCellDecorations(window.activeTextEditor); } } + +/** + * Opens the Manim Notebook log file in a new editor. + * + * @param context The extension context. + */ +function openLogFile(context: vscode.ExtensionContext) { + const logFilePath = vscode.Uri.joinPath(context.logUri, `${loggerName}.log`); + vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + title: "Opening Manim Notebook log file...", + cancellable: false + }, async (progressIndicator, token) => { + await new Promise(async (resolve) => { + try { + const doc = await vscode.workspace.openTextDocument(logFilePath); + await vscode.window.showTextDocument(doc); + } catch { + vscode.window.showErrorMessage("Could not open Manim Notebook log file"); + } finally { + resolve(); + } + + // I've also tried to open the log file in the OS browser, + // but didn't get it to work via: + // commands.executeCommand("revealFileInOS", logFilePath); + // For a sample usage, see this: + // https://github.com/microsoft/vscode/blob/9de080f7cbcec77de4ef3e0d27fbf9fd335d3fba/extensions/typescript-language-features/src/typescriptServiceClient.ts#L580-L586 + }); + }); +} \ No newline at end of file diff --git a/src/logger.ts b/src/logger.ts new file mode 100644 index 00000000..d84e5c52 --- /dev/null +++ b/src/logger.ts @@ -0,0 +1,4 @@ +import { window } from 'vscode'; + +export const loggerName = 'Manim Notebook'; +export const logger = window.createOutputChannel(loggerName, { log: true }); From cdd0c8317ce0745e2bcc76f16293bad07c044cda Mon Sep 17 00:00:00 2001 From: Splines Date: Tue, 29 Oct 2024 02:38:23 +0100 Subject: [PATCH 2/8] Add caller information to logger --- src/extension.ts | 5 +++- src/logger.ts | 77 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 6c93837f..ed40993c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -5,7 +5,8 @@ import { ManimCell } from './manimCell'; import { ManimCellRanges } from './manimCellRanges'; import { previewCode } from './previewCode'; import { startScene, exitScene } from './startStopScene'; -import { logger, loggerName } from './logger'; +import { loggerName } from './logger'; +import Logger from './logger'; export function activate(context: vscode.ExtensionContext) { @@ -55,6 +56,8 @@ export function activate(context: vscode.ExtensionContext) { openLogFileCommand ); registerManimCellProviders(context); + + Logger.info("Manim Notebook activated"); } export function deactivate() { } diff --git a/src/logger.ts b/src/logger.ts index d84e5c52..da2a796a 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,4 +1,79 @@ import { window } from 'vscode'; +import * as path from 'path'; export const loggerName = 'Manim Notebook'; -export const logger = window.createOutputChannel(loggerName, { log: true }); +const logger = window.createOutputChannel(loggerName, { log: true }); + +export default class Logger { + + public static trace(message: string) { + logger.trace(`${Logger.getFormattedCallerInformation()} ${message}`); + } + + public static debug(message: string) { + logger.debug(`${Logger.getFormattedCallerInformation()} ${message}`); + } + + public static info(message: string) { + logger.info(`${Logger.getFormattedCallerInformation()} ${message}`); + } + + public static warn(message: string) { + logger.warn(`${Logger.getFormattedCallerInformation()} ${message}`); + } + + public static error(message: string) { + logger.error(`${Logger.getFormattedCallerInformation()} ${message}`); + } + + /** + * Returns formatted caller information in the form of + * "[filename] [methodname]". + * + * It works by creating a stack trace and extracting the file name from the + * third line of the stack trace, e.g. + * + * Error: + * at Logger.getCurrentFileName (manim-notebook/out/logger.js:32:19) + * at Logger.info (manim-notebook/out/logger.js:46:39) + * at activate (manim-notebook/out/extension.js:37:21) + * ... + * + * where "extension.js" is the file that called the logger method + * and "activate" is the respective method. + */ + private static getFormattedCallerInformation(): string { + const error = new Error(); + const stack = error.stack; + + const unknownString = "[unknown] [unknown]"; + + if (!stack) { + return unknownString; + } + + const stackLines = stack.split('\n'); + if (stackLines.length < 4) { + return unknownString; + } + + const callerLine = stackLines[3]; + if (!callerLine) { + return unknownString; + } + + const methodMatch = callerLine.match(/at (\w+) \(/); + let methodName = 'unknown'; + if (methodMatch && methodMatch[1]) { + methodName = methodMatch[1]; + } + + const fileMatch = callerLine.match(/\((.*):\d+:\d+\)/); + let fileName = 'unknown'; + if (fileMatch && fileMatch[1]) { + fileName = path.basename(fileMatch[1]); + } + + return `[${fileName}] [${methodName}]`; + } +} From 7d991600126aae8ea84adbe07377010046e76a5f Mon Sep 17 00:00:00 2001 From: Splines Date: Tue, 29 Oct 2024 02:48:40 +0100 Subject: [PATCH 3/8] Explain how to attach log file to GitHub issue --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 1b092bf9..73838906 100644 --- a/README.md +++ b/README.md @@ -71,3 +71,15 @@ The resulting workflow can look like Grant's 🥳 - [contributing](https://github.com/bhoov/manim-notebook/blob/main/CONTRIBUTING.md) - [wiki](https://github.com/bhoov/manim-notebook/wiki) + +

+ +## Troubleshooting + +If you encounter an issue, search for some related keywords first in the [issues](https://github.com/bhoov/manim-notebook/issues). If you can't find anything, feel free to open a new issue. To analyze the problem, we need a **log file** from you: + +- Reload the VSCode window. This is important for us such that only important log messages are included in the log file and not unrelated ones. +- Open the command palette (`Ctrl+Shift+P` or `Cmd+Shift+P`). Use the command `Developer: Set Log Level...`, click on `Manim Notebook` and set the log level to `Trace`. +- Now reproduce the issue, e.g. by running a command that causes the problem. +- Open the command palette again and use the command `Manim Notebook: Open Log File`. +- Attach the log file to your GitHub issue. To do so, right-click on the opened log file header (the tab pane that shows the filename at the top of the editor) and select `Reveal In File Explorer`. Then drag and drop the file into the GitHub issue text field. From 5f490951f6fd85d1d96d79884ec2a67b5c40a946 Mon Sep 17 00:00:00 2001 From: Splines Date: Tue, 29 Oct 2024 02:49:42 +0100 Subject: [PATCH 4/8] Capitalize open log file command Many VSCode commands are actually capitalized. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c4d9a9d5..2fc66046 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ }, { "command": "manim-notebook.openLogFile", - "title": "Open log file", + "title": "Open Log File", "category": "Manim Notebook" } ], From 239a9476fa6dd5ccaa3be38748415bf12c0a113e Mon Sep 17 00:00:00 2001 From: Vladimir Fokow <57260995+VladimirFokow@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:44:37 +0100 Subject: [PATCH 5/8] add the MacOS option: "Reveal in Finder" --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 73838906..0ee81b64 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ The resulting workflow can look like Grant's 🥳 If you encounter an issue, search for some related keywords first in the [issues](https://github.com/bhoov/manim-notebook/issues). If you can't find anything, feel free to open a new issue. To analyze the problem, we need a **log file** from you: - Reload the VSCode window. This is important for us such that only important log messages are included in the log file and not unrelated ones. -- Open the command palette (`Ctrl+Shift+P` or `Cmd+Shift+P`). Use the command `Developer: Set Log Level...`, click on `Manim Notebook` and set the log level to `Trace`. +- Open the command palette `Ctrl+Shift+P` (or `Cmd+Shift+P`). Use the command `Developer: Set Log Level...`, click on `Manim Notebook` and set the log level to `Trace`. - Now reproduce the issue, e.g. by running a command that causes the problem. - Open the command palette again and use the command `Manim Notebook: Open Log File`. -- Attach the log file to your GitHub issue. To do so, right-click on the opened log file header (the tab pane that shows the filename at the top of the editor) and select `Reveal In File Explorer`. Then drag and drop the file into the GitHub issue text field. +- Attach the log file to your GitHub issue. To do so, right-click on the opened log file header (the tab pane that shows the filename at the top of the editor) and select `Reveal In File Explorer` (or `Reveal in Finder`). Then drag and drop the file into the GitHub issue text field. From 3e9666da535d9c79468c74c168f46a0855767c42 Mon Sep 17 00:00:00 2001 From: Splines Date: Tue, 29 Oct 2024 11:24:29 +0100 Subject: [PATCH 6/8] Also match line number & other case in Logger --- src/logger.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/logger.ts b/src/logger.ts index da2a796a..9c6d050c 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -39,8 +39,18 @@ export default class Logger { * at activate (manim-notebook/out/extension.js:37:21) * ... * - * where "extension.js" is the file that called the logger method + * where "extension.js:37:21" is the file that called the logger method * and "activate" is the respective method. + * + * Another example where the Logger is called in a Promise might be: + * + * Error: + * at Function.getFormattedCallerInformation (manim-notebook/src/logger.ts:46:23) + * at Function.info (manim-notebook/src/logger.ts:18:31) + * at manim-notebook/src/extension.ts:199:12 + * + * where "extension.ts:199:12" is the file that called the logger method + * and the method is unknown. */ private static getFormattedCallerInformation(): string { const error = new Error(); @@ -62,18 +72,18 @@ export default class Logger { return unknownString; } + const fileMatch = callerLine.match(/(?:[^\(\s][\S])*:\d+:\d+/); + let fileName = 'unknown'; + if (fileMatch && fileMatch[0]) { + fileName = path.basename(fileMatch[0]); + } + const methodMatch = callerLine.match(/at (\w+) \(/); let methodName = 'unknown'; if (methodMatch && methodMatch[1]) { methodName = methodMatch[1]; } - const fileMatch = callerLine.match(/\((.*):\d+:\d+\)/); - let fileName = 'unknown'; - if (fileMatch && fileMatch[1]) { - fileName = path.basename(fileMatch[1]); - } - return `[${fileName}] [${methodName}]`; } } From 423cb47fad2dac5e19dacd676a556f503b39a29a Mon Sep 17 00:00:00 2001 From: Splines Date: Tue, 29 Oct 2024 11:26:59 +0100 Subject: [PATCH 7/8] Add "Reset log level" as last step to Readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0ee81b64..95bd5ccb 100644 --- a/README.md +++ b/README.md @@ -83,3 +83,4 @@ If you encounter an issue, search for some related keywords first in the [issues - Now reproduce the issue, e.g. by running a command that causes the problem. - Open the command palette again and use the command `Manim Notebook: Open Log File`. - Attach the log file to your GitHub issue. To do so, right-click on the opened log file header (the tab pane that shows the filename at the top of the editor) and select `Reveal In File Explorer` (or `Reveal in Finder`). Then drag and drop the file into the GitHub issue text field. +- Last, but not least, don't forget to set the log level back to `Info` to avoid performance issues. `Developer: Set Log Level...` -> `Manim Notebook` -> `Info`. From 2267122ff05ff879edb0fa4a2b9fbc59bbdeec31 Mon Sep 17 00:00:00 2001 From: Splines Date: Tue, 29 Oct 2024 11:33:08 +0100 Subject: [PATCH 8/8] Make RegEx more stable --- src/logger.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logger.ts b/src/logger.ts index 9c6d050c..3c4fe72f 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -72,7 +72,7 @@ export default class Logger { return unknownString; } - const fileMatch = callerLine.match(/(?:[^\(\s][\S])*:\d+:\d+/); + const fileMatch = callerLine.match(/(?:[^\(\s])*?:\d+:\d+/); let fileName = 'unknown'; if (fileMatch && fileMatch[0]) { fileName = path.basename(fileMatch[0]);