Skip to content

Commit b64a1bf

Browse files
committed
print stack trace only in debug mode
1 parent b1f87d0 commit b64a1bf

27 files changed

+216
-80
lines changed

test-app/app/src/debug/java/com/tns/ErrorReport.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ public static void verifyStoragePermissions(Activity activity) {
8888
checkSelfPermissionMethod = ActivityCompat.class.getMethod("checkSelfPermission", Context.class, String.class);
8989
} catch (NoSuchMethodException e) {
9090
// method wasn't found, so there is no need to handle permissions explicitly
91-
e.printStackTrace();
91+
if (Util.isDebuggableApp(activity)) {
92+
e.printStackTrace();
93+
}
9294
return;
9395
}
9496

@@ -104,7 +106,9 @@ public static void verifyStoragePermissions(Activity activity) {
104106
}
105107
} catch (Exception e) {
106108
Toast.makeText(activity, "Couldn't resolve permissions", Toast.LENGTH_LONG).show();
107-
e.printStackTrace();
109+
if (Util.isDebuggableApp(activity)) {
110+
e.printStackTrace();
111+
}
108112
return;
109113
}
110114
}
@@ -410,7 +414,9 @@ public void onClick(View v) {
410414
} catch (Exception e) {
411415
String err = "Could not write logcat report to sdcard. Make sure you have allowed access to external storage!";
412416
Toast.makeText(activity, err, Toast.LENGTH_LONG).show();
413-
e.printStackTrace();
417+
if (Util.isDebuggableApp(container.getContext())) {
418+
e.printStackTrace();
419+
}
414420
}
415421
}
416422
}

test-app/app/src/debug/java/com/tns/ErrorReportActivity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
4141

4242
resetCheckingForPermissions();
4343
} catch (Exception e) {
44-
e.printStackTrace();
44+
if (Util.isDebuggableApp(this)) {
45+
e.printStackTrace();
46+
}
4547
Toast.makeText(this, "Couldn't resolve permissions", Toast.LENGTH_LONG).show();
4648
resetCheckingForPermissions();
4749
}

test-app/app/src/debug/java/com/tns/NativeScriptSyncService.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ public void stop() {
9090
try {
9191
serverSocket.close();
9292
} catch (IOException e) {
93-
e.printStackTrace();
93+
if (com.tns.Runtime.isDebuggable()) {
94+
e.printStackTrace();
95+
}
9496
}
9597
}
9698

@@ -104,7 +106,9 @@ public void run() {
104106
new Thread(commThread).start();
105107
}
106108
} catch (IOException e) {
107-
e.printStackTrace();
109+
if (com.tns.Runtime.isDebuggable()) {
110+
e.printStackTrace();
111+
}
108112
}
109113
}
110114
}
@@ -131,11 +135,15 @@ public void run() {
131135
try {
132136
output.write(1);
133137
} catch (IOException e) {
134-
e.printStackTrace();
138+
if (com.tns.Runtime.isDebuggable()) {
139+
e.printStackTrace();
140+
}
135141
}
136142
socket.close();
137143
} catch (IOException e) {
138-
e.printStackTrace();
144+
if (com.tns.Runtime.isDebuggable()) {
145+
e.printStackTrace();
146+
}
139147
}
140148
}
141149
}
@@ -153,7 +161,9 @@ public static boolean isSyncEnabled(Context context) {
153161
flags = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.flags;
154162
shouldExecuteSync = ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
155163
} catch (NameNotFoundException e) {
156-
e.printStackTrace();
164+
if (com.tns.Runtime.isDebuggable()) {
165+
e.printStackTrace();
166+
}
157167
return false;
158168
}
159169

@@ -309,11 +319,15 @@ private boolean copyFile(String sourceFile, String destinationFile) {
309319
}
310320
} catch (FileNotFoundException e) {
311321
logger.write("Error copying file " + sourceFile);
312-
e.printStackTrace();
322+
if (com.tns.Runtime.isDebuggable()) {
323+
e.printStackTrace();
324+
}
313325
return false;
314326
} catch (IOException e) {
315327
logger.write("Error copying file " + sourceFile);
316-
e.printStackTrace();
328+
if (com.tns.Runtime.isDebuggable()) {
329+
e.printStackTrace();
330+
}
317331
return false;
318332
} finally {
319333
try {

test-app/app/src/debug/java/com/tns/NativeScriptSyncServiceSocketImpl.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public void stop() {
4848
try {
4949
deviceSystemSocket.close();
5050
} catch (IOException e) {
51-
e.printStackTrace();
51+
if (com.tns.Runtime.isDebuggable()) {
52+
e.printStackTrace();
53+
}
5254
}
5355
}
5456

@@ -63,10 +65,14 @@ public void run() {
6365
liveSyncThread.start();
6466
}
6567
} catch (IOException e) {
66-
e.printStackTrace();
68+
if (com.tns.Runtime.isDebuggable()) {
69+
e.printStackTrace();
70+
}
6771
}
6872
catch (java.security.NoSuchAlgorithmException e) {
69-
e.printStackTrace();
73+
if (com.tns.Runtime.isDebuggable()) {
74+
e.printStackTrace();
75+
}
7076
}
7177
}
7278

@@ -136,7 +142,9 @@ public void run() {
136142

137143
} catch (IOException e) {
138144
logger.write(String.format("Error while LiveSyncing: Client socket might be closed!", e.toString()));
139-
e.printStackTrace();
145+
if (com.tns.Runtime.isDebuggable()) {
146+
e.printStackTrace();
147+
}
140148
}
141149
try {
142150
do {
@@ -426,7 +434,9 @@ private void closeWithError(String message) {
426434
logger.write(message);
427435
this.livesyncSocket.close();
428436
} catch (IOException e) {
429-
e.printStackTrace();
437+
if (com.tns.Runtime.isDebuggable()) {
438+
e.printStackTrace();
439+
}
430440
}
431441
}
432442

test-app/app/src/main/java/com/tns/AndroidJsV8Inspector.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ private static void sendToDevToolsConsole(Object connection, String message, Str
9494
AndroidJsV8Inspector.send(connection, sendingText);
9595

9696
} catch (JSONException | IOException e) {
97-
e.printStackTrace();
97+
if (com.tns.Runtime.isDebuggable()) {
98+
e.printStackTrace();
99+
}
98100
}
99101
}
100102

@@ -190,7 +192,9 @@ protected void waitForDebugger(boolean shouldBreak) {
190192
try {
191193
this.debugBrkLock.wait(1000 * 30);
192194
} catch (InterruptedException e) {
193-
e.printStackTrace();
195+
if (com.tns.Runtime.isDebuggable()) {
196+
e.printStackTrace();
197+
}
194198
} finally {
195199
AndroidJsV8Inspector.ReadyToProcessMessages.set(true);
196200
this.processDebugBreak();
@@ -331,7 +335,9 @@ public String getInspectorMessage() {
331335
try {
332336
return inspectorMessages.take();
333337
} catch (InterruptedException e) {
334-
e.printStackTrace();
338+
if (com.tns.Runtime.isDebuggable()) {
339+
e.printStackTrace();
340+
}
335341
}
336342

337343
return null;
@@ -345,7 +351,9 @@ protected void onPong(NanoWSD.WebSocketFrame pong) {
345351
protected void onException(IOException exception) {
346352
// 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
347353
if(!exception.getMessage().equals("Broken pipe") || currentRuntimeLogger.isEnabled()) {
348-
exception.printStackTrace();
354+
if (com.tns.Runtime.isDebuggable()) {
355+
exception.printStackTrace();
356+
}
349357
}
350358
disconnect();
351359
}

test-app/app/src/main/java/com/tns/DefaultExtractPolicy.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ private String generateAssetsThumb(Context context) {
7474
return updateTime + "-" + code;
7575
} catch (NameNotFoundException e) {
7676
logger.write("Error while getting current assets thumb");
77-
e.printStackTrace();
77+
if (com.tns.Runtime.isDebuggable()) {
78+
e.printStackTrace();
79+
}
7880
}
7981

8082
return null;
@@ -93,10 +95,14 @@ private String getCachedAssetsThumb(String assetsThumbFilePath) {
9395
}
9496
} catch (FileNotFoundException e) {
9597
logger.write("Error while getting current assets thumb");
96-
e.printStackTrace();
98+
if (com.tns.Runtime.isDebuggable()) {
99+
e.printStackTrace();
100+
}
97101
} catch (IOException e) {
98102
logger.write("Error while getting current asstes thumb");
99-
e.printStackTrace();
103+
if (com.tns.Runtime.isDebuggable()) {
104+
e.printStackTrace();
105+
}
100106
}
101107

102108
return null;
@@ -117,10 +123,14 @@ private void saveNewAssetsThumb(String newThumb, String assetsThumbFile) {
117123
}
118124
} catch (FileNotFoundException e) {
119125
logger.write("Error while writing current assets thumb");
120-
e.printStackTrace();
126+
if (com.tns.Runtime.isDebuggable()) {
127+
e.printStackTrace();
128+
}
121129
} catch (IOException e) {
122130
logger.write("Error while writing current assets thumb");
123-
e.printStackTrace();
131+
if (com.tns.Runtime.isDebuggable()) {
132+
e.printStackTrace();
133+
}
124134
}
125135
}
126136

test-app/app/src/main/java/com/tns/NativeScriptUncaughtExceptionHandler.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@ public void uncaughtException(Thread thread, Throwable ex) {
2525

2626
if (Runtime.isInitialized()) {
2727
try {
28-
ex.printStackTrace();
28+
if (Util.isDebuggableApp(context)) {
29+
ex.printStackTrace();
30+
}
2931

3032
Runtime runtime = Runtime.getCurrentRuntime();
3133

3234
if (runtime != null) {
3335
runtime.passUncaughtExceptionToJs(ex, errorMessage);
3436
}
3537
} catch (Throwable t) {
36-
t.printStackTrace();
38+
if (Util.isDebuggableApp(context)) {
39+
t.printStackTrace();
40+
}
3741
}
3842
}
3943

@@ -55,7 +59,9 @@ public void uncaughtException(Thread thread, Throwable ex) {
5559
res = (Boolean) startActivity.invoke(null, context, errorMessage);
5660
} catch (Exception e) {
5761
android.util.Log.v("Error", errorMessage);
58-
e.printStackTrace();
62+
if (Util.isDebuggableApp(context)) {
63+
e.printStackTrace();
64+
};
5965
android.util.Log.v("Application Error", "ErrorActivity default implementation not found. Reinstall android platform to fix.");
6066
}
6167
}

test-app/app/src/main/java/com/tns/RuntimeHelper.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,18 @@ public static Runtime initRuntime(Context context) {
142142
if (logger.isEnabled()) {
143143
logger.write("Error while getting current proxy thumb");
144144
}
145-
e.printStackTrace();
145+
if (Util.isDebuggableApp(context)) {
146+
e.printStackTrace();
147+
}
146148
}
147149

148150
String nativeLibDir = null;
149151
try {
150152
nativeLibDir = context.getPackageManager().getApplicationInfo(appName, 0).nativeLibraryDir;
151153
} catch (NameNotFoundException e) {
152-
e.printStackTrace();
154+
if (Util.isDebuggableApp(context)) {
155+
e.printStackTrace();
156+
}
153157
}
154158

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

187191
v8Inspector.waitForDebugger(shouldBreak);
188192
} catch (IOException e) {
189-
e.printStackTrace();
193+
if (Util.isDebuggableApp(context)) {
194+
e.printStackTrace();
195+
}
190196
}
191197

192198
// if app is in debuggable mode run livesync service
@@ -213,7 +219,9 @@ public static Runtime initRuntime(Context context) {
213219
if (logger.isEnabled()) {
214220
logger.write("Cannot initialize application instance.");
215221
}
216-
e.printStackTrace();
222+
if (Util.isDebuggableApp(context)) {
223+
e.printStackTrace();
224+
}
217225
}
218226

219227
if (appConfig.handleTimeZoneChanges()) {
@@ -320,15 +328,25 @@ public static void initLiveSync(Runtime runtime, Logger logger, Context context)
320328
Method startServerMethod = NativeScriptSyncService.getMethod("startServer");
321329
startServerMethod.invoke(syncService);
322330
} catch (ClassNotFoundException e) {
323-
e.printStackTrace();
331+
if (Util.isDebuggableApp(context)) {
332+
e.printStackTrace();
333+
}
324334
} catch (NoSuchMethodException e) {
325-
e.printStackTrace();
335+
if (Util.isDebuggableApp(context)) {
336+
e.printStackTrace();
337+
}
326338
} catch (IllegalAccessException e) {
327-
e.printStackTrace();
339+
if (Util.isDebuggableApp(context)) {
340+
e.printStackTrace();
341+
}
328342
} catch (InvocationTargetException e) {
329-
e.printStackTrace();
343+
if (Util.isDebuggableApp(context)) {
344+
e.printStackTrace();
345+
}
330346
} catch (InstantiationException e) {
331-
e.printStackTrace();
347+
if (Util.isDebuggableApp(context)) {
348+
e.printStackTrace();
349+
}
332350
}
333351
}
334352

test-app/app/src/main/java/com/tns/Util.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.content.pm.PackageManager;
1111
import android.content.pm.PackageManager.NameNotFoundException;
1212
import android.os.Bundle;
13+
import android.util.Log;
1314

1415
import androidx.core.content.pm.PackageInfoCompat;
1516

@@ -30,7 +31,7 @@ public static boolean isDebuggableApp(Context context) {
3031
flags = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.flags;
3132
} catch (NameNotFoundException e) {
3233
flags = 0;
33-
e.printStackTrace();
34+
Log.w("JS", String.format("Error getting application flags: %s", e.getMessage()));
3435
}
3536

3637
boolean isDebuggableApp = ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
@@ -47,7 +48,7 @@ static boolean runPlugin(Logger logger, Context context) {
4748
pluginClassName = metadataBundle.getString("com.tns.internal.Plugin");
4849
}
4950
} catch (Exception e) {
50-
if (logger.isEnabled()) {
51+
if (Util.isDebuggableApp(context)) {
5152
e.printStackTrace();
5253
}
5354
}
@@ -57,7 +58,7 @@ static boolean runPlugin(Logger logger, Context context) {
5758
Plugin p = (Plugin) liveSyncPluginClass.newInstance();
5859
success = p.execute(context);
5960
} catch (Exception e) {
60-
if (logger.isEnabled()) {
61+
if (Util.isDebuggableApp(context)) {
6162
e.printStackTrace();
6263
}
6364
}

0 commit comments

Comments
 (0)