From 8137c802fe7d39f7e4b82a8c34ec84e46198e7fb Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 12 Oct 2017 21:49:48 +0200 Subject: [PATCH] print status-go logs in adb logcat --- .gitignore | 1 + android/app/build.gradle | 19 +++++++++ android/app/jni/Android.mk | 11 ++++++ android/app/jni/Application.mk | 4 ++ android/app/jni/Log.c | 39 +++++++++++++++++++ .../java/im/status/ethereum/MainActivity.java | 9 +++++ 6 files changed, 83 insertions(+) create mode 100644 android/app/jni/Android.mk create mode 100644 android/app/jni/Application.mk create mode 100644 android/app/jni/Log.c diff --git a/.gitignore b/.gitignore index a11c1d4702..c913638b75 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,7 @@ npm-debug.log buck-out/ \.buckd/ android/app/libs +android/app/obj android/keystores/debug.keystore # Generated by re-natal diff --git a/android/app/build.gradle b/android/app/build.gradle index 9c4f603383..1ae11b290b 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -159,6 +159,8 @@ android { } } } + + sourceSets { main { jniLibs.srcDirs 'libs' } } } dependencies { @@ -204,3 +206,20 @@ task copyDownloadableDepsToLibs(type: Copy) { // Must be at bottom to avoid dependency collision apply plugin: "com.google.gms.google-services" + +task hemroidBuild(type: Exec) { + def rootDir = project.rootDir + def localProperties = new File(rootDir, "local.properties") + + def ndkDir = "$System.env.ANDROID_NDK_HOME" + if (localProperties.exists()) { + Properties properties = new Properties() + localProperties.withInputStream { instr -> + properties.load(instr) + } + ndkDir = properties.getProperty('ndk.dir') + } + executable "$ndkDir/ndk-build" +} + +preBuild.dependsOn hemroidBuild diff --git a/android/app/jni/Android.mk b/android/app/jni/Android.mk new file mode 100644 index 0000000000..1d61e349ff --- /dev/null +++ b/android/app/jni/Android.mk @@ -0,0 +1,11 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := status-logs +LOCAL_SRC_FILES := Log.c + +LOCAL_CPPFLAGS := -std=c++11 -fexceptions -I$(LOCAL_PATH)/include +LOCAL_LDFLAGS := -llog + +include $(BUILD_SHARED_LIBRARY) diff --git a/android/app/jni/Application.mk b/android/app/jni/Application.mk new file mode 100644 index 0000000000..e75a613e5e --- /dev/null +++ b/android/app/jni/Application.mk @@ -0,0 +1,4 @@ +APP_ABI := all +APP_PLATFORM := android-15 +APP_STL := gnustl_static +NDK_TOOLCHAIN_VERSION=4.9 diff --git a/android/app/jni/Log.c b/android/app/jni/Log.c new file mode 100644 index 0000000000..19968f37a3 --- /dev/null +++ b/android/app/jni/Log.c @@ -0,0 +1,39 @@ +// https://codelab.wordpress.com/2014/11/03/how-to-use-standard-output-streams-for-logging-in-android-apps/ +#include +#include +#include +#include + +static int pfd[2]; +static pthread_t thr; + +static void *thread_func(void* param) +{ + ssize_t rdsz; + char buf[128]; + while((rdsz = read(pfd[0], buf, sizeof buf - 1)) > 0) { + if(buf[rdsz - 1] == '\n') --rdsz; + buf[rdsz] = 0; /* add null-terminator */ + __android_log_write(ANDROID_LOG_DEBUG, "StatusNativeLogs", buf); + } + return 0; +} + +JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) +{ + /* make stdout line-buffered and stderr unbuffered */ + setvbuf(stdout, 0, _IOLBF, 0); + setvbuf(stderr, 0, _IONBF, 0); + + /* create the pipe and redirect stdout and stderr */ + pipe(pfd); + dup2(pfd[1], 1); + dup2(pfd[1], 2); + + /* spawn the logging thread */ + if(pthread_create(&thr, 0, thread_func, 0) == -1) + return JNI_VERSION_1_6; // fail silently + pthread_detach(thr); + + return JNI_VERSION_1_6; +} diff --git a/android/app/src/main/java/im/status/ethereum/MainActivity.java b/android/app/src/main/java/im/status/ethereum/MainActivity.java index 613bf34834..e445ccc6f5 100644 --- a/android/app/src/main/java/im/status/ethereum/MainActivity.java +++ b/android/app/src/main/java/im/status/ethereum/MainActivity.java @@ -115,6 +115,15 @@ public class MainActivity extends ReactActivity { dialog.show(); } + + Thread thread = new Thread() { + @Override + public void run() { + System.loadLibrary("status-logs"); + } + }; + + thread.start(); } @Override