forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.ts
More file actions
213 lines (180 loc) · 7.35 KB
/
Copy pathlogging.ts
File metadata and controls
213 lines (180 loc) · 7.35 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! Licensed to the .NET Foundation under one or more agreements.
//! The .NET Foundation licenses this file to you under the MIT license.
import BuildConfiguration from "consts:configuration";
import { INTERNAL, Module, runtimeHelpers } from "./imports";
import { CharPtr, VoidPtr } from "./types/emscripten";
const wasm_func_map = new Map<number, string>();
const regexes: any[] = [];
// V8
// at <anonymous>:wasm-function[1900]:0x83f63
// at dlfree (<anonymous>:wasm-function[18739]:0x2328ef)
regexes.push(/at (?<replaceSection>[^:()]+:wasm-function\[(?<funcNum>\d+)\]:0x[a-fA-F\d]+)((?![^)a-fA-F\d])|$)/);
//# 5: WASM [009712b2], function #111 (''), pc=0x7c16595c973 (+0x53), pos=38740 (+11)
regexes.push(/(?:WASM \[[\da-zA-Z]+\], (?<replaceSection>function #(?<funcNum>[\d]+) \(''\)))/);
//# chrome
//# at http://127.0.0.1:63817/dotnet.wasm:wasm-function[8963]:0x1e23f4
regexes.push(/(?<replaceSection>[a-z]+:\/\/[^ )]*:wasm-function\[(?<funcNum>\d+)\]:0x[a-fA-F\d]+)/);
//# <?>.wasm-function[8962]
regexes.push(/(?<replaceSection><[^ >]+>[.:]wasm-function\[(?<funcNum>[0-9]+)\])/);
export function mono_wasm_symbolicate_string(message: string): string {
try {
if (wasm_func_map.size == 0)
return message;
const origMessage = message;
for (let i = 0; i < regexes.length; i++) {
const newRaw = message.replace(new RegExp(regexes[i], "g"), (substring, ...args) => {
const groups = args.find(arg => {
return typeof (arg) == "object" && arg.replaceSection !== undefined;
});
if (groups === undefined)
return substring;
const funcNum = groups.funcNum;
const replaceSection = groups.replaceSection;
const name = wasm_func_map.get(Number(funcNum));
if (name === undefined)
return substring;
return substring.replace(replaceSection, `${name} (${replaceSection})`);
});
if (newRaw !== origMessage)
return newRaw;
}
return origMessage;
} catch (error) {
console.debug(`MONO_WASM: failed to symbolicate: ${error}`);
return message;
}
}
export function mono_wasm_stringify_as_error_with_stack(err: Error | string): string {
let errObj: any = err;
if (!(errObj instanceof Error)) {
errObj = new Error(errObj);
}
// Error
return mono_wasm_symbolicate_string(errObj.stack);
}
export function mono_wasm_trace_logger(log_domain_ptr: CharPtr, log_level_ptr: CharPtr, message_ptr: CharPtr, fatal: number, user_data: VoidPtr): void {
const origMessage = Module.UTF8ToString(message_ptr);
const isFatal = !!fatal;
const domain = Module.UTF8ToString(log_domain_ptr);
const dataPtr = user_data;
const log_level = Module.UTF8ToString(log_level_ptr);
const message = `[MONO] ${origMessage}`;
if (INTERNAL["logging"] && typeof INTERNAL.logging["trace"] === "function") {
INTERNAL.logging.trace(domain, log_level, message, isFatal, dataPtr);
return;
}
switch (log_level) {
case "critical":
case "error":
console.error(mono_wasm_stringify_as_error_with_stack(message));
break;
case "warning":
console.warn(message);
break;
case "message":
console.log(message);
break;
case "info":
console.info(message);
break;
case "debug":
console.debug(message);
break;
default:
console.log(message);
break;
}
}
export let consoleWebSocket: WebSocket;
export function setup_proxy_console(id: string, console: Console, origin: string): void {
// this need to be copy, in order to keep reference to original methods
const originalConsole = {
log: console.log,
error: console.error
};
const anyConsole = console as any;
function proxyConsoleMethod(prefix: string, func: any, asJson: boolean) {
return function (...args: any[]) {
try {
let payload = args[0];
if (payload === undefined) payload = "undefined";
else if (payload === null) payload = "null";
else if (typeof payload === "function") payload = payload.toString();
else if (typeof payload !== "string") {
try {
payload = JSON.stringify(payload);
} catch (e) {
payload = payload.toString();
}
}
if (typeof payload === "string" && id !== "main")
payload = `[${id}] ${payload}`;
if (asJson) {
func(JSON.stringify({
method: prefix,
payload: payload,
arguments: args
}));
} else {
func([prefix + payload, ...args.slice(1)]);
}
} catch (err) {
originalConsole.error(`proxyConsole failed: ${err}`);
}
};
}
const methods = ["debug", "trace", "warn", "info", "error"];
for (const m of methods) {
if (typeof (anyConsole[m]) !== "function") {
anyConsole[m] = proxyConsoleMethod(`console.${m}: `, console.log, false);
}
}
const consoleUrl = `${origin}/console`.replace("https://", "wss://").replace("http://", "ws://");
consoleWebSocket = new WebSocket(consoleUrl);
consoleWebSocket.addEventListener("open", () => {
originalConsole.log(`browser: [${id}] Console websocket connected.`);
});
consoleWebSocket.addEventListener("error", (event) => {
originalConsole.error(`[${id}] websocket error: ${event}`, event);
});
consoleWebSocket.addEventListener("close", (event) => {
originalConsole.error(`[${id}] websocket closed: ${event}`, event);
});
const send = (msg: string) => {
if (consoleWebSocket.readyState === WebSocket.OPEN) {
consoleWebSocket.send(msg);
}
else {
originalConsole.log(msg);
}
};
for (const m of ["log", ...methods])
anyConsole[m] = proxyConsoleMethod(`console.${m}`, send, true);
}
export function readSymbolMapFile(filename: string): void {
if (runtimeHelpers.mono_wasm_symbols_are_ready) return;
runtimeHelpers.mono_wasm_symbols_are_ready = true;
try {
const res = Module.FS_readFile(filename, { flags: "r", encoding: "utf8" });
res.split(/[\r\n]/).forEach((line: string) => {
const parts: string[] = line.split(/:/);
if (parts.length < 2)
return;
parts[1] = parts.splice(1).join(":");
wasm_func_map.set(Number(parts[0]), parts[1]);
});
if (BuildConfiguration === "Debug") {
console.debug(`MONO_WASM: Loaded ${wasm_func_map.size} symbols`);
}
} catch (error: any) {
if (error.errno == 44) {// NOENT
if (BuildConfiguration === "Debug") {
console.debug(`MONO_WASM: Could not find symbols file ${filename}. Ignoring.`);
}
}
else {
console.log(`MONO_WASM: Error loading symbol file ${filename}: ${JSON.stringify(error)}`);
}
return;
}
}