Browse Source

Updates decorators

main
Eric Amodio 6 years ago
parent
commit
3f6cf7ed66
3 changed files with 31 additions and 31 deletions
  1. +11
    -23
      src/logger.ts
  2. +9
    -3
      src/system/decorators/gate.ts
  3. +11
    -5
      src/system/decorators/log.ts

+ 11
- 23
src/logger.ts View File

@ -2,7 +2,7 @@
import { ConfigurationChangeEvent, ExtensionContext, OutputChannel, Uri, window } from 'vscode'; import { ConfigurationChangeEvent, ExtensionContext, OutputChannel, Uri, window } from 'vscode';
import { configuration, LogLevel } from './configuration'; import { configuration, LogLevel } from './configuration';
import { extensionOutputChannelName } from './constants'; import { extensionOutputChannelName } from './constants';
import { getCorrelationContext } from './system/decorators/log';
import { getCorrelationContext } from './system';
// import { Telemetry } from './telemetry'; // import { Telemetry } from './telemetry';
export { LogLevel } from './configuration'; export { LogLevel } from './configuration';
@ -196,21 +196,9 @@ export class Logger {
} }
static showOutputChannel() { 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) { 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 { private static get timestamp(): string {

+ 9
- 3
src/system/decorators/gate.ts View File

@ -3,10 +3,16 @@ import { Functions } from '../function';
export function gate() { export function gate() {
return (target: any, key: string, descriptor: PropertyDescriptor) => { 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 gateKey = `$gate$${key}`;
const fn = descriptor.value;
descriptor.value = function(this: any, ...args: any[]) { descriptor.value = function(this: any, ...args: any[]) {
if (!this.hasOwnProperty(gateKey)) { if (!this.hasOwnProperty(gateKey)) {
@ -20,7 +26,7 @@ export function gate() {
let promise = this[gateKey]; let promise = this[gateKey];
if (promise === undefined) { if (promise === undefined) {
const result = fn.apply(this, args);
const result = fn!.apply(this, args);
if (result == null || !Functions.isPromise(result)) { if (result == null || !Functions.isPromise(result)) {
return result; return result;
} }

+ 11
- 5
src/system/decorators/log.ts View File

@ -76,8 +76,14 @@ export function log(
const logFn = options.debug ? Logger.debug.bind(Logger) : Logger.log.bind(Logger); const logFn = options.debug ? Logger.debug.bind(Logger) : Logger.log.bind(Logger);
return (target: any, key: string, descriptor: PropertyDescriptor) => { 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); const parameters = Functions.getParameters(fn);
@ -86,7 +92,7 @@ export function log(
(Logger.level !== LogLevel.Debug && !(Logger.level === LogLevel.Verbose && !options.debug)) || (Logger.level !== LogLevel.Debug && !(Logger.level === LogLevel.Verbose && !options.debug)) ||
(typeof options.condition === 'function' && !options.condition(...args)) (typeof options.condition === 'function' && !options.condition(...args))
) { ) {
return fn.apply(this, args);
return fn!.apply(this, args);
} }
let instanceName: string; let instanceName: string;
@ -175,7 +181,7 @@ export function log(
let result; let result;
try { try {
result = fn.apply(this, args);
result = fn!.apply(this, args);
} }
catch (ex) { catch (ex) {
logError(ex); logError(ex);
@ -225,7 +231,7 @@ export function log(
return result; return result;
} }
return fn.apply(this, args);
return fn!.apply(this, args);
}; };
}; };
} }

Loading…
Cancel
Save