Skip to content

Add RuntimeUtils#setDebugMode to configure debug logging #45

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Add the following dependency to your pom.xml:

## JavaDocs & Documentation

* [Documentation](https://solutions.weblite.ca/maven/java-objc-bridge/apidocs/index.html)
* [Documentation](https://solutions.weblite.ca/java-objective-c-bridge/docs/)
* Read a [blog post](https://sjhannah.com/blog/2012/10/29/speaking-cocoa-from-java/) about the motivation for this project.

## Contact
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/ca/weblite/objc/RuntimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,12 @@ public static ByReference getAsReferenceWrapper(Object val, String signature){
*/
public static native Recipient getJavaPeer(long nsObject);


/**
* Configures debug logging. Even if debug logging is disabled, exceptions
* will still be printed when they occur. Debug mode is disabled by default.
*
* @param debugMode whether debug mode should be enabled
*/
public static native void setDebugMode(boolean debugMode);

}
1 change: 1 addition & 0 deletions src/main/objectivec/implementation/WLJavaProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

}
+(void)setJVM:(JavaVM*)theJvm;
+(void)setDebugMode:(bool)debugMode;
-(WLJavaProxy*)init:(jobject)peer;
-(NSMethodSignature*)methodSignatureForSelector:(SEL)sel;
-(void)forwardInvocation:(NSInvocation *)invocation;
Expand Down
15 changes: 12 additions & 3 deletions src/main/objectivec/implementation/WLJavaProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "JavaUtil.h"

static JavaVM *jvm = NULL;
static bool debugMode = false;

unsigned long acceptNSRange(NSRange range) {
return range.length + range.location;
Expand Down Expand Up @@ -123,23 +124,27 @@ -(NSMethodSignature*)methodSignatureForSelector:(SEL)sel
// return [NSMethodSignature methodSignatureForSelector:sel];
}

#ifndef logIfDebug
#define logIfDebug(args...) if(debugMode) { NSLog(args); }
#endif

-(void)forwardInvocation:(NSInvocation *)invocation
{
NSLog(@"forwardInvocation: %@", invocation);
logIfDebug(@"forwardInvocation: %@", invocation);

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

@try {
if ( attach == 0 ) {
NSLog(@"Before CallBooleanMethod");
logIfDebug(@"Before CallBooleanMethod");

//JNF_COCOA_ENTER(env);
(*env)->CallVoidMethod(env, peer, jForwardInvocation, invocation);
//JNF_COCOA_EXIT(env);

NSLog(@"After CallBooleanMethod");
logIfDebug(@"After CallBooleanMethod");
}
//(*jvm)->DetachCurrentThread(jvm);
} @catch (NSException *e) {
Expand Down Expand Up @@ -267,4 +272,8 @@ - (id)forwardInvocationForSelector: (SEL)aSelector withTarget: (id _Nullable)aTa
return nil;
}

+(void)setDebugMode:(bool)_debugMode {
debugMode = _debugMode;
}

@end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ void exceptionHandler(NSException *exception)
}
}

JNIEXPORT void JNICALL Java_ca_weblite_objc_RuntimeUtils_setDebugMode
(JNIEnv *env, jclass cls, jboolean debugMode)
{
[WLJavaProxy setDebugMode:debugMode];
}

/*
JNIEXPORT jobject JNICALL Java_ca_weblite_objc_RuntimeUtils_invokeWithSelfAndTarget
(JNIEnv *env, jclass jcls, jlong selfPtr, jlong target, jlong invocation)
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/ca/weblite/objc/NSObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ public void testCustomClass(){
myObj = cls.sendProxy("getMyObj");
assertEquals(array, myObj);
}

@Test
public void debugModeTest() {
setDebugMode(true);
TestCustomClass cls = new TestCustomClass();
cls.init("NSObject");
cls.send("myCustomString");
setDebugMode(false);
cls.send("myCustomString");
System.out.println("Exactly one set of debug logs should be printed above");
}

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