Skip to content

Commit

Permalink
Merge pull request #97 from bendera/feat/add-logger
Browse files Browse the repository at this point in the history
feat: add logger function
  • Loading branch information
bendera authored May 8, 2023
2 parents 0b07930 + 9cc3c10 commit 2dfa1b0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/commands/EditorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -39,6 +41,8 @@ export async function activate(context: vscode.ExtensionContext) {
copyFromScmInputBoxCommand
)
);

logger.log('Extension has been activated');
}

export function deactivate() {}
33 changes: 33 additions & 0 deletions src/utils/Logger.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}
}

0 comments on commit 2dfa1b0

Please sign in to comment.