UNPKG

3.9 kBJavaScriptView Raw
1import RemoteLogging from './RemoteLogging';
2/**
3 * Creates a console object that delegates calls to the specified underlying console and also sends
4 * the messages to the development environment over a remote connection.
5 */
6function createRemoteConsole(originalConsole) {
7 let groupDepth = 0;
8 const enhancedConsole = Object.create(originalConsole);
9 // https://console.spec.whatwg.org/#debug
10 // Don't use a level below "info" because "debug" is intended for messages that shouldn't be shown
11 // to the developer
12 _defineConsoleLogMethod('debug', 'info');
13 // https://console.spec.whatwg.org/#log
14 _defineConsoleLogMethod('log', 'info');
15 // https://console.spec.whatwg.org/#info
16 _defineConsoleLogMethod('info', 'info');
17 // https://console.spec.whatwg.org/#warn
18 _defineConsoleLogMethod('warn', 'warn');
19 // https://console.spec.whatwg.org/#error
20 _defineConsoleLogMethod('error', 'error');
21 // https://console.spec.whatwg.org/#assert
22 enhancedConsole.assert = function assert(condition, ...data) {
23 if (originalConsole.assert) {
24 // @ts-ignore
25 originalConsole.assert(!!condition, ...data);
26 }
27 if (condition) {
28 return;
29 }
30 const assertionMessage = 'Assertion failed';
31 if (!data.length) {
32 data.push(assertionMessage);
33 }
34 else {
35 if (typeof data[0] !== 'string') {
36 data.unshift(assertionMessage);
37 }
38 else {
39 data[0] = `${assertionMessage}: ${data[0]}`;
40 }
41 }
42 _enqueueRemoteLog('error', {}, data);
43 };
44 // https://console.spec.whatwg.org/#group
45 enhancedConsole.group = function group(...data) {
46 if (originalConsole.group) {
47 // @ts-ignore
48 originalConsole.group(...data);
49 }
50 _enqueueRemoteLog('info', {}, data);
51 groupDepth++;
52 };
53 // https://console.spec.whatwg.org/#groupcollapsed
54 enhancedConsole.groupCollapsed = function groupCollapsed(...data) {
55 if (originalConsole.groupCollapsed) {
56 // @ts-ignore
57 originalConsole.groupCollapsed(...data);
58 }
59 _enqueueRemoteLog('info', { groupCollapsed: true }, data);
60 groupDepth++;
61 };
62 // https://console.spec.whatwg.org/#groupend
63 enhancedConsole.groupEnd = function groupEnd() {
64 if (originalConsole.groupEnd) {
65 originalConsole.groupEnd();
66 }
67 if (groupDepth > 0) {
68 groupDepth--;
69 }
70 _enqueueRemoteLog('info', { shouldHide: true }, []);
71 };
72 /**
73 * Defines a method in the `console.log()` family on the enhanced console
74 * instance
75 */
76 function _defineConsoleLogMethod(name, level) {
77 enhancedConsole[name] = function __expoConsoleLog(...data) {
78 const originalMethod = originalConsole[name];
79 if (typeof originalMethod === 'function') {
80 originalMethod.apply(originalConsole, data);
81 }
82 _enqueueRemoteLog(level, {}, data);
83 };
84 }
85 /**
86 * Schedules the given log entry to be sent remotely in a safe way that handles all errors. This
87 * function is responsible for error handling because the console methods are synchronous but
88 * sending log messages is asynchronous, so this code (instead of the console methods) needs to be
89 * responsible for asynchronous errors.
90 */
91 function _enqueueRemoteLog(level, additionalFields, data) {
92 RemoteLogging.enqueueRemoteLogAsync(level, { groupDepth, ...additionalFields }, data).catch(error => {
93 originalConsole.error(`There was a problem sending log messages to your development environment`, error);
94 });
95 }
96 return enhancedConsole;
97}
98export default {
99 createRemoteConsole,
100};
101//# sourceMappingURL=RemoteConsole.js.map
\No newline at end of file