summaryrefslogtreecommitdiffstats
path: root/core/jni/android_util_EventLog.cpp
diff options
context:
space:
mode:
authorDan Egnor <egnor@google.com>2010-01-05 19:09:08 -0800
committerDan Egnor <egnor@google.com>2010-01-06 12:30:12 -0800
commit62136d3e1c1262fc31a1f77f6d6acbd75e5ea81d (patch)
tree3addaf756197802741a15e36046ddadfe894554c /core/jni/android_util_EventLog.cpp
parent432bff01ec67533dbbb6ed27cb190c99e83ea226 (diff)
downloadframeworks_base-62136d3e1c1262fc31a1f77f6d6acbd75e5ea81d.zip
frameworks_base-62136d3e1c1262fc31a1f77f6d6acbd75e5ea81d.tar.gz
frameworks_base-62136d3e1c1262fc31a1f77f6d6acbd75e5ea81d.tar.bz2
Simplify EventLog interface -- remove supported for nested
sequences (which nobody used) and streamline the API, adding documentation in preparation for inclusion in the SDK. Gut and deprecate EventLogTags, which unfortunately was put into the public SDK (an oversight). Include the functionality in EventLog proper, in a simpler and easier to use manner. This change doesn't actually un-@hide anything, but it does change it to @pending.
Diffstat (limited to 'core/jni/android_util_EventLog.cpp')
-rw-r--r--core/jni/android_util_EventLog.cpp285
1 files changed, 67 insertions, 218 deletions
diff --git a/core/jni/android_util_EventLog.cpp b/core/jni/android_util_EventLog.cpp
index 34b7c89..78356cf 100644
--- a/core/jni/android_util_EventLog.cpp
+++ b/core/jni/android_util_EventLog.cpp
@@ -21,13 +21,6 @@
#include "jni.h"
#include "cutils/logger.h"
-#define END_DELIMITER '\n'
-#define INT_BUFFER_SIZE (sizeof(jbyte)+sizeof(jint)+sizeof(END_DELIMITER))
-#define LONG_BUFFER_SIZE (sizeof(jbyte)+sizeof(jlong)+sizeof(END_DELIMITER))
-#define INITAL_BUFFER_CAPACITY 256
-
-#define MAX(a,b) ((a>b)?a:b)
-
namespace android {
static jclass gCollectionClass;
@@ -47,107 +40,6 @@ static jfieldID gLongValueID;
static jclass gStringClass;
-struct ByteBuf {
- size_t len;
- size_t capacity;
- uint8_t* buf;
-
- ByteBuf(size_t initSize) {
- buf = (uint8_t*)malloc(initSize);
- len = 0;
- capacity = initSize;
- }
-
- ~ByteBuf() {
- free(buf);
- }
-
- bool ensureExtraCapacity(size_t extra) {
- size_t spaceNeeded = len + extra;
- if (spaceNeeded > capacity) {
- size_t newCapacity = MAX(spaceNeeded, 2 * capacity);
- void* newBuf = realloc(buf, newCapacity);
- if (newBuf == NULL) {
- return false;
- }
- capacity = newCapacity;
- buf = (uint8_t*)newBuf;
- return true;
- } else {
- return true;
- }
- }
-
- void putIntEvent(jint value) {
- bool succeeded = ensureExtraCapacity(INT_BUFFER_SIZE);
- buf[len++] = EVENT_TYPE_INT;
- memcpy(buf+len, &value, sizeof(jint));
- len += sizeof(jint);
- }
-
- void putByte(uint8_t value) {
- bool succeeded = ensureExtraCapacity(sizeof(uint8_t));
- buf[len++] = value;
- }
-
- void putLongEvent(jlong value) {
- bool succeeded = ensureExtraCapacity(LONG_BUFFER_SIZE);
- buf[len++] = EVENT_TYPE_LONG;
- memcpy(buf+len, &value, sizeof(jlong));
- len += sizeof(jlong);
- }
-
-
- void putStringEvent(JNIEnv* env, jstring value) {
- const char* strValue = env->GetStringUTFChars(value, NULL);
- uint32_t strLen = strlen(strValue); //env->GetStringUTFLength(value);
- bool succeeded = ensureExtraCapacity(1 + sizeof(uint32_t) + strLen);
- buf[len++] = EVENT_TYPE_STRING;
- memcpy(buf+len, &strLen, sizeof(uint32_t));
- len += sizeof(uint32_t);
- memcpy(buf+len, strValue, strLen);
- env->ReleaseStringUTFChars(value, strValue);
- len += strLen;
- }
-
- void putList(JNIEnv* env, jobject list) {
- jobjectArray items = (jobjectArray) env->GetObjectField(list, gListItemsID);
- if (items == NULL) {
- jniThrowException(env, "java/lang/NullPointerException", NULL);
- return;
- }
-
- jsize numItems = env->GetArrayLength(items);
- putByte(EVENT_TYPE_LIST);
- putByte(numItems);
- // We'd like to call GetPrimitveArrayCritical() but that might
- // not be safe since we're going to be doing some I/O
- for (int i = 0; i < numItems; i++) {
- jobject item = env->GetObjectArrayElement(items, i);
- if (env->IsInstanceOf(item, gIntegerClass)) {
- jint intVal = env->GetIntField(item, gIntegerValueID);
- putIntEvent(intVal);
- } else if (env->IsInstanceOf(item, gLongClass)) {
- jlong longVal = env->GetLongField(item, gLongValueID);
- putLongEvent(longVal);
- } else if (env->IsInstanceOf(item, gStringClass)) {
- putStringEvent(env, (jstring)item);
- } else if (env->IsInstanceOf(item, gListClass)) {
- putList(env, item);
- } else {
- jniThrowException(
- env,
- "java/lang/IllegalArgumentException",
- "Attempt to log an illegal item type.");
- return;
- }
- env->DeleteLocalRef(item);
- }
-
- env->DeleteLocalRef(items);
- }
-};
-
/*
* In class android.util.EventLog:
* static native int writeEvent(int tag, int value)
@@ -170,41 +62,80 @@ static jint android_util_EventLog_writeEvent_Long(JNIEnv* env, jobject clazz,
/*
* In class android.util.EventLog:
- * static native int writeEvent(long tag, List value)
+ * static native int writeEvent(int tag, String value)
*/
-static jint android_util_EventLog_writeEvent_List(JNIEnv* env, jobject clazz,
- jint tag, jobject value) {
- if (value == NULL) {
- jclass clazz = env->FindClass("java/lang/IllegalArgumentException");
- env->ThrowNew(clazz, "writeEvent needs a value.");
- return -1;
- }
- ByteBuf byteBuf(INITAL_BUFFER_CAPACITY);
- byteBuf.putList(env, value);
- byteBuf.putByte((uint8_t)END_DELIMITER);
- int numBytesPut = byteBuf.len;
- int bytesWritten = android_bWriteLog(tag, byteBuf.buf, numBytesPut);
- return bytesWritten;
+static jint android_util_EventLog_writeEvent_String(JNIEnv* env, jobject clazz,
+ jint tag, jstring value) {
+ uint8_t buf[LOGGER_ENTRY_MAX_PAYLOAD];
+
+ // 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.
+ const char *str = value != NULL ? env->GetStringUTFChars(value, NULL) : "NULL";
+ jint len = strlen(str);
+ const int max = sizeof(buf) - sizeof(len) - 2; // Type byte, final newline
+ if (len > max) len = max;
+
+ buf[0] = EVENT_TYPE_STRING;
+ memcpy(&buf[1], &len, sizeof(len));
+ memcpy(&buf[1 + sizeof(len)], str, len);
+ buf[1 + sizeof(len) + len] = '\n';
+
+ if (value != NULL) env->ReleaseStringUTFChars(value, str);
+ return android_bWriteLog(tag, buf, 2 + sizeof(len) + len);
}
/*
* In class android.util.EventLog:
- * static native int writeEvent(int tag, String value)
+ * static native int writeEvent(long tag, Object... value)
*/
-static jint android_util_EventLog_writeEvent_String(JNIEnv* env, jobject clazz,
- jint tag, jstring value) {
+static jint android_util_EventLog_writeEvent_Array(JNIEnv* env, jobject clazz,
+ jint tag, jobjectArray value) {
if (value == NULL) {
- jclass clazz = env->FindClass("java/lang/IllegalArgumentException");
- env->ThrowNew(clazz, "logEvent needs a value.");
- return -1;
+ return android_util_EventLog_writeEvent_String(env, clazz, tag, NULL);
+ }
+
+ uint8_t buf[LOGGER_ENTRY_MAX_PAYLOAD];
+ const size_t max = sizeof(buf) - 1; // leave room for final newline
+ size_t pos = 2; // Save room for type tag & array count
+
+ jsize copied = 0, num = env->GetArrayLength(value);
+ for (; copied < num && copied < 256; ++copied) {
+ jobject item = env->GetObjectArrayElement(value, copied);
+ if (item == NULL || env->IsInstanceOf(item, gStringClass)) {
+ if (pos + 1 + sizeof(jint) > max) break;
+ const char *str = item != NULL ? env->GetStringUTFChars((jstring) item, NULL) : "NULL";
+ jint len = strlen(str);
+ if (pos + 1 + sizeof(len) + len > max) len = max - pos - 1 - sizeof(len);
+ buf[pos++] = EVENT_TYPE_STRING;
+ 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)) {
+ jint intVal = 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)) {
+ jlong longVal = 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 {
+ jniThrowException(env,
+ "java/lang/IllegalArgumentException",
+ "Invalid payload item type");
+ return -1;
+ }
+ env->DeleteLocalRef(item);
}
- ByteBuf byteBuf(INITAL_BUFFER_CAPACITY);
- byteBuf.putStringEvent(env, value);
- byteBuf.putByte((uint8_t)END_DELIMITER);
- int numBytesPut = byteBuf.len;
- int bytesWritten = android_bWriteLog(tag, byteBuf.buf, numBytesPut);
- return bytesWritten;
+ buf[0] = EVENT_TYPE_LIST;
+ buf[1] = copied;
+ buf[pos++] = '\n';
+ return android_bWriteLog(tag, buf, pos);
}
/*
@@ -276,81 +207,6 @@ static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz,
}
/*
- * In class android.util.EventLog:
- * static native void readEvents(String path, Collection<Event> output)
- *
- * Reads events from a file (See Checkin.Aggregation). Events are stored in
- * native raw format (logger_entry + payload).
- */
-static void android_util_EventLog_readEventsFile(JNIEnv* env, jobject clazz, jstring path,
- jobject out) {
- if (path == NULL || out == NULL) {
- jniThrowException(env, "java/lang/NullPointerException", NULL);
- return;
- }
-
- const char *pathString = env->GetStringUTFChars(path, 0);
- int fd = open(pathString, O_RDONLY | O_NONBLOCK);
- env->ReleaseStringUTFChars(path, pathString);
-
- if (fd < 0) {
- jniThrowIOException(env, errno);
- return;
- }
-
- uint8_t buf[LOGGER_ENTRY_MAX_LEN];
- for (;;) {
- // read log entry structure from file
- int len = read(fd, buf, sizeof(logger_entry));
- if (len == 0) {
- break; // end of file
- } else if (len < 0) {
- jniThrowIOException(env, errno);
- } else if ((size_t) len < sizeof(logger_entry)) {
- jniThrowException(env, "java/io/IOException", "Event header too short");
- break;
- }
-
- // read event payload
- logger_entry* entry = (logger_entry*) buf;
- if (entry->len > LOGGER_ENTRY_MAX_PAYLOAD) {
- jniThrowException(env,
- "java/lang/IllegalArgumentException",
- "Too much data for event payload. Corrupt file?");
- break;
- }
-
- len = read(fd, buf + sizeof(logger_entry), entry->len);
- if (len == 0) {
- break; // end of file
- } else if (len < 0) {
- jniThrowIOException(env, errno);
- } else if ((size_t) len < entry->len) {
- jniThrowException(env, "java/io/IOException", "Event payload too short");
- break;
- }
-
- // create EventLog$Event and add it to the collection
- int buffer_size = sizeof(logger_entry) + entry->len;
- jbyteArray array = env->NewByteArray(buffer_size);
- if (array == NULL) break;
-
- jbyte *bytes = env->GetByteArrayElements(array, NULL);
- memcpy(bytes, buf, buffer_size);
- env->ReleaseByteArrayElements(array, bytes, 0);
-
- jobject event = env->NewObject(gEventClass, gEventInitID, array);
- if (event == NULL) break;
-
- env->CallBooleanMethod(out, gCollectionAddID, event);
- env->DeleteLocalRef(event);
- env->DeleteLocalRef(array);
- }
-
- close(fd);
-}
-
-/*
* JNI registration.
*/
static JNINativeMethod gRegisterMethods[] = {
@@ -362,22 +218,17 @@ static JNINativeMethod gRegisterMethods[] = {
(void*) android_util_EventLog_writeEvent_String
},
{ "writeEvent",
- "(ILandroid/util/EventLog$List;)I",
- (void*) android_util_EventLog_writeEvent_List
+ "(I[Ljava/lang/Object;)I",
+ (void*) android_util_EventLog_writeEvent_Array
},
{ "readEvents",
"([ILjava/util/Collection;)V",
(void*) android_util_EventLog_readEvents
},
- { "readEvents",
- "(Ljava/lang/String;Ljava/util/Collection;)V",
- (void*) android_util_EventLog_readEventsFile
- }
};
static struct { const char *name; jclass *clazz; } gClasses[] = {
{ "android/util/EventLog$Event", &gEventClass },
- { "android/util/EventLog$List", &gListClass },
{ "java/lang/Integer", &gIntegerClass },
{ "java/lang/Long", &gLongClass },
{ "java/lang/String", &gStringClass },
@@ -386,7 +237,6 @@ static struct { const char *name; jclass *clazz; } gClasses[] = {
static struct { jclass *c; const char *name, *ft; jfieldID *id; } gFields[] = {
{ &gIntegerClass, "value", "I", &gIntegerValueID },
- { &gListClass, "mItems", "[Ljava/lang/Object;", &gListItemsID },
{ &gLongClass, "value", "J", &gLongValueID },
};
@@ -430,4 +280,3 @@ int register_android_util_EventLog(JNIEnv* env) {
}
}; // namespace android
-