Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/client/common/logger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// These are all just temporary aliases, for backward compatibility
// and to avoid churn.
export {
// aliases:
// (temporarily for backward compatibility and to avoid churn)
traceDecorators,
logError as traceError,
logInfo as traceInfo,
logVerbose as traceVerbose,
logWarning as traceWarning
} from '../logging';
export { TraceOptions as LogOptions } from '../logging/trace';
traceError,
traceInfo,
traceVerbose,
traceWarning
} from '../logging/_trace';
export { TraceOptions as LogOptions } from '../logging/types';
10 changes: 0 additions & 10 deletions src/client/common/utils/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ export async function waitForPromise<T>(promise: Promise<T>, timeout: number): P
});
}

// tslint:disable-next-line: no-any
export function isThenable<T>(v: any): v is Thenable<T> {
return typeof v?.then === 'function';
}

// tslint:disable-next-line: no-any
export function isPromise<T>(v: any): v is Promise<T> {
return typeof v?.then === 'function' && typeof v?.catch === 'function';
}

//======================
// Deferred

Expand Down
35 changes: 0 additions & 35 deletions src/client/common/utils/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { traceError, traceVerbose } from '../logger';
import { Resource } from '../types';
import { createDeferred, Deferred } from './async';
import { getCacheKeyFromFunctionArgs, getGlobalCacheStore, InMemoryInterpreterSpecificCache } from './cacheUtils';
import { TraceInfo, tracing } from './misc';

// tslint:disable-next-line:no-require-imports no-var-requires
const _debounce = require('lodash/debounce') as typeof import('lodash/debounce');
Expand Down Expand Up @@ -226,37 +225,3 @@ export function displayProgress(title: string, location = ProgressLocation.Windo
};
};
}

// Information about a function/method call.
export type CallInfo = {
kind: string; // "Class", etc.
name: string;
// tslint:disable-next-line:no-any
args: any[];
};

// Return a decorator that traces the decorated function.
export function trace(log: (c: CallInfo, t: TraceInfo) => void) {
// tslint:disable-next-line:no-function-expression no-any
return function (_: Object, __: string, descriptor: TypedPropertyDescriptor<any>) {
const originalMethod = descriptor.value;
// tslint:disable-next-line:no-function-expression no-any
descriptor.value = function (...args: any[]) {
const call = {
kind: 'Class',
name: _ && _.constructor ? _.constructor.name : '',
args
};
// tslint:disable-next-line:no-this-assignment no-invalid-this
const scope = this;
return tracing(
// "log()"
(t) => log(call, t),
// "run()"
() => originalMethod.apply(scope, args)
);
};

return descriptor;
};
}
42 changes: 0 additions & 42 deletions src/client/common/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import { Uri } from 'vscode';
import { InterpreterUri } from '../installer/types';
import { IAsyncDisposable, IDisposable, Resource } from '../types';
import { isPromise } from './async';
import { StopWatch } from './stopWatch';

// tslint:disable-next-line:no-empty
export function noop() {}
Expand Down Expand Up @@ -40,46 +38,6 @@ export async function usingAsync<T extends IAsyncDisposable, R>(
}
}

// Information about a traced function/method call.
export type TraceInfo = {
elapsed: number; // milliseconds
// Either returnValue or err will be set.
// tslint:disable-next-line:no-any
returnValue?: any;
err?: Error;
};

// Call run(), call log() with the trace info, and return the result.
export function tracing<T>(log: (t: TraceInfo) => void, run: () => T): T {
const timer = new StopWatch();
try {
// tslint:disable-next-line:no-invalid-this no-use-before-declare no-unsafe-any
const result = run();

// If method being wrapped returns a promise then wait for it.
if (isPromise(result)) {
// tslint:disable-next-line:prefer-type-cast
(result as Promise<void>)
.then((data) => {
log({ elapsed: timer.elapsedTime, returnValue: data });
return data;
})
.catch((ex) => {
log({ elapsed: timer.elapsedTime, err: ex });
// tslint:disable-next-line:no-suspicious-comment
// TODO(GH-11645) Re-throw the error like we do
// in the non-Promise case.
});
} else {
log({ elapsed: timer.elapsedTime, returnValue: result });
}
return result;
} catch (ex) {
log({ elapsed: timer.elapsedTime, err: ex });
throw ex;
}
}

/**
* Checking whether something is a Resource (Uri/undefined).
* Using `instanceof Uri` doesn't always work as the object is not an instance of Uri (at least not in tests).
Expand Down
74 changes: 74 additions & 0 deletions src/client/logging/_console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';

// tslint:disable:no-console no-any

import { ConsoleStreams, LogLevel } from './types';

const logMethods = {
log: Symbol.for('log'),
info: Symbol.for('info'),
error: Symbol.for('error'),
debug: Symbol.for('debug'),
warn: Symbol.for('warn')
};

// Log a message based on "args" to the given console "stream".
export function logToConsole(stream: ConsoleStreams, ...args: any[]) {
if (['info', 'error', 'warn', 'log', 'debug'].indexOf(stream) === -1) {
stream = 'log';
}
// Further below we monkeypatch the console.log, etc methods.
const fn = (console as any)[logMethods[stream]] || console[stream] || console.log;
fn(...args);
}

/**
* What we're doing here is monkey patching the console.log so we can
* send everything sent to console window into our logs. This is only
* required when we're directly writing to `console.log` or not using
* our `winston logger`. This is something we'd generally turn on, only
* on CI so we can see everything logged to the console window
* (via the logs).
*/
export function monkeypatchConsole(logToFile: (logLevel: LogLevel, ...args: any[]) => void) {
// Keep track of the original functions before we monkey patch them.
// Using symbols guarantee the properties will be unique & prevents clashing with names other code/library may create or have created.
(console as any)[logMethods.log] = console.log;
(console as any)[logMethods.info] = console.info;
(console as any)[logMethods.error] = console.error;
(console as any)[logMethods.debug] = console.debug;
(console as any)[logMethods.warn] = console.warn;

// tslint:disable-next-line: no-function-expression
console.log = function () {
const args = Array.prototype.slice.call(arguments);
logToConsole('log', ...args);
logToFile(LogLevel.Information, ...args);
};
// tslint:disable-next-line: no-function-expression
console.info = function () {
const args = Array.prototype.slice.call(arguments);
logToConsole('info', ...args);
logToFile(LogLevel.Information, ...args);
};
// tslint:disable-next-line: no-function-expression
console.warn = function () {
const args = Array.prototype.slice.call(arguments);
logToConsole('warn', ...args);
logToFile(LogLevel.Warning, ...args);
};
// tslint:disable-next-line: no-function-expression
console.error = function () {
const args = Array.prototype.slice.call(arguments);
logToConsole('error', ...args);
logToFile(LogLevel.Error, ...args);
};
// tslint:disable-next-line: no-function-expression
console.debug = function () {
const args = Array.prototype.slice.call(arguments);
logToConsole('debug', ...args);
logToFile(LogLevel.Information, ...args);
};
}
157 changes: 0 additions & 157 deletions src/client/logging/_global.ts

This file was deleted.

Loading