Skip to content

Commit

Permalink
print stack trace only in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vtrifonov authored and darind committed Jun 21, 2019
1 parent 8b5ca4a commit 1f28cf6
Show file tree
Hide file tree
Showing 29 changed files with 242 additions and 82 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [Enable AndroidX and Jetifier(#1370)](https://github.com/NativeScript/android-runtime/issues/1370)
- [Upgrade v8 to 7.5.288.22(#1387)](https://github.com/NativeScript/android-runtime/issues/1387)
- [Upgrade android gradle plugin to the latest 3.4.1 version(#1390)](https://github.com/NativeScript/android-runtime/issues/1390)
- [Remove printStackTrace method calls from the source code(#1359)](https://github.com/NativeScript/android-runtime/issues/1359)

## Bug Fixes

Expand Down
12 changes: 9 additions & 3 deletions test-app/app/src/debug/java/com/tns/ErrorReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public static void verifyStoragePermissions(Activity activity) {
checkSelfPermissionMethod = ActivityCompat.class.getMethod("checkSelfPermission", Context.class, String.class);
} catch (NoSuchMethodException e) {
// method wasn't found, so there is no need to handle permissions explicitly
e.printStackTrace();
if (Util.isDebuggableApp(activity)) {
e.printStackTrace();
}
return;
}

Expand All @@ -104,7 +106,9 @@ public static void verifyStoragePermissions(Activity activity) {
}
} catch (Exception e) {
Toast.makeText(activity, "Couldn't resolve permissions", Toast.LENGTH_LONG).show();
e.printStackTrace();
if (Util.isDebuggableApp(activity)) {
e.printStackTrace();
}
return;
}
}
Expand Down Expand Up @@ -410,7 +414,9 @@ public void onClick(View v) {
} catch (Exception e) {
String err = "Could not write logcat report to sdcard. Make sure you have allowed access to external storage!";
Toast.makeText(activity, err, Toast.LENGTH_LONG).show();
e.printStackTrace();
if (Util.isDebuggableApp(container.getContext())) {
e.printStackTrace();
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion test-app/app/src/debug/java/com/tns/ErrorReportActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis

resetCheckingForPermissions();
} catch (Exception e) {
e.printStackTrace();
if (Util.isDebuggableApp(this)) {
e.printStackTrace();
}
Toast.makeText(this, "Couldn't resolve permissions", Toast.LENGTH_LONG).show();
resetCheckingForPermissions();
}
Expand Down
28 changes: 21 additions & 7 deletions test-app/app/src/debug/java/com/tns/NativeScriptSyncService.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ public void stop() {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
}

Expand All @@ -104,7 +106,9 @@ public void run() {
new Thread(commThread).start();
}
} catch (IOException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
}
}
Expand All @@ -131,11 +135,15 @@ public void run() {
try {
output.write(1);
} catch (IOException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
}
}
Expand All @@ -153,7 +161,9 @@ public static boolean isSyncEnabled(Context context) {
flags = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.flags;
shouldExecuteSync = ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
return false;
}

Expand Down Expand Up @@ -309,11 +319,15 @@ private boolean copyFile(String sourceFile, String destinationFile) {
}
} catch (FileNotFoundException e) {
logger.write("Error copying file " + sourceFile);
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
return false;
} catch (IOException e) {
logger.write("Error copying file " + sourceFile);
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
return false;
} finally {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public void stop() {
try {
deviceSystemSocket.close();
} catch (IOException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
}

Expand All @@ -63,10 +65,14 @@ public void run() {
liveSyncThread.start();
}
} catch (IOException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
catch (java.security.NoSuchAlgorithmException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
}

Expand Down Expand Up @@ -136,7 +142,9 @@ public void run() {

} catch (IOException e) {
logger.write(String.format("Error while LiveSyncing: Client socket might be closed!", e.toString()));
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
try {
do {
Expand Down Expand Up @@ -426,7 +434,9 @@ private void closeWithError(String message) {
logger.write(message);
this.livesyncSocket.close();
} catch (IOException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
}

Expand Down
16 changes: 12 additions & 4 deletions test-app/app/src/main/java/com/tns/AndroidJsV8Inspector.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ private static void sendToDevToolsConsole(Object connection, String message, Str
AndroidJsV8Inspector.send(connection, sendingText);

} catch (JSONException | IOException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
}

Expand Down Expand Up @@ -190,7 +192,9 @@ protected void waitForDebugger(boolean shouldBreak) {
try {
this.debugBrkLock.wait(1000 * 30);
} catch (InterruptedException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
} finally {
AndroidJsV8Inspector.ReadyToProcessMessages.set(true);
this.processDebugBreak();
Expand Down Expand Up @@ -331,7 +335,9 @@ public String getInspectorMessage() {
try {
return inspectorMessages.take();
} catch (InterruptedException e) {
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}

return null;
Expand All @@ -345,7 +351,9 @@ protected void onPong(NanoWSD.WebSocketFrame pong) {
protected void onException(IOException exception) {
// when the chrome inspector is disconnected by closing the tab a "Broken pipe" exception is thrown which we don't need to log, only in verbose logging mode
if(!exception.getMessage().equals("Broken pipe") || currentRuntimeLogger.isEnabled()) {
exception.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
exception.printStackTrace();
}
}
disconnect();
}
Expand Down
20 changes: 15 additions & 5 deletions test-app/app/src/main/java/com/tns/DefaultExtractPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ private String generateAssetsThumb(Context context) {
return updateTime + "-" + code;
} catch (NameNotFoundException e) {
logger.write("Error while getting current assets thumb");
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}

return null;
Expand All @@ -93,10 +95,14 @@ private String getCachedAssetsThumb(String assetsThumbFilePath) {
}
} catch (FileNotFoundException e) {
logger.write("Error while getting current assets thumb");
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
} catch (IOException e) {
logger.write("Error while getting current asstes thumb");
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}

return null;
Expand All @@ -117,10 +123,14 @@ private void saveNewAssetsThumb(String newThumb, String assetsThumbFile) {
}
} catch (FileNotFoundException e) {
logger.write("Error while writing current assets thumb");
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
} catch (IOException e) {
logger.write("Error while writing current assets thumb");
e.printStackTrace();
if (com.tns.Runtime.isDebuggable()) {
e.printStackTrace();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ public void uncaughtException(Thread thread, Throwable ex) {

if (Runtime.isInitialized()) {
try {
// print this only in debug
System.err.println(errorMessage);
if (Util.isDebuggableApp(context)) {
ex.printStackTrace();
}

Runtime runtime = Runtime.getCurrentRuntime();

if (runtime != null) {
runtime.passUncaughtExceptionToJs(ex, ex.getMessage(), stackTraceErrorMessage);
}
} catch (Throwable t) {
t.printStackTrace();
if (Util.isDebuggableApp(context)) {
t.printStackTrace();
}
}
}

Expand All @@ -55,7 +58,9 @@ public void uncaughtException(Thread thread, Throwable ex) {
res = (Boolean) startActivity.invoke(null, context, errorMessage);
} catch (Exception e) {
android.util.Log.v("Error", errorMessage);
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
};
android.util.Log.v("Application Error", "ErrorActivity default implementation not found. Reinstall android platform to fix.");
}
}
Expand Down
36 changes: 27 additions & 9 deletions test-app/app/src/main/java/com/tns/RuntimeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,18 @@ public static Runtime initRuntime(Context context) {
if (logger.isEnabled()) {
logger.write("Error while getting current proxy thumb");
}
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}

String nativeLibDir = null;
try {
nativeLibDir = context.getPackageManager().getApplicationInfo(appName, 0).nativeLibraryDir;
} catch (NameNotFoundException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}

boolean isDebuggable = Util.isDebuggableApp(context);
Expand Down Expand Up @@ -186,7 +190,9 @@ public static Runtime initRuntime(Context context) {

v8Inspector.waitForDebugger(shouldBreak);
} catch (IOException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}

// if app is in debuggable mode run livesync service
Expand All @@ -213,7 +219,9 @@ public static Runtime initRuntime(Context context) {
if (logger.isEnabled()) {
logger.write("Cannot initialize application instance.");
}
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}

if (appConfig.handleTimeZoneChanges()) {
Expand Down Expand Up @@ -320,15 +328,25 @@ public static void initLiveSync(Runtime runtime, Logger logger, Context context)
Method startServerMethod = NativeScriptSyncService.getMethod("startServer");
startServerMethod.invoke(syncService);
} catch (ClassNotFoundException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
} catch (InvocationTargetException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
} catch (InstantiationException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}
}

Expand Down
Loading

0 comments on commit 1f28cf6

Please sign in to comment.