From 780423b19553159943d78b291732c8a16ed7e8ad Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sun, 26 Feb 2017 23:50:29 -0500 Subject: [PATCH] Fixes logging to clean up on extension deactivate --- package.json | 2 +- src/configuration.ts | 8 +------ src/extension.ts | 2 ++ src/logger.ts | 62 ++++++++++++++++++++++++++++++++-------------------- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 586bda1..40b95a3 100644 --- a/package.json +++ b/package.json @@ -653,8 +653,8 @@ }, "devDependencies": { "@types/copy-paste": "^1.1.30", - "@types/node": "^7.0.5", "@types/mocha": "^2.2.39", + "@types/node": "^7.0.5", "@types/tmp": "^0.0.32", "mocha": "^3.2.0", "tslint": "^4.4.2", diff --git a/src/configuration.ts b/src/configuration.ts index 75767e7..29062bc 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -1,5 +1,6 @@ 'use strict'; import { Commands } from './commands'; +import { OutputLevel } from './logger'; export type BlameAnnotationStyle = 'compact' | 'expanded' | 'trailing'; export const BlameAnnotationStyle = { @@ -90,13 +91,6 @@ export interface IStatusBarConfig { command: StatusBarCommand; } -export type OutputLevel = 'silent' | 'errors' | 'verbose'; -export const OutputLevel = { - Silent: 'silent' as OutputLevel, - Errors: 'errors' as OutputLevel, - Verbose: 'verbose' as OutputLevel -}; - export interface IAdvancedConfig { caching: { enabled: boolean; diff --git a/src/extension.ts b/src/extension.ts index eff91cc..4151928 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -27,6 +27,8 @@ import { Logger } from './logger'; // this method is called when your extension is activated export async function activate(context: ExtensionContext) { + Logger.configure(context); + // Workspace not using a folder. No access to git repo. if (!workspace.rootPath) { Logger.warn('GitLens inactive: no rootPath'); diff --git a/src/logger.ts b/src/logger.ts index 5c02e4b..0124e3d 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,58 +1,72 @@ 'use strict'; -import { Objects } from './system'; -import { OutputChannel, window, workspace } from 'vscode'; -import { IAdvancedConfig, OutputLevel } from './configuration'; +import { ExtensionContext, OutputChannel, window, workspace } from 'vscode'; +import { IAdvancedConfig } from './configuration'; -let config: IAdvancedConfig; +const ConfigurationName = 'gitlens'; +const OutputChannelName = 'GitLens'; +const ConsolePrefix = `[${OutputChannelName}]`; + +export type OutputLevel = 'silent' | 'errors' | 'verbose'; +export const OutputLevel = { + Silent: 'silent' as OutputLevel, + Errors: 'errors' as OutputLevel, + Verbose: 'verbose' as OutputLevel +}; + +let debug = false; +let level: OutputLevel = OutputLevel.Silent; let output: OutputChannel; -workspace.onDidChangeConfiguration(onConfigurationChange); -onConfigurationChange(); +function onConfigurationChanged() { + const cfg = workspace.getConfiguration(ConfigurationName).get('advanced'); -function onConfigurationChange() { - const cfg = workspace.getConfiguration('gitlens').get('advanced'); + if (cfg.debug !== debug || cfg.output.level !== level) { + debug = cfg.debug; + level = cfg.output.level; - if (!Objects.areEquivalent(cfg.output, config && config.output)) { - if (cfg.output.level === OutputLevel.Silent) { + if (level === OutputLevel.Silent) { output && output.dispose(); } - else if (!output) { - output = window.createOutputChannel('GitLens'); + else { + output = output && window.createOutputChannel(OutputChannelName); } } - - config = cfg; } export class Logger { + static configure(context: ExtensionContext) { + context.subscriptions.push(workspace.onDidChangeConfiguration(onConfigurationChanged)); + onConfigurationChanged(); + } + static log(message?: any, ...params: any[]): void { - if (config.debug) { - console.log('[GitLens]', message, ...params); + if (debug && level !== OutputLevel.Silent) { + console.log(ConsolePrefix, message, ...params); } - if (config.output.level === OutputLevel.Verbose) { + if (level === OutputLevel.Verbose) { output.appendLine([message, ...params].join(' ')); } } static error(message?: any, ...params: any[]): void { - if (config.debug) { - console.error('[GitLens]', message, ...params); + if (debug) { + console.error(ConsolePrefix, message, ...params); } - if (config.output.level !== OutputLevel.Silent) { + if (level !== OutputLevel.Silent) { output.appendLine([message, ...params].join(' ')); } } static warn(message?: any, ...params: any[]): void { - if (config.debug) { - console.warn('[GitLens]', message, ...params); + if (debug) { + console.warn(ConsolePrefix, message, ...params); } - if (config.output.level !== OutputLevel.Silent) { + if (level !== OutputLevel.Silent) { output.appendLine([message, ...params].join(' ')); } } -} +} \ No newline at end of file