diagnostics.ts 2.1 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import * as Platform from 'vs/base/common/platform';

/**
 * To enable diagnostics, open a browser console and type: window.Monaco.Diagnostics.<diagnostics name> = true.
 * Then trigger an action that will write to diagnostics to see all cached output from the past.
 */

var globals = Platform.globals;
J
Johannes Rieken 已提交
15
if (!globals.Monaco) {
E
Erich Gamma 已提交
16 17 18 19 20 21
	globals.Monaco = {};
}
globals.Monaco.Diagnostics = {};

var switches = globals.Monaco.Diagnostics;
var map = {};
B
Benjamin Pasero 已提交
22
var data: any[] = [];
E
Erich Gamma 已提交
23

J
Johannes Rieken 已提交
24 25
function fifo(array: any[], size: number) {
	while (array.length > size) {
E
Erich Gamma 已提交
26 27 28 29
		array.shift();
	}
}

J
Johannes Rieken 已提交
30 31
export function register(what: string, fn: Function): (...args: any[]) => void {

32 33
	let disable = true; // Otherwise we have unreachable code.
	if (disable) {
J
Johannes Rieken 已提交
34 35 36 37
		return () => {
			// Intentional empty, disable for now because it is leaking memory
		};
	}
E
Erich Gamma 已提交
38 39 40 41 42 43 44 45 46 47

	// register switch
	var flag = switches[what] || false;
	switches[what] = flag;

	// register function
	var tracers = map[what] || [];
	tracers.push(fn);
	map[what] = tracers;

J
Johannes Rieken 已提交
48
	var result = function (...args: any[]) {
E
Erich Gamma 已提交
49

J
Johannes Rieken 已提交
50
		var idx: number;
E
Erich Gamma 已提交
51

J
Johannes Rieken 已提交
52
		if (switches[what] === true) {
E
Erich Gamma 已提交
53 54 55
			// replay back-in-time functions
			var allArgs = [arguments];
			idx = data.indexOf(fn);
J
Johannes Rieken 已提交
56
			if (idx !== -1) {
E
Erich Gamma 已提交
57 58 59 60
				allArgs.unshift.apply(allArgs, data[idx + 1] || []);
				data[idx + 1] = [];
			}

J
Johannes Rieken 已提交
61
			var doIt: () => void = function () {
E
Erich Gamma 已提交
62 63
				var thisArguments = allArgs.shift();
				fn.apply(fn, thisArguments);
J
Johannes Rieken 已提交
64
				if (allArgs.length > 0) {
E
Erich Gamma 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
					Platform.setTimeout(doIt, 500);
				}
			};
			doIt();

		} else {
			// know where to store
			idx = data.indexOf(fn);
			idx = idx !== -1 ? idx : data.length;
			var dataIdx = idx + 1;

			// store arguments
			var allargs = data[dataIdx] || [];
			allargs.push(arguments);
			fifo(allargs, 50);

			// store data
			data[idx] = fn;
			data[dataIdx] = allargs;
		}
	};

	return result;
}