diff --git a/src/commands/EditorController.ts b/src/commands/EditorController.ts index d8183b9..195db4b 100644 --- a/src/commands/EditorController.ts +++ b/src/commands/EditorController.ts @@ -4,6 +4,7 @@ import { editorGroupNameMap, ViewColumnKey, ViewType } from '../definitions'; import GitService, { RepositoryInfo } from '../utils/GitService'; import EditorView from '../webviews/EditorView'; import UiApi from '../utils/UiApi'; +import Logger from '../utils/Logger'; export default class EditorController { private _primaryEditorPanel: vscode.WebviewPanel | undefined; @@ -12,10 +13,12 @@ export default class EditorController { constructor( private _context: vscode.ExtensionContext, - private _git: GitService + private _git: GitService, + private _logger: Logger ) {} async openInTheMainView(repo: Repository) { + this._logger.log('Opened in the main view'); const config = vscode.workspace.getConfiguration('commit-message-editor'); if (this._primaryEditorPanel) { @@ -54,15 +57,22 @@ export default class EditorController { if (repo && repo.rootUri) { const repoPath = repo.rootUri.path; const commits = await this._git.getRecentCommitMessagesByPath(repoPath); - - this._ui.sendRepositoryInfo({ + const repositoryInfo = { numberOfRepositories: this._git.getNumberOfRepositories(), selectedRepositoryPath: repoPath, availableRepositories: this._git.getAvailableRepositoryPaths(), - }); + }; + + this._ui.sendRepositoryInfo(repositoryInfo); + this._logger.logObject( + repositoryInfo, + 'Suggested repository:' + ); this._ui.sendRecentCommits(commits); } else { - this._ui.sendRepositoryInfo(this._getRepositoryInfo()); + const repositoryInfo = this._getRepositoryInfo(); + this._ui.sendRepositoryInfo(repositoryInfo); + this._logger.logObject(repositoryInfo, 'Default repository:'); } this._initReceivedMessageListener(); @@ -74,6 +84,7 @@ export default class EditorController { } private async _handleRepositoryDidChange(repositoryInfo: RepositoryInfo) { + this._logger.logObject(repositoryInfo, 'Repository did change:'); this._ui?.sendRepositoryInfo(repositoryInfo); const { selectedRepositoryPath } = repositoryInfo; diff --git a/src/extension.ts b/src/extension.ts index 026d4eb..f5bf14e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,11 +4,13 @@ import EditorController from './commands/EditorController'; import SettingsPageController from './commands/SettingsPageController'; import CopyFromScmInputBoxCommand from './commands/CopyFromScmInputBoxCommand'; import { Command } from './definitions'; +import Logger from './utils/Logger'; export async function activate(context: vscode.ExtensionContext) { + const logger = new Logger(); const git = new GitService(); - const editorController = new EditorController(context, git); + const editorController = new EditorController(context, git, logger); const settingsPageController = new SettingsPageController(context); const copyFromScmInputBoxCommand = new CopyFromScmInputBoxCommand( @@ -39,6 +41,8 @@ export async function activate(context: vscode.ExtensionContext) { copyFromScmInputBoxCommand ) ); + + logger.log('Extension has been activated'); } export function deactivate() {} diff --git a/src/utils/Logger.ts b/src/utils/Logger.ts new file mode 100644 index 0000000..259f6bb --- /dev/null +++ b/src/utils/Logger.ts @@ -0,0 +1,33 @@ +import * as vscode from 'vscode'; + +export default class Logger { + private _channel: vscode.OutputChannel; + + constructor() { + this._channel = vscode.window.createOutputChannel('Commit Message Editor', 'Log'); + } + + log(message: string) { + const timeStamp = `[${this._getTimestamp()}]`; + this._channel.appendLine(`${timeStamp} ${message}`); + } + + logObject(obj: any, label = '') { + const timeStamp = `[${this._getTimestamp()}]`; + this._channel.appendLine(`${timeStamp} ${label}`); + this._channel.appendLine(JSON.stringify(obj, undefined, 2)); + } + + private _getTimestamp() { + const now = new Date(); + const y = now.getFullYear(); + const m = (now.getMonth() + 1).toString().padStart(2, '0'); + const d = now.getDate().toString().padStart(2, '0'); + const h = now.getHours().toString().padStart(2, '0'); + const mi = now.getMinutes().toString().padStart(2, '0'); + const s = now.getSeconds().toString().padStart(2, '0'); + const ms = now.getMilliseconds().toString().padStart(3, '0'); + + return `${y}-${m}-${d} ${h}:${mi}:${s}.${ms}`; + } +}