1 /*
2  * Copyright (C) 2007-2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android-base/macros.h>
18 #include <log/log_id.h>
19 
20 #include <nativehelper/JNIHelp.h>
21 #include "jni.h"
22 
23 #include "core_jni_helpers.h"
24 #include "eventlog_helper.h"
25 
26 namespace android {
27 
28 constexpr char kEventLogEventClass[] = "android/util/EventLog$Event";
29 template class EventLogHelper<log_id_t::LOG_ID_EVENTS, kEventLogEventClass>;
30 using ELog = EventLogHelper<log_id_t::LOG_ID_EVENTS, kEventLogEventClass>;
31 
32 /*
33  * In class android.util.EventLog:
34  *  static native void readEvents(int[] tags, Collection<Event> output)
35  *
36  *  Reads events from the event log
37  */
android_util_EventLog_readEvents(JNIEnv * env,jobject clazz ATTRIBUTE_UNUSED,jintArray tags,jobject out)38 static void android_util_EventLog_readEvents(JNIEnv* env, jobject clazz ATTRIBUTE_UNUSED,
39                                              jintArray tags,
40                                              jobject out) {
41 
42     if (tags == NULL || out == NULL) {
43         jniThrowNullPointerException(env, NULL);
44         return;
45     }
46 
47     ELog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, tags, 0, out);
48  }
49 /*
50  * In class android.util.EventLog:
51  *  static native void readEventsOnWrapping(int[] tags, long timestamp, Collection<Event> output)
52  *
53  *  Reads events from the event log, blocking until events after timestamp are to be overwritten.
54  */
android_util_EventLog_readEventsOnWrapping(JNIEnv * env,jobject clazz ATTRIBUTE_UNUSED,jintArray tags,jlong timestamp,jobject out)55 static void android_util_EventLog_readEventsOnWrapping(JNIEnv* env, jobject clazz ATTRIBUTE_UNUSED,
56                                              jintArray tags,
57                                              jlong timestamp,
58                                              jobject out) {
59     if (tags == NULL || out == NULL) {
60         jniThrowNullPointerException(env, NULL);
61         return;
62     }
63     ELog::readEvents(env, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK | ANDROID_LOG_WRAP, tags,
64             timestamp, out);
65 }
66 
67 /*
68  * JNI registration.
69  */
70 static const JNINativeMethod gRegisterMethods[] = {
71     /* name, signature, funcPtr */
72     { "writeEvent", "(II)I", (void*) ELog::writeEventInteger },
73     { "writeEvent", "(IJ)I", (void*) ELog::writeEventLong },
74     { "writeEvent", "(IF)I", (void*) ELog::writeEventFloat },
75     { "writeEvent", "(ILjava/lang/String;)I", (void*) ELog::writeEventString },
76     { "writeEvent", "(I[Ljava/lang/Object;)I", (void*) ELog::writeEventArray },
77     { "readEvents",
78       "([ILjava/util/Collection;)V",
79       (void*) android_util_EventLog_readEvents
80     },
81     { "readEventsOnWrapping",
82       "([IJLjava/util/Collection;)V",
83       (void*) android_util_EventLog_readEventsOnWrapping
84     },
85 };
86 
register_android_util_EventLog(JNIEnv * env)87 int register_android_util_EventLog(JNIEnv* env) {
88     ELog::Init(env);
89 
90     return RegisterMethodsOrDie(
91             env,
92             "android/util/EventLog",
93             gRegisterMethods, NELEM(gRegisterMethods));
94 }
95 
96 }; // namespace android
97