diff --git a/src/logger.ts b/src/logger.ts index 574524b..1ff07f1 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -2,7 +2,7 @@ import { ConfigurationChangeEvent, ExtensionContext, OutputChannel, Uri, window } from 'vscode'; import { configuration, LogLevel } from './configuration'; import { extensionOutputChannelName } from './constants'; -import { getCorrelationContext } from './system/decorators/log'; +import { getCorrelationContext } from './system'; // import { Telemetry } from './telemetry'; export { LogLevel } from './configuration'; @@ -196,21 +196,9 @@ export class Logger { } static showOutputChannel() { - if (this.output !== undefined) { - this.output.show(); - } - } + if (this.output === undefined) return; - static toLoggableName(instance: { constructor: Function }) { - const name = - typeof instance === 'function' - ? instance.name - : instance.constructor != null - ? instance.constructor.name - : ''; - // Strip webpack module name (since I never name classes with an _) - const index = name.indexOf('_'); - return index === -1 ? name : name.substr(index + 1); + this.output.show(); } static toLoggable(p: any, sanitize?: ((key: string, value: any) => any) | undefined) { @@ -229,15 +217,15 @@ export class Logger { } } - private static getCallerContext(caller: Function): LogCorrelationContext | undefined { - let context = (caller as any).$log; - if (context == null && caller.prototype != null) { - context = caller.prototype.$log; - if (context == null && caller.prototype.constructor != null) { - context = caller.prototype.constructor.$log; - } + static toLoggableName(instance: Function | object) { + if (typeof instance === 'function') { + return instance.name; } - return context; + + const name = instance.constructor != null ? instance.constructor.name : ''; + // Strip webpack module name (since I never name classes with an _) + const index = name.indexOf('_'); + return index === -1 ? name : name.substr(index + 1); } private static get timestamp(): string { diff --git a/src/system/decorators/gate.ts b/src/system/decorators/gate.ts index edd00d1..22d0cd1 100644 --- a/src/system/decorators/gate.ts +++ b/src/system/decorators/gate.ts @@ -3,10 +3,16 @@ import { Functions } from '../function'; export function gate() { return (target: any, key: string, descriptor: PropertyDescriptor) => { - if (typeof descriptor.value !== 'function') throw new Error('Not supported'); + let fn: Function | undefined; + if (typeof descriptor.value === 'function') { + fn = descriptor.value; + } + else if (typeof descriptor.get === 'function') { + fn = descriptor.get; + } + if (fn == null) throw new Error('Not supported'); const gateKey = `$gate$${key}`; - const fn = descriptor.value; descriptor.value = function(this: any, ...args: any[]) { if (!this.hasOwnProperty(gateKey)) { @@ -20,7 +26,7 @@ export function gate() { let promise = this[gateKey]; if (promise === undefined) { - const result = fn.apply(this, args); + const result = fn!.apply(this, args); if (result == null || !Functions.isPromise(result)) { return result; } diff --git a/src/system/decorators/log.ts b/src/system/decorators/log.ts index 4aa63c5..9d4bacc 100644 --- a/src/system/decorators/log.ts +++ b/src/system/decorators/log.ts @@ -76,8 +76,14 @@ export function log( const logFn = options.debug ? Logger.debug.bind(Logger) : Logger.log.bind(Logger); return (target: any, key: string, descriptor: PropertyDescriptor) => { - const fn = descriptor.value; - if (typeof fn !== 'function') throw new Error('not supported'); + let fn: Function | undefined; + if (typeof descriptor.value === 'function') { + fn = descriptor.value; + } + else if (typeof descriptor.get === 'function') { + fn = descriptor.get; + } + if (fn == null) throw new Error('Not supported'); const parameters = Functions.getParameters(fn); @@ -86,7 +92,7 @@ export function log( (Logger.level !== LogLevel.Debug && !(Logger.level === LogLevel.Verbose && !options.debug)) || (typeof options.condition === 'function' && !options.condition(...args)) ) { - return fn.apply(this, args); + return fn!.apply(this, args); } let instanceName: string; @@ -175,7 +181,7 @@ export function log( let result; try { - result = fn.apply(this, args); + result = fn!.apply(this, args); } catch (ex) { logError(ex); @@ -225,7 +231,7 @@ export function log( return result; } - return fn.apply(this, args); + return fn!.apply(this, args); }; }; }