Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ddd8c26c authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change 6320

* changes:
  Add method to read events from a file.  Remove unused method.  Fixed after review.
parents 03217ae7 95ff2401
Loading
Loading
Loading
Loading
+10 −1
Original line number Original line Diff line number Diff line
@@ -73,7 +73,7 @@ import java.util.List;
 *   </ul>
 *   </ul>
 * </li>
 * </li>
 * <li> '\n': 1 byte - an automatically generated newline, used to help detect and recover from log
 * <li> '\n': 1 byte - an automatically generated newline, used to help detect and recover from log
 *                     corruption and enable stansard unix tools like grep, tail and wc to operate
 *                     corruption and enable standard unix tools like grep, tail and wc to operate
 *                     on event logs. </li>
 *                     on event logs. </li>
 * </ul>
 * </ul>
 *
 *
@@ -289,4 +289,13 @@ public class EventLog {
     */
     */
    public static native void readEvents(int[] tags, Collection<Event> output)
    public static native void readEvents(int[] tags, Collection<Event> output)
            throws IOException;
            throws IOException;

    /**
     * Read events from a file.
     * @param path to read from
     * @param output container to add events into
     * @throws IOException if something goes wrong reading events
     */
    public static native void readEvents(String path, Collection<Event> output)
            throws IOException;
}
}
+86 −6
Original line number Original line Diff line number Diff line
@@ -210,6 +210,8 @@ static jint android_util_EventLog_writeEvent_String(JNIEnv* env, jobject clazz,
/*
/*
 * In class android.util.EventLog:
 * In class android.util.EventLog:
 *  static native void readEvents(int[] tags, Collection<Event> output)
 *  static native void readEvents(int[] tags, Collection<Event> output)
 *
 *  Reads events from the event log, typically /dev/log/events
 */
 */
static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz,
static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz,
                                             jintArray tags,
                                             jintArray tags,
@@ -273,6 +275,80 @@ static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz,
    env->ReleaseIntArrayElements(tags, tagValues, 0);
    env->ReleaseIntArrayElements(tags, tagValues, 0);
}
}


/*
 * 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.
 * JNI registration.
@@ -292,6 +368,10 @@ static JNINativeMethod gRegisterMethods[] = {
    { "readEvents",
    { "readEvents",
      "([ILjava/util/Collection;)V",
      "([ILjava/util/Collection;)V",
      (void*) android_util_EventLog_readEvents
      (void*) android_util_EventLog_readEvents
    },
    { "readEvents",
      "(Ljava/lang/String;Ljava/util/Collection;)V",
      (void*) android_util_EventLog_readEventsFile
    }
    }
};
};