1 /*
2 * Copyright 2013 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <log/log.h>
24
25 #include "EventLog.h"
26
27 namespace android {
28
ANDROID_SINGLETON_STATIC_INSTANCE(EventLog)29 ANDROID_SINGLETON_STATIC_INSTANCE(EventLog)
30
31
32 EventLog::EventLog() {
33 }
34
doLogFrameDurations(const std::string_view & name,const int32_t * durations,size_t numDurations)35 void EventLog::doLogFrameDurations(const std::string_view& name, const int32_t* durations,
36 size_t numDurations) {
37 EventLog::TagBuffer buffer(LOGTAG_SF_FRAME_DUR);
38 buffer.startList(1 + numDurations);
39 buffer.writeString(name);
40 for (size_t i = 0; i < numDurations; i++) {
41 buffer.writeInt32(durations[i]);
42 }
43 buffer.endList();
44 buffer.log();
45 }
46
logFrameDurations(const std::string_view & name,const int32_t * durations,size_t numDurations)47 void EventLog::logFrameDurations(const std::string_view& name, const int32_t* durations,
48 size_t numDurations) {
49 EventLog::getInstance().doLogFrameDurations(name, durations, numDurations);
50 }
51
52 // ---------------------------------------------------------------------------
53
TagBuffer(int32_t tag)54 EventLog::TagBuffer::TagBuffer(int32_t tag)
55 : mPos(0), mTag(tag), mOverflow(false) {
56 }
57
log()58 void EventLog::TagBuffer::log() {
59 if (mOverflow) {
60 ALOGW("couldn't log to binary event log: overflow.");
61 } else if (android_bWriteLog(mTag, mStorage, mPos) < 0) {
62 ALOGE("couldn't log to EventLog: %s", strerror(errno));
63 }
64 // purge the buffer
65 mPos = 0;
66 mOverflow = false;
67 }
68
startList(int8_t count)69 void EventLog::TagBuffer::startList(int8_t count) {
70 if (mOverflow) return;
71 const size_t needed = 1 + sizeof(count);
72 if (mPos + needed > STORAGE_MAX_SIZE) {
73 mOverflow = true;
74 return;
75 }
76 mStorage[mPos + 0] = EVENT_TYPE_LIST;
77 mStorage[mPos + 1] = count;
78 mPos += needed;
79 }
80
endList()81 void EventLog::TagBuffer::endList() {
82 if (mOverflow) return;
83 const size_t needed = 1;
84 if (mPos + needed > STORAGE_MAX_SIZE) {
85 mOverflow = true;
86 return;
87 }
88 mStorage[mPos + 0] = '\n';
89 mPos += needed;
90 }
91
writeInt32(int32_t value)92 void EventLog::TagBuffer::writeInt32(int32_t value) {
93 if (mOverflow) return;
94 const size_t needed = 1 + sizeof(value);
95 if (mPos + needed > STORAGE_MAX_SIZE) {
96 mOverflow = true;
97 return;
98 }
99 mStorage[mPos + 0] = EVENT_TYPE_INT;
100 memcpy(&mStorage[mPos + 1], &value, sizeof(value));
101 mPos += needed;
102 }
103
writeInt64(int64_t value)104 void EventLog::TagBuffer::writeInt64(int64_t value) {
105 if (mOverflow) return;
106 const size_t needed = 1 + sizeof(value);
107 if (mPos + needed > STORAGE_MAX_SIZE) {
108 mOverflow = true;
109 return;
110 }
111 mStorage[mPos + 0] = EVENT_TYPE_LONG;
112 memcpy(&mStorage[mPos + 1], &value, sizeof(value));
113 mPos += needed;
114 }
115
writeString(const std::string_view & value)116 void EventLog::TagBuffer::writeString(const std::string_view& value) {
117 if (mOverflow) return;
118 const size_t stringLen = value.length();
119 const size_t needed = 1 + sizeof(int32_t) + stringLen;
120 if (mPos + needed > STORAGE_MAX_SIZE) {
121 mOverflow = true;
122 return;
123 }
124 mStorage[mPos + 0] = EVENT_TYPE_STRING;
125 memcpy(&mStorage[mPos + 1], &stringLen, sizeof(int32_t));
126 memcpy(&mStorage[mPos + 5], value.data(), stringLen);
127 mPos += needed;
128 }
129
130 } // namespace android
131
132 // TODO(b/129481165): remove the #pragma below and fix conversion issues
133 #pragma clang diagnostic pop // ignored "-Wconversion"
134