Skip to content

Commit fb29f33

Browse files
committed
fix: diagnostic logging for code_cache handling
1 parent 2b6cb03 commit fb29f33

3 files changed

Lines changed: 65 additions & 12 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ public static Runtime initRuntime(Context context) {
127127
dexDir.mkdirs();
128128
}
129129
if (!dexDir.exists() || !dexDir.canWrite()) {
130-
if (logger.isEnabled()) {
131-
logger.write("Unable to use dex dir: " + dexDir.getAbsolutePath() + ", falling back to files/secondary-dexes");
132-
}
130+
File primary = dexDir;
133131
dexDir = new File(appDir, "secondary-dexes");
134132
if (!dexDir.exists()) {
135133
dexDir.mkdirs();
136134
}
135+
Log.w(logTag, "Unable to use primary dex dir '" + primary.getAbsolutePath()
136+
+ "' (exists=" + primary.exists() + " canWrite=" + primary.canWrite()
137+
+ "); falling back to '" + dexDir.getAbsolutePath()
138+
+ "' (exists=" + dexDir.exists() + " canWrite=" + dexDir.canWrite() + ")");
137139
}
138140
String dexThumb = null;
139141
try {

test-app/runtime-binding-generator/src/main/java/com/tns/bindings/ProxyGenerator.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,46 @@ private String saveProxy(String proxyName, byte[] proxyBytes) throws IOException
6262
if (parentDir != null && !parentDir.exists()) {
6363
parentDir.mkdirs();
6464
}
65-
file.createNewFile();
66-
FileOutputStream stream = new FileOutputStream(file);
67-
stream.write(proxyBytes);
68-
stream.flush();
69-
stream.close();
65+
try {
66+
file.createNewFile();
67+
FileOutputStream stream = new FileOutputStream(file);
68+
try {
69+
stream.write(proxyBytes);
70+
stream.flush();
71+
} finally {
72+
stream.close();
73+
}
74+
} catch (IOException e) {
75+
throw new IOException("Failed to save proxy dex '" + file.getAbsolutePath()
76+
+ "' (" + proxyBytes.length + " bytes): " + e.getMessage()
77+
+ " | " + describeDexDirState(file, parentDir), e);
78+
}
7079

7180
return file.getAbsolutePath();
7281
}
82+
83+
private static String describeDexDirState(File file, File parentDir) {
84+
StringBuilder sb = new StringBuilder();
85+
sb.append("file.exists=").append(file.exists());
86+
if (parentDir != null) {
87+
sb.append(" parent=").append(parentDir.getAbsolutePath());
88+
sb.append(" parent.exists=").append(parentDir.exists());
89+
sb.append(" parent.isDir=").append(parentDir.isDirectory());
90+
sb.append(" parent.canWrite=").append(parentDir.canWrite());
91+
File grand = parentDir.getParentFile();
92+
if (grand != null) {
93+
sb.append(" grand=").append(grand.getAbsolutePath());
94+
sb.append(" grand.exists=").append(grand.exists());
95+
sb.append(" grand.canWrite=").append(grand.canWrite());
96+
}
97+
try {
98+
sb.append(" parent.freeSpace=").append(parentDir.getFreeSpace());
99+
sb.append(" parent.usableSpace=").append(parentDir.getUsableSpace());
100+
} catch (Throwable ignored) {
101+
}
102+
} else {
103+
sb.append(" parent=null");
104+
}
105+
return sb.toString();
106+
}
73107
}

test-app/runtime/src/main/java/com/tns/DexFactory.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public class DexFactory {
5656
odexDir.mkdir();
5757
}
5858

59+
if (!dexDir.exists() || !dexDir.canWrite()) {
60+
Log.w("JS", "DexFactory: dexDir unusable at init path=" + dexDir.getAbsolutePath()
61+
+ " exists=" + dexDir.exists() + " canWrite=" + dexDir.canWrite());
62+
}
63+
5964
this.updateDexThumbAndPurgeCache();
6065
this.proxyGenerator.setProxyThumb(this.dexThumb);
6166
this.classStorageService = classStorageService;
@@ -123,10 +128,22 @@ public Class<?> resolveClass(String baseClassName, String name, String className
123128
}
124129

125130
String dexFilePath;
126-
if (isInterface) {
127-
dexFilePath = this.generateDex(name, classToProxy, methodOverrides, implementedInterfaces, isInterface);
128-
} else {
129-
dexFilePath = this.generateDex(desiredDexClassName, classToProxy, methodOverrides, implementedInterfaces, isInterface);
131+
try {
132+
if (isInterface) {
133+
dexFilePath = this.generateDex(name, classToProxy, methodOverrides, implementedInterfaces, isInterface);
134+
} else {
135+
dexFilePath = this.generateDex(desiredDexClassName, classToProxy, methodOverrides, implementedInterfaces, isInterface);
136+
}
137+
} catch (IOException e) {
138+
String diag = "DexFactory.generateDex failed class=" + className
139+
+ " baseClass=" + baseClassName
140+
+ " isInterface=" + isInterface
141+
+ " dexDir=" + dexDir.getAbsolutePath()
142+
+ " dexDir.exists=" + dexDir.exists()
143+
+ " dexDir.canWrite=" + dexDir.canWrite()
144+
+ " dexThumb=" + dexThumb;
145+
Log.e("JS", diag + " | " + e.getMessage());
146+
throw new IOException(diag + " | " + e.getMessage(), e);
130147
}
131148
dexFile = new File(dexFilePath);
132149
long stopGenTime = System.nanoTime();

0 commit comments

Comments
 (0)