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(android): not override console so it shows proper line #1832

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.getcapacitor.plugin.Browser;
import com.getcapacitor.plugin.Camera;
import com.getcapacitor.plugin.Clipboard;
import com.getcapacitor.plugin.Console;
import com.getcapacitor.plugin.Device;
import com.getcapacitor.plugin.Filesystem;
import com.getcapacitor.plugin.Geolocation;
Expand Down Expand Up @@ -356,7 +355,6 @@ private void registerAllPlugins() {
this.registerPlugin(Browser.class);
this.registerPlugin(Camera.class);
this.registerPlugin(Clipboard.class);
this.registerPlugin(Console.class);
this.registerPlugin(Device.class);
this.registerPlugin(LocalNotifications.class);
this.registerPlugin(Filesystem.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,24 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {

@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
String tag = "Capacitor/Console";
if (consoleMessage.message() != null && isValidMsg(consoleMessage.message())) {
String msg = String.format("File: %s - Line %d - Msg: %s" , consoleMessage.sourceId() , consoleMessage.lineNumber(), consoleMessage.message());
String level = consoleMessage.messageLevel().name();
if ("ERROR".equalsIgnoreCase(level)) {
Log.e(tag, msg);
} else if ("WARNING".equalsIgnoreCase(level)) {
Log.w(tag, msg);
} else if ("TIP".equalsIgnoreCase(level)) {
Log.d(tag, msg);
} else {
Log.i(tag, msg);
}
}
return true;
}

public boolean isValidMsg(String msg) {
return !(msg.contains("%cresult %c") || (msg.contains("%cnative %c")) || msg.equalsIgnoreCase("[object Object]") || msg.equalsIgnoreCase("console.groupEnd"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public void postMessage(String jsonStr) {
String pluginId = postData.getString("pluginId");
String methodName = postData.getString("methodName");
JSObject methodData = postData.getJSObject("options", new JSObject());
if (!pluginId.equals("Console")) {
Log.v(LogUtils.getPluginTag(), "To native (Capacitor plugin): callbackId: " + callbackId + ", pluginId: " + pluginId +
", methodName: " + methodName);
}
Log.v(LogUtils.getPluginTag(), "To native (Capacitor plugin): callbackId: " + callbackId + ", pluginId: " + pluginId + ", methodName: " + methodName);
this.callPluginMethod(callbackId, pluginId, methodName, methodData);
}

Expand Down

This file was deleted.

59 changes: 29 additions & 30 deletions core/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,37 @@
if (typeof win.console[level] === 'function') {
// loop through all the console functions and keep references to the original
orgConsole[level] = win.console[level];
if (capacitor.isIOS) {
win.console[level] = function capacitorConsole() {
var msgs = Array.prototype.slice.call(arguments);

win.console[level] = function capacitorConsole() {
var msgs = Array.prototype.slice.call(arguments);

// console log to browser
orgConsole[level].apply(win.console, msgs);

if (capacitor.isNative && bridgedLevels[level]) {
// send log to native to print
try {
// convert all args to strings
msgs = msgs.map(function (arg) {
if (typeof arg === 'object') {
try {
arg = JSON.stringify(arg);
} catch (e) {}
}
// convert to string
return arg + '';
});
capacitor.toNative('Console', 'log', {
level: level,
message: msgs.join(' ')
});
// console log to browser
orgConsole[level].apply(win.console, msgs);

} catch (e) {
// error converting/posting console messages
orgConsole.error.apply(win.console, e);
if (capacitor.isNative && bridgedLevels[level]) {
// send log to native to print
try {
// convert all args to strings
msgs = msgs.map(function (arg) {
if (typeof arg === 'object') {
try {
arg = JSON.stringify(arg);
} catch (e) {}
}
// convert to string
return arg + '';
});
capacitor.toNative('Console', 'log', {
level: level,
message: msgs.join(' ')
});
} catch (e) {
// error converting/posting console messages
orgConsole.error.apply(win.console, e);
}
}
}
};
};
}
}
});

Expand Down Expand Up @@ -384,10 +384,9 @@
c.groupCollapsed('%cnative %c' + call.pluginId + '.' + call.methodName + ' (#' + call.callbackId + ')', 'font-weight: lighter; color: gray', 'font-weight: bold; color: #000');
c.dir(call);
c.groupEnd();
//orgConsole.log('LOG TO NATIVE', call);
} else {
win.console.log('LOG TO NATIVE: ', call);
if (capacitor.isNative) {
if (capacitor.isIOS) {
try {
capacitor.toNative('Console', 'log', {message: JSON.stringify(call)});
} catch (e) {
Expand Down