Merge "EventLogHelper: Clean up code"

This commit is contained in:
Treehugger Robot
2017-10-11 15:48:46 +00:00
committed by Gerrit Code Review

View File

@@ -17,6 +17,8 @@
#ifndef FRAMEWORKS_BASE_CORE_JNI_EVENTLOG_HELPER_H_ #ifndef FRAMEWORKS_BASE_CORE_JNI_EVENTLOG_HELPER_H_
#define FRAMEWORKS_BASE_CORE_JNI_EVENTLOG_HELPER_H_ #define FRAMEWORKS_BASE_CORE_JNI_EVENTLOG_HELPER_H_
#include <memory>
#include <fcntl.h> #include <fcntl.h>
#include <android-base/macros.h> #include <android-base/macros.h>
@@ -26,6 +28,8 @@
#include <nativehelper/JNIHelp.h> #include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedLocalRef.h> #include <nativehelper/ScopedLocalRef.h>
#include <nativehelper/ScopedPrimitiveArray.h>
#include <nativehelper/ScopedUtfChars.h>
#include "core_jni_helpers.h" #include "core_jni_helpers.h"
#include "jni.h" #include "jni.h"
@@ -91,20 +95,14 @@ public:
android_log_event_list ctx(tag); android_log_event_list ctx(tag);
// Don't throw NPE -- I feel like it's sort of mean for a logging function // Don't throw NPE -- I feel like it's sort of mean for a logging function
// to be all crashy if you pass in NULL -- but make the NULL value explicit. // to be all crashy if you pass in NULL -- but make the NULL value explicit.
if (value != NULL) { ctx << (value != nullptr ? ScopedUtfChars(env, value).c_str() : "NULL");
const char *str = env->GetStringUTFChars(value, NULL);
ctx << str;
env->ReleaseStringUTFChars(value, str);
} else {
ctx << "NULL";
}
return ctx.write(LogID); return ctx.write(LogID);
} }
static jint writeEventArray(JNIEnv* env, jobject clazz ATTRIBUTE_UNUSED, jint tag, static jint writeEventArray(JNIEnv* env, jobject clazz ATTRIBUTE_UNUSED, jint tag,
jobjectArray value) { jobjectArray value) {
android_log_event_list ctx(tag); android_log_event_list ctx(tag);
if (value == NULL) { if (value == nullptr) {
ctx << "[NULL]"; ctx << "[NULL]";
return ctx.write(LogID); return ctx.write(LogID);
} }
@@ -112,26 +110,23 @@ public:
jsize copied = 0, num = env->GetArrayLength(value); jsize copied = 0, num = env->GetArrayLength(value);
for (; copied < num && copied < 255; ++copied) { for (; copied < num && copied < 255; ++copied) {
if (ctx.status()) break; if (ctx.status()) break;
jobject item = env->GetObjectArrayElement(value, copied); ScopedLocalRef<jobject> item(env, env->GetObjectArrayElement(value, copied));
if (item == NULL) { if (item == nullptr) {
ctx << "NULL"; ctx << "NULL";
} else if (env->IsInstanceOf(item, gStringClass)) { } else if (env->IsInstanceOf(item.get(), gStringClass)) {
const char *str = env->GetStringUTFChars((jstring) item, NULL); ctx << ScopedUtfChars(env, (jstring) item.get()).c_str();
ctx << str; } else if (env->IsInstanceOf(item.get(), gIntegerClass)) {
env->ReleaseStringUTFChars((jstring) item, str); ctx << (int32_t)env->GetIntField(item.get(), gIntegerValueID);
} else if (env->IsInstanceOf(item, gIntegerClass)) { } else if (env->IsInstanceOf(item.get(), gLongClass)) {
ctx << (int32_t)env->GetIntField(item, gIntegerValueID); ctx << (int64_t)env->GetLongField(item.get(), gLongValueID);
} else if (env->IsInstanceOf(item, gLongClass)) { } else if (env->IsInstanceOf(item.get(), gFloatClass)) {
ctx << (int64_t)env->GetLongField(item, gLongValueID); ctx << (float)env->GetFloatField(item.get(), gFloatValueID);
} else if (env->IsInstanceOf(item, gFloatClass)) {
ctx << (float)env->GetFloatField(item, gFloatValueID);
} else { } else {
jniThrowException(env, jniThrowException(env,
"java/lang/IllegalArgumentException", "java/lang/IllegalArgumentException",
"Invalid payload item type"); "Invalid payload item type");
return -1; return -1;
} }
env->DeleteLocalRef(item);
} }
return ctx.write(LogID); return ctx.write(LogID);
} }
@@ -140,39 +135,37 @@ public:
readEvents(env, loggerMode, nullptr, startTime, out); readEvents(env, loggerMode, nullptr, startTime, out);
} }
static void readEvents(JNIEnv* env, int loggerMode, jintArray tags, jlong startTime, static void readEvents(JNIEnv* env, int loggerMode, jintArray jTags, jlong startTime,
jobject out) { jobject out) {
struct logger_list *logger_list; std::unique_ptr<struct logger_list, decltype(&android_logger_list_close)> logger_list(
nullptr, android_logger_list_close);
if (startTime) { if (startTime) {
logger_list = android_logger_list_alloc_time(loggerMode, logger_list.reset(android_logger_list_alloc_time(loggerMode,
log_time(startTime / NS_PER_SEC, startTime % NS_PER_SEC), 0); log_time(startTime / NS_PER_SEC, startTime % NS_PER_SEC), 0));
} else { } else {
logger_list = android_logger_list_alloc(loggerMode, 0, 0); logger_list.reset(android_logger_list_alloc(loggerMode, 0, 0));
} }
if (!logger_list) { if (!logger_list) {
jniThrowIOException(env, errno); jniThrowIOException(env, errno);
return; return;
} }
if (!android_logger_open(logger_list, LogID)) { if (!android_logger_open(logger_list.get(), LogID)) {
jniThrowIOException(env, errno); jniThrowIOException(env, errno);
android_logger_list_free(logger_list);
return; return;
} }
jsize tagLength = 0; ScopedIntArrayRO tags(env);
jint *tagValues = nullptr; if (jTags != nullptr) {
if (tags != nullptr) { tags.reset(jTags);
tagLength = env->GetArrayLength(tags);
tagValues = env->GetIntArrayElements(tags, NULL);
} }
while (1) { while (1) {
log_msg log_msg; log_msg log_msg;
int ret = android_logger_list_read(logger_list, &log_msg); int ret = android_logger_list_read(logger_list.get(), &log_msg);
if (ret == 0) { if (ret == 0) {
break; return;
} }
if (ret < 0) { if (ret < 0) {
if (ret == -EINTR) { if (ret == -EINTR) {
@@ -183,7 +176,7 @@ public:
} else if (ret != -EAGAIN) { } else if (ret != -EAGAIN) {
jniThrowIOException(env, -ret); // Will throw on return jniThrowIOException(env, -ret); // Will throw on return
} }
break; return;
} }
if (log_msg.id() != LogID) { if (log_msg.id() != LogID) {
@@ -192,10 +185,10 @@ public:
int32_t tag = * (int32_t *) log_msg.msg(); int32_t tag = * (int32_t *) log_msg.msg();
if (tags != nullptr) { if (jTags != nullptr) {
bool found = false; bool found = false;
for (int i = 0; !found && i < tagLength; ++i) { for (size_t i = 0; !found && i < tags.size(); ++i) {
found = (tag == tagValues[i]); found = (tag == tags[i]);
} }
if (!found) { if (!found) {
continue; continue;
@@ -203,33 +196,27 @@ public:
} }
jsize len = ret; jsize len = ret;
jbyteArray array = env->NewByteArray(len); ScopedLocalRef<jbyteArray> array(env, env->NewByteArray(len));
if (array == NULL) { if (array == nullptr) {
break; return;
} }
jbyte *bytes = env->GetByteArrayElements(array, NULL); {
memcpy(bytes, log_msg.buf, len); ScopedByteArrayRW bytes(env, array.get());
env->ReleaseByteArrayElements(array, bytes, 0); memcpy(bytes.get(), log_msg.buf, len);
jobject event = env->NewObject(gEventClass, gEventInitID, array);
if (event == NULL) {
break;
} }
env->CallBooleanMethod(out, gCollectionAddID, event); ScopedLocalRef<jobject> event(env,
env->DeleteLocalRef(event); env->NewObject(gEventClass, gEventInitID, array.get()));
env->DeleteLocalRef(array); if (event == nullptr) {
return;
}
env->CallBooleanMethod(out, gCollectionAddID, event.get());
if (env->ExceptionCheck() == JNI_TRUE) { if (env->ExceptionCheck() == JNI_TRUE) {
break; return;
} }
} }
android_logger_list_close(logger_list);
if (tags != nullptr) {
env->ReleaseIntArrayElements(tags, tagValues, 0);
}
} }
private: private: