/
console.sub.ts
executable file
·58 lines (52 loc) · 2.38 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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 { StatsbeatInstrumentation } from "../../Declarations/Constants";
import { channel, IStandardEvent, trueFilter } 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.config.enableLoggerErrorToTrace) {
client.trackException({ exception: message });
}
else if(message instanceof Error) {
// If logging errors as traces return the error as a string and mark severity as error
client.trackTrace({ message: message.toString(), severity: (event.data.stderr ? SeverityLevel.Error : SeverityLevel.Information) });
} 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) {
let clientFound = clients.find(c => c == client);
if (clientFound) {
return;
}
if (clients.length === 0) {
channel.subscribe<consolePub.IConsoleData>("console", subscriber, trueFilter, (module, version) => {
let statsbeat = client.getStatsbeat();
if (statsbeat) {
statsbeat.addInstrumentation(StatsbeatInstrumentation.CONSOLE);
}
});
}
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 = [];
}