Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): show correct line number on console logs #5073

Merged
merged 2 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,24 +258,21 @@ const nativeBridge = (function (exports) {
};
// patch window.console on iOS and store original console fns
const isIos = getPlatformId(win) === 'ios';
const originalConsole = Object.assign({}, win.console);
if (win.console && isIos) {
for (const logfn of BRIDGED_CONSOLE_METHODS) {
win.console[logfn] = (...args) => {
const msgs = [...args];
originalConsole[logfn](...msgs);
try {
Object.defineProperties(win.console, BRIDGED_CONSOLE_METHODS.reduce((props, method) => {
const consoleMethod = win.console[method].bind(win.console);
props[method] = {
value: (...args) => {
const msgs = [...args];
cap.toNative('Console', 'log', {
level: logfn,
level: method,
message: msgs.map(serializeConsoleMessage).join(' '),
});
}
catch (e) {
// error converting/posting console messages
originalConsole.error(e);
}
return consoleMethod(...args);
},
};
}
return props;
}, {}));
}
cap.logJs = (msg, level) => {
switch (level) {
Expand Down Expand Up @@ -501,4 +498,4 @@ const nativeBridge = (function (exports) {

return exports;

}({}));
})({});
36 changes: 17 additions & 19 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,26 +302,24 @@ const initBridge = (w: any): void => {

// patch window.console on iOS and store original console fns
const isIos = getPlatformId(win) === 'ios';
const originalConsole: any = { ...win.console };

if (win.console && isIos) {
for (const logfn of BRIDGED_CONSOLE_METHODS) {
(win.console[logfn] as any) = (...args: any[]) => {
const msgs = [...args];

originalConsole[logfn](...msgs);

try {
cap.toNative('Console', 'log', {
level: logfn,
message: msgs.map(serializeConsoleMessage).join(' '),
});
} catch (e) {
// error converting/posting console messages
originalConsole.error(e);
}
};
}
Object.defineProperties(
win.console,
BRIDGED_CONSOLE_METHODS.reduce((props, method) => {
const consoleMethod = win.console[method].bind(win.console);
props[method] = {
value: (...args: any[]) => {
const msgs = [...args];
cap.toNative('Console', 'log', {
level: method,
message: msgs.map(serializeConsoleMessage).join(' '),
});
return consoleMethod(...args);
},
};
return props;
}, {}),
);
}

cap.logJs = (msg, level) => {
Expand Down
25 changes: 11 additions & 14 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,24 +258,21 @@ const nativeBridge = (function (exports) {
};
// patch window.console on iOS and store original console fns
const isIos = getPlatformId(win) === 'ios';
const originalConsole = Object.assign({}, win.console);
if (win.console && isIos) {
for (const logfn of BRIDGED_CONSOLE_METHODS) {
win.console[logfn] = (...args) => {
const msgs = [...args];
originalConsole[logfn](...msgs);
try {
Object.defineProperties(win.console, BRIDGED_CONSOLE_METHODS.reduce((props, method) => {
const consoleMethod = win.console[method].bind(win.console);
props[method] = {
value: (...args) => {
const msgs = [...args];
cap.toNative('Console', 'log', {
level: logfn,
level: method,
message: msgs.map(serializeConsoleMessage).join(' '),
});
}
catch (e) {
// error converting/posting console messages
originalConsole.error(e);
}
return consoleMethod(...args);
},
};
}
return props;
}, {}));
}
cap.logJs = (msg, level) => {
switch (level) {
Expand Down Expand Up @@ -501,4 +498,4 @@ const nativeBridge = (function (exports) {

return exports;

}({}));
})({});