-
Notifications
You must be signed in to change notification settings - Fork 135
/
console.sub.ts
executable file
·44 lines (38 loc) · 1.6 KB
/
console.sub.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import TelemetryClient = require("../../Library/TelemetryClient");
import {SeverityLevel} from "../../Declarations/Contracts";
import {channel, IStandardEvent} from "diagnostic-channel";
import {console as consolePub} from "diagnostic-channel-publishers";
let clients: TelemetryClient[] = [];
const subscriber = (event: IStandardEvent<consolePub.IConsoleData>) => {
let message = event.data.message as Error | string;
clients.forEach((client) => {
if (message instanceof Error) {
client.trackException({ exception: message});
} else {
// Message can have a trailing newline
if (message.lastIndexOf("\n") == message.length - 1) {
message = message.substring(0, message.length - 1);
}
client.trackTrace({message: message, severity: (event.data.stderr ? SeverityLevel.Warning : SeverityLevel.Information)});
}
});
};
export function enable(enabled: boolean, client: TelemetryClient) {
if (enabled) {
if (clients.length === 0) {
channel.subscribe<consolePub.IConsoleData>("console", subscriber);
};
clients.push(client);
} else {
clients = clients.filter((c) => c != client);
if (clients.length === 0) {
channel.unsubscribe("console", subscriber);
}
}
}
export function dispose() {
channel.unsubscribe("console", subscriber);
clients = [];
}