From cd054ecf265f2e9ec532eebd2a97d40244e90de1 Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Fri, 11 Nov 2016 08:08:28 -0800 Subject: [PATCH] Expose CSSLayoutSetLogger to java Summary: Expose CSSLayoutSetLogger to java Reviewed By: astreet Differential Revision: D4153502 fbshipit-source-id: 630909d9d0d36d94d7cd3027476ddb52668b8cc0 --- java/com/facebook/csslayout/CSSLogger.java | 27 ++++++++++++++ java/com/facebook/csslayout/CSSNode.java | 6 ++++ java/jni/CSSJNI.cpp | 35 +++++++++++++++++++ .../com/facebook/csslayout/CSSNodeTest.java | 32 +++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 java/com/facebook/csslayout/CSSLogger.java diff --git a/java/com/facebook/csslayout/CSSLogger.java b/java/com/facebook/csslayout/CSSLogger.java new file mode 100644 index 00000000..44536c46 --- /dev/null +++ b/java/com/facebook/csslayout/CSSLogger.java @@ -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); +} diff --git a/java/com/facebook/csslayout/CSSNode.java b/java/com/facebook/csslayout/CSSNode.java index 612be246..9cc47686 100644 --- a/java/com/facebook/csslayout/CSSNode.java +++ b/java/com/facebook/csslayout/CSSNode.java @@ -33,6 +33,12 @@ public class CSSNode implements CSSNodeAPI { * Get native instance count. Useful for testing only. */ 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 List mChildren; diff --git a/java/jni/CSSJNI.cpp b/java/jni/CSSJNI.cpp index 16869f1a..1b5d1de9 100644 --- a/java/jni/CSSJNI.cpp +++ b/java/jni/CSSJNI.cpp @@ -68,10 +68,43 @@ static CSSSize _jniMeasureFunc(CSSNodeRef node, return CSSSize{measuredWidth, measuredHeight}; } +static global_ref *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("log"); + logFunc(jLogger->get(), static_cast(level), Environment::current()->NewStringUTF(buffer)); + + return result; +} + static inline CSSNodeRef _jlong2CSSNodeRef(jlong addr) { return reinterpret_cast(static_cast(addr)); } +void jni_CSSLayoutSetLogger(alias_ref clazz, alias_ref logger) { + if (jLogger) { + jLogger->releaseAlias(); + delete jLogger; + } + + if (logger) { + jLogger = new global_ref(make_global(logger)); + CSSLayoutSetLogger(_jniLog); + } else { + jLogger = NULL; + CSSLayoutSetLogger(NULL); + } +} + +void jni_CSSLog(alias_ref clazz, jint level, jstring message) { + const char *nMessage = Environment::current()->GetStringUTFChars(message, 0); + CSSLog(static_cast(level), "%s", nMessage); + Environment::current()->ReleaseStringUTFChars(message, nMessage); +} + jint jni_CSSNodeGetInstanceCount(alias_ref clazz) { return CSSNodeGetInstanceCount(); } @@ -256,6 +289,8 @@ jint JNI_OnLoad(JavaVM *vm, void *) { CSSMakeNativeMethod(jni_CSSNodeStyleSetMaxHeight), CSSMakeNativeMethod(jni_CSSNodeGetInstanceCount), + CSSMakeNativeMethod(jni_CSSLayoutSetLogger), + CSSMakeNativeMethod(jni_CSSLog), }); }); } diff --git a/java/tests/com/facebook/csslayout/CSSNodeTest.java b/java/tests/com/facebook/csslayout/CSSNodeTest.java index c61b8988..5f2d3ae2 100644 --- a/java/tests/com/facebook/csslayout/CSSNodeTest.java +++ b/java/tests/com/facebook/csslayout/CSSNodeTest.java @@ -39,4 +39,36 @@ public class CSSNodeTest { assertEquals(100, (int) node.getLayoutWidth()); 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); + } }