1 | import RemoteLogging from './RemoteLogging';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | function createRemoteConsole(originalConsole) {
|
7 | let groupDepth = 0;
|
8 | const enhancedConsole = Object.create(originalConsole);
|
9 |
|
10 |
|
11 |
|
12 | _defineConsoleLogMethod('debug', 'info');
|
13 |
|
14 | _defineConsoleLogMethod('log', 'info');
|
15 |
|
16 | _defineConsoleLogMethod('info', 'info');
|
17 |
|
18 | _defineConsoleLogMethod('warn', 'warn');
|
19 |
|
20 | _defineConsoleLogMethod('error', 'error');
|
21 |
|
22 | enhancedConsole.assert = function assert(condition, ...data) {
|
23 | if (originalConsole.assert) {
|
24 |
|
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 |
|
45 | enhancedConsole.group = function group(...data) {
|
46 | if (originalConsole.group) {
|
47 |
|
48 | originalConsole.group(...data);
|
49 | }
|
50 | _enqueueRemoteLog('info', {}, data);
|
51 | groupDepth++;
|
52 | };
|
53 |
|
54 | enhancedConsole.groupCollapsed = function groupCollapsed(...data) {
|
55 | if (originalConsole.groupCollapsed) {
|
56 |
|
57 | originalConsole.groupCollapsed(...data);
|
58 | }
|
59 | _enqueueRemoteLog('info', { groupCollapsed: true }, data);
|
60 | groupDepth++;
|
61 | };
|
62 |
|
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 |
|
74 |
|
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 |
|
87 |
|
88 |
|
89 |
|
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 | }
|
98 | export default {
|
99 | createRemoteConsole,
|
100 | };
|
101 |
|
\ | No newline at end of file |