Skip to content

Commit d3bad51

Browse files
Add RuntimeUtils#setDebugMode to configure debug logging
closes shannah#38
1 parent dbd1237 commit d3bad51

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

src/main/java/ca/weblite/objc/RuntimeUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,12 @@ public static ByReference getAsReferenceWrapper(Object val, String signature){
10931093
*/
10941094
public static native Recipient getJavaPeer(long nsObject);
10951095

1096-
1096+
/**
1097+
* Configures debug logging. Even if debug logging is disabled, exceptions
1098+
* will still be printed when they occur.
1099+
*
1100+
* @param debugMode whether debug mode should be enabled
1101+
*/
1102+
public static native void setDebugMode(boolean debugMode);
10971103

10981104
}

src/main/objectivec/implementation/WLJavaProxy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
}
2020
+(void)setJVM:(JavaVM*)theJvm;
21+
+(void)setDebugMode:(bool)debugMode;
2122
-(WLJavaProxy*)init:(jobject)peer;
2223
-(NSMethodSignature*)methodSignatureForSelector:(SEL)sel;
2324
-(void)forwardInvocation:(NSInvocation *)invocation;

src/main/objectivec/implementation/WLJavaProxy.m

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "JavaUtil.h"
1111

1212
static JavaVM *jvm = NULL;
13+
static bool debugMode = false;
1314

1415
unsigned long acceptNSRange(NSRange range) {
1516
return range.length + range.location;
@@ -123,23 +124,27 @@ -(NSMethodSignature*)methodSignatureForSelector:(SEL)sel
123124
// return [NSMethodSignature methodSignatureForSelector:sel];
124125
}
125126

127+
#ifndef logIfDebug
128+
#define logIfDebug(args...) if(debugMode) { NSLog(args); }
129+
#endif
130+
126131
-(void)forwardInvocation:(NSInvocation *)invocation
127132
{
128-
NSLog(@"forwardInvocation: %@", invocation);
133+
logIfDebug(@"forwardInvocation: %@", invocation);
129134

130135
JNIEnv *env=0;
131136
SEL aSelector = [invocation selector];
132137
int attach = (*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL);
133138

134139
@try {
135140
if ( attach == 0 ) {
136-
NSLog(@"Before CallBooleanMethod");
141+
logIfDebug(@"Before CallBooleanMethod");
137142

138143
//JNF_COCOA_ENTER(env);
139144
(*env)->CallVoidMethod(env, peer, jForwardInvocation, invocation);
140145
//JNF_COCOA_EXIT(env);
141146

142-
NSLog(@"After CallBooleanMethod");
147+
logIfDebug(@"After CallBooleanMethod");
143148
}
144149
//(*jvm)->DetachCurrentThread(jvm);
145150
} @catch (NSException *e) {
@@ -267,4 +272,8 @@ - (id)forwardInvocationForSelector: (SEL)aSelector withTarget: (id _Nullable)aTa
267272
return nil;
268273
}
269274

275+
+(void)setDebugMode:(bool)_debugMode {
276+
debugMode = _debugMode;
277+
}
278+
270279
@end

src/main/objectivec/implementation/jnaexample_NativeClient_Setup.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/objectivec/implementation/jnaexample_NativeClient_Setup.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ void exceptionHandler(NSException *exception)
7171
}
7272
}
7373

74+
JNIEXPORT void JNICALL Java_ca_weblite_objc_RuntimeUtils_setDebugMode
75+
(JNIEnv *env, jclass cls, jboolean debugMode)
76+
{
77+
[WLJavaProxy setDebugMode:debugMode];
78+
}
79+
7480
/*
7581
JNIEXPORT jobject JNICALL Java_ca_weblite_objc_RuntimeUtils_invokeWithSelfAndTarget
7682
(JNIEnv *env, jclass jcls, jlong selfPtr, jlong target, jlong invocation)

src/test/java/ca/weblite/objc/NSObjectTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ public void testCustomClass(){
119119
myObj = cls.sendProxy("getMyObj");
120120
assertEquals(array, myObj);
121121
}
122+
123+
@Test
124+
public void debugModeTest() {
125+
setDebugMode(true);
126+
TestCustomClass cls = new TestCustomClass();
127+
cls.init("NSObject");
128+
cls.send("myCustomString");
129+
setDebugMode(false);
130+
cls.send("myCustomString");
131+
System.out.println("Exactly one set of debug logs should be printed above");
132+
}
122133

123134
public static class TestCustomClass extends NSObject {
124135
private String str = "My custom string";

0 commit comments

Comments
 (0)