|
4 | 4 | * SPDX-License-Identifier: MIT
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +#if ASSERTIONS || RUNTIME_DEBUG || AUTODEBUG |
| 8 | +var runtimeDebug = true; // Switch to false at runtime to disable logging at the right times |
| 9 | + |
| 10 | +// Used by XXXXX_DEBUG settings to output debug messages. |
| 11 | +function dbg(...args) { |
| 12 | + if (!runtimeDebug && typeof runtimeDebug != 'undefined') return; |
| 13 | +#if ENVIRONMENT_MAY_BE_NODE && (PTHREADS || WASM_WORKERS) |
| 14 | + // Avoid using the console for debugging in multi-threaded node applications |
| 15 | + // See https://github.com/emscripten-core/emscripten/issues/14804 |
| 16 | + if (ENVIRONMENT_IS_NODE) { |
| 17 | + // TODO(sbc): Unify with err/out implementation in shell.sh. |
| 18 | + var fs = require('fs'); |
| 19 | + var utils = require('util'); |
| 20 | + var stringify = (a) => typeof a == 'object' ? utils.inspect(a) : a; |
| 21 | + fs.writeSync(1, args.map(stringify).join(' ') + '\n'); |
| 22 | + } else |
| 23 | +#endif |
| 24 | + // TODO(sbc): Make this configurable somehow. Its not always convenient for |
| 25 | + // logging to show up as warnings. |
| 26 | + console.warn(...args); |
| 27 | +} |
| 28 | +#endif |
| 29 | + |
7 | 30 | #if ASSERTIONS
|
8 | 31 |
|
9 | 32 | // Endianness check
|
@@ -129,6 +152,45 @@ function unexportedRuntimeSymbol(sym) {
|
129 | 152 | }
|
130 | 153 | }
|
131 | 154 |
|
| 155 | +#if WASM_WORKERS || PTHREADS |
| 156 | +/** |
| 157 | + * Override `err`/`out`/`dbg` to report thread / worker information |
| 158 | + */ |
| 159 | +function initWorkerLogging() { |
| 160 | + function getLogPrefix() { |
| 161 | +#if WASM_WORKERS |
| 162 | + if (Module['$ww']) { |
| 163 | + return `ww:${Module['$ww']}:` |
| 164 | + } |
| 165 | +#endif |
| 166 | +#if PTHREADS |
| 167 | + var t = 0; |
| 168 | + if (runtimeInitialized && typeof _pthread_self != 'undefined' |
| 169 | +#if EXIT_RUNTIME |
| 170 | + && !runtimeExited |
| 171 | +#endif |
| 172 | + ) { |
| 173 | + t = _pthread_self(); |
| 174 | + } |
| 175 | + return `w:${workerID},t:${ptrToString(t)}:`; |
| 176 | +#else |
| 177 | + return `ww:0:`; |
| 178 | +#endif |
| 179 | + } |
| 180 | + |
| 181 | + // Prefix all dbg() messages with the calling thread info. |
| 182 | + var origDbg = dbg; |
| 183 | + dbg = (...args) => origDbg(getLogPrefix(), ...args); |
| 184 | +#if RUNTIME_DEBUG |
| 185 | + // With RUNTIME_DEBUG also prefix all err() messages. |
| 186 | + var origErr = err; |
| 187 | + err = (...args) => origErr(getLogPrefix(), ...args); |
| 188 | +#endif |
| 189 | +} |
| 190 | + |
| 191 | +initWorkerLogging(); |
| 192 | +#endif |
| 193 | + |
132 | 194 | #if ASSERTIONS == 2
|
133 | 195 |
|
134 | 196 | var MAX_UINT8 = (2 ** 8) - 1;
|
@@ -186,26 +248,3 @@ function prettyPrint(arg) {
|
186 | 248 | return arg;
|
187 | 249 | }
|
188 | 250 | #endif
|
189 |
| - |
190 |
| -#if ASSERTIONS || RUNTIME_DEBUG || AUTODEBUG |
191 |
| -var runtimeDebug = true; // Switch to false at runtime to disable logging at the right times |
192 |
| - |
193 |
| -// Used by XXXXX_DEBUG settings to output debug messages. |
194 |
| -function dbg(...args) { |
195 |
| - if (!runtimeDebug && typeof runtimeDebug != 'undefined') return; |
196 |
| -#if ENVIRONMENT_MAY_BE_NODE && (PTHREADS || WASM_WORKERS) |
197 |
| - // Avoid using the console for debugging in multi-threaded node applications |
198 |
| - // See https://github.com/emscripten-core/emscripten/issues/14804 |
199 |
| - if (ENVIRONMENT_IS_NODE) { |
200 |
| - // TODO(sbc): Unify with err/out implementation in shell.sh. |
201 |
| - var fs = require('fs'); |
202 |
| - var utils = require('util'); |
203 |
| - var stringify = (a) => typeof a == 'object' ? utils.inspect(a) : a; |
204 |
| - fs.writeSync(1, args.map(stringify).join(' ') + '\n'); |
205 |
| - } else |
206 |
| -#endif |
207 |
| - // TODO(sbc): Make this configurable somehow. Its not always convenient for |
208 |
| - // logging to show up as warnings. |
209 |
| - console.warn(...args); |
210 |
| -} |
211 |
| -#endif |
|
0 commit comments