print status-go logs in adb logcat

This commit is contained in:
Roman Volosovskyi 2017-10-12 21:49:48 +02:00
parent c2ef0e4547
commit 8137c802fe
6 changed files with 83 additions and 0 deletions

1
.gitignore vendored
View File

@ -41,6 +41,7 @@ npm-debug.log
buck-out/ buck-out/
\.buckd/ \.buckd/
android/app/libs android/app/libs
android/app/obj
android/keystores/debug.keystore android/keystores/debug.keystore
# Generated by re-natal # Generated by re-natal

View File

@ -159,6 +159,8 @@ android {
} }
} }
} }
sourceSets { main { jniLibs.srcDirs 'libs' } }
} }
dependencies { dependencies {
@ -204,3 +206,20 @@ task copyDownloadableDepsToLibs(type: Copy) {
// Must be at bottom to avoid dependency collision // Must be at bottom to avoid dependency collision
apply plugin: "com.google.gms.google-services" 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

View File

@ -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)

View File

@ -0,0 +1,4 @@
APP_ABI := all
APP_PLATFORM := android-15
APP_STL := gnustl_static
NDK_TOOLCHAIN_VERSION=4.9

39
android/app/jni/Log.c Normal file
View File

@ -0,0 +1,39 @@
// https://codelab.wordpress.com/2014/11/03/how-to-use-standard-output-streams-for-logging-in-android-apps/
#include <jni.h>
#include <pthread.h>
#include <stdio.h>
#include <android/log.h>
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;
}

View File

@ -115,6 +115,15 @@ public class MainActivity extends ReactActivity {
dialog.show(); dialog.show();
} }
Thread thread = new Thread() {
@Override
public void run() {
System.loadLibrary("status-logs");
}
};
thread.start();
} }
@Override @Override