Expose CSSLayoutSetLogger to java

Summary: Expose CSSLayoutSetLogger to java

Reviewed By: astreet

Differential Revision: D4153502

fbshipit-source-id: 630909d9d0d36d94d7cd3027476ddb52668b8cc0
This commit is contained in:
Emil Sjolander
2016-11-11 08:08:28 -08:00
committed by Facebook Github Bot
parent aaa977f645
commit cd054ecf26
4 changed files with 100 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.csslayout;
import com.facebook.proguard.annotations.DoNotStrip;
/**
* Inteface for recieving logs from native layer. Use by setting CSSNode.setLogger(myLogger);
* LOG_LEVEL_ERROR indicated a fatal error.
*/
public interface CSSLogger {
public final int LOG_LEVEL_ERROR = 0;
public final int LOG_LEVEL_WARN = 1;
public final int LOG_LEVEL_INFO = 2;
public final int LOG_LEVEL_DEBUG = 3;
public final int LOG_LEVEL_VERBOSE = 4;
@DoNotStrip
void log(int level, String message);
}

View File

@@ -33,6 +33,12 @@ public class CSSNode implements CSSNodeAPI<CSSNode> {
* Get native instance count. Useful for testing only. * Get native instance count. Useful for testing only.
*/ */
static native int jni_CSSNodeGetInstanceCount(); static native int jni_CSSNodeGetInstanceCount();
static native void jni_CSSLog(int level, String message);
private static native void jni_CSSLayoutSetLogger(Object logger);
public static void setLogger(CSSLogger logger) {
jni_CSSLayoutSetLogger(logger);
}
private CSSNode mParent; private CSSNode mParent;
private List<CSSNode> mChildren; private List<CSSNode> mChildren;

View File

@@ -68,10 +68,43 @@ static CSSSize _jniMeasureFunc(CSSNodeRef node,
return CSSSize{measuredWidth, measuredHeight}; return CSSSize{measuredWidth, measuredHeight};
} }
static global_ref<jobject> *jLogger;
static int _jniLog(CSSLogLevel level, const char *format, va_list args) {
char buffer[256];
int result = vsnprintf(buffer, sizeof(buffer), format, args);
static auto logFunc =
findClassLocal("com/facebook/csslayout/CSSLogger")->getMethod<void(jint, jstring)>("log");
logFunc(jLogger->get(), static_cast<jint>(level), Environment::current()->NewStringUTF(buffer));
return result;
}
static inline CSSNodeRef _jlong2CSSNodeRef(jlong addr) { static inline CSSNodeRef _jlong2CSSNodeRef(jlong addr) {
return reinterpret_cast<CSSNodeRef>(static_cast<intptr_t>(addr)); return reinterpret_cast<CSSNodeRef>(static_cast<intptr_t>(addr));
} }
void jni_CSSLayoutSetLogger(alias_ref<jclass> clazz, alias_ref<jobject> logger) {
if (jLogger) {
jLogger->releaseAlias();
delete jLogger;
}
if (logger) {
jLogger = new global_ref<jobject>(make_global(logger));
CSSLayoutSetLogger(_jniLog);
} else {
jLogger = NULL;
CSSLayoutSetLogger(NULL);
}
}
void jni_CSSLog(alias_ref<jclass> clazz, jint level, jstring message) {
const char *nMessage = Environment::current()->GetStringUTFChars(message, 0);
CSSLog(static_cast<CSSLogLevel>(level), "%s", nMessage);
Environment::current()->ReleaseStringUTFChars(message, nMessage);
}
jint jni_CSSNodeGetInstanceCount(alias_ref<jclass> clazz) { jint jni_CSSNodeGetInstanceCount(alias_ref<jclass> clazz) {
return CSSNodeGetInstanceCount(); return CSSNodeGetInstanceCount();
} }
@@ -256,6 +289,8 @@ jint JNI_OnLoad(JavaVM *vm, void *) {
CSSMakeNativeMethod(jni_CSSNodeStyleSetMaxHeight), CSSMakeNativeMethod(jni_CSSNodeStyleSetMaxHeight),
CSSMakeNativeMethod(jni_CSSNodeGetInstanceCount), CSSMakeNativeMethod(jni_CSSNodeGetInstanceCount),
CSSMakeNativeMethod(jni_CSSLayoutSetLogger),
CSSMakeNativeMethod(jni_CSSLog),
}); });
}); });
} }

View File

@@ -39,4 +39,36 @@ public class CSSNodeTest {
assertEquals(100, (int) node.getLayoutWidth()); assertEquals(100, (int) node.getLayoutWidth());
assertEquals(100, (int) node.getLayoutHeight()); assertEquals(100, (int) node.getLayoutHeight());
} }
private int mLogLevel;
private String mLogMessage;
@Test
public void testLogger() {
CSSNode.setLogger(new CSSLogger() {
public void log(int level, String message) {
mLogLevel = level;
mLogMessage = message;
}
});
CSSNode.jni_CSSLog(CSSLogger.LOG_LEVEL_DEBUG, "Hello");
assertEquals(CSSLogger.LOG_LEVEL_DEBUG, mLogLevel);
assertEquals("Hello", mLogMessage);
}
@Test
public void testUpdateLogger() {
CSSNode.setLogger(new CSSLogger() {
public void log(int level, String message) {}
});
CSSNode.setLogger(new CSSLogger() {
public void log(int level, String message) {
mLogLevel = level;
mLogMessage = message;
}
});
CSSNode.jni_CSSLog(CSSLogger.LOG_LEVEL_VERBOSE, "Flexbox");
assertEquals(CSSLogger.LOG_LEVEL_VERBOSE, mLogLevel);
assertEquals("Flexbox", mLogMessage);
}
} }