jni: EventLog replace open coding with android_log_event_list

Test: manual confirm event log messages from services continue
Bug: 31992412
Bug: 31456426
Change-Id: I20a4228120c454d3e736dae8a927f1e0f9a62e37
This commit is contained in:
Mark Salyzyn
2016-11-08 09:28:51 -08:00
parent 45c0c6939f
commit 7b25bb8ec7

View File

@@ -16,16 +16,14 @@
#include <fcntl.h> #include <fcntl.h>
#include <log/log_event_list.h>
#include "JNIHelp.h" #include "JNIHelp.h"
#include "core_jni_helpers.h" #include "core_jni_helpers.h"
#include "jni.h" #include "jni.h"
#include <log/logger.h>
#define UNUSED __attribute__((__unused__)) #define UNUSED __attribute__((__unused__))
// The size of the tag number comes out of the payload size.
#define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(int32_t))
namespace android { namespace android {
static jclass gCollectionClass; static jclass gCollectionClass;
@@ -53,7 +51,9 @@ static jint android_util_EventLog_writeEvent_Integer(JNIEnv* env UNUSED,
jobject clazz UNUSED, jobject clazz UNUSED,
jint tag, jint value) jint tag, jint value)
{ {
return android_btWriteLog(tag, EVENT_TYPE_INT, &value, sizeof(value)); android_log_event_list ctx(tag);
ctx << (int32_t)value;
return ctx.write();
} }
/* /*
@@ -64,7 +64,9 @@ static jint android_util_EventLog_writeEvent_Long(JNIEnv* env UNUSED,
jobject clazz UNUSED, jobject clazz UNUSED,
jint tag, jlong value) jint tag, jlong value)
{ {
return android_btWriteLog(tag, EVENT_TYPE_LONG, &value, sizeof(value)); android_log_event_list ctx(tag);
ctx << (int64_t)value;
return ctx.write();
} }
/* /*
@@ -75,7 +77,9 @@ static jint android_util_EventLog_writeEvent_Float(JNIEnv* env UNUSED,
jobject clazz UNUSED, jobject clazz UNUSED,
jint tag, jfloat value) jint tag, jfloat value)
{ {
return android_btWriteLog(tag, EVENT_TYPE_FLOAT, &value, sizeof(value)); android_log_event_list ctx(tag);
ctx << (float)value;
return ctx.write();
} }
/* /*
@@ -85,22 +89,17 @@ static jint android_util_EventLog_writeEvent_Float(JNIEnv* env UNUSED,
static jint android_util_EventLog_writeEvent_String(JNIEnv* env, static jint android_util_EventLog_writeEvent_String(JNIEnv* env,
jobject clazz UNUSED, jobject clazz UNUSED,
jint tag, jstring value) { jint tag, jstring value) {
uint8_t buf[MAX_EVENT_PAYLOAD]; 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.
const char *str = value != NULL ? env->GetStringUTFChars(value, NULL) : "NULL"; if (value != NULL) {
uint32_t len = strlen(str); const char *str = env->GetStringUTFChars(value, NULL);
size_t max = sizeof(buf) - sizeof(len) - 2; // Type byte, final newline ctx << str;
if (len > max) len = max; env->ReleaseStringUTFChars(value, str);
} else {
buf[0] = EVENT_TYPE_STRING; ctx << "NULL";
memcpy(&buf[1], &len, sizeof(len)); }
memcpy(&buf[1 + sizeof(len)], str, len); return ctx.write();
buf[1 + sizeof(len) + len] = '\n';
if (value != NULL) env->ReleaseStringUTFChars(value, str);
return android_bWriteLog(tag, buf, 2 + sizeof(len) + len);
} }
/* /*
@@ -109,45 +108,29 @@ static jint android_util_EventLog_writeEvent_String(JNIEnv* env,
*/ */
static jint android_util_EventLog_writeEvent_Array(JNIEnv* env, jobject clazz, static jint android_util_EventLog_writeEvent_Array(JNIEnv* env, jobject clazz,
jint tag, jobjectArray value) { jint tag, jobjectArray value) {
if (value == NULL) { android_log_event_list ctx(tag);
return android_util_EventLog_writeEvent_String(env, clazz, tag, NULL);
}
uint8_t buf[MAX_EVENT_PAYLOAD]; if (value == NULL) {
const size_t max = sizeof(buf) - 1; // leave room for final newline ctx << "[NULL]";
size_t pos = 2; // Save room for type tag & array count return ctx.write();
}
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;
jobject item = env->GetObjectArrayElement(value, copied); jobject item = env->GetObjectArrayElement(value, copied);
if (item == NULL || env->IsInstanceOf(item, gStringClass)) { if (item == NULL) {
if (pos + 1 + sizeof(jint) > max) break; ctx << "NULL";
const char *str = item != NULL ? env->GetStringUTFChars((jstring) item, NULL) : "NULL"; } else if (env->IsInstanceOf(item, gStringClass)) {
jint len = strlen(str); const char *str = env->GetStringUTFChars((jstring) item, NULL);
if (pos + 1 + sizeof(len) + len > max) len = max - pos - 1 - sizeof(len); ctx << str;
buf[pos++] = EVENT_TYPE_STRING; env->ReleaseStringUTFChars((jstring) item, str);
memcpy(&buf[pos], &len, sizeof(len));
memcpy(&buf[pos + sizeof(len)], str, len);
pos += sizeof(len) + len;
if (item != NULL) env->ReleaseStringUTFChars((jstring) item, str);
} else if (env->IsInstanceOf(item, gIntegerClass)) { } else if (env->IsInstanceOf(item, gIntegerClass)) {
jint intVal = env->GetIntField(item, gIntegerValueID); ctx << (int32_t)env->GetIntField(item, gIntegerValueID);
if (pos + 1 + sizeof(intVal) > max) break;
buf[pos++] = EVENT_TYPE_INT;
memcpy(&buf[pos], &intVal, sizeof(intVal));
pos += sizeof(intVal);
} else if (env->IsInstanceOf(item, gLongClass)) { } else if (env->IsInstanceOf(item, gLongClass)) {
jlong longVal = env->GetLongField(item, gLongValueID); ctx << (int64_t)env->GetLongField(item, gLongValueID);
if (pos + 1 + sizeof(longVal) > max) break;
buf[pos++] = EVENT_TYPE_LONG;
memcpy(&buf[pos], &longVal, sizeof(longVal));
pos += sizeof(longVal);
} else if (env->IsInstanceOf(item, gFloatClass)) { } else if (env->IsInstanceOf(item, gFloatClass)) {
jfloat floatVal = env->GetFloatField(item, gFloatValueID); ctx << (float)env->GetFloatField(item, gFloatValueID);
if (pos + 1 + sizeof(floatVal) > max) break;
buf[pos++] = EVENT_TYPE_FLOAT;
memcpy(&buf[pos], &floatVal, sizeof(floatVal));
pos += sizeof(floatVal);
} else { } else {
jniThrowException(env, jniThrowException(env,
"java/lang/IllegalArgumentException", "java/lang/IllegalArgumentException",
@@ -156,11 +139,7 @@ static jint android_util_EventLog_writeEvent_Array(JNIEnv* env, jobject clazz,
} }
env->DeleteLocalRef(item); env->DeleteLocalRef(item);
} }
return ctx.write();
buf[0] = EVENT_TYPE_LIST;
buf[1] = copied;
buf[pos++] = '\n';
return android_bWriteLog(tag, buf, pos);
} }
/* /*