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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <cutils/log.h>
20 #include <utils/String8.h>
21
22 #include "EventLog.h"
23
24 // ---------------------------------------------------------------------------
25 namespace android {
26 // ---------------------------------------------------------------------------
27
ANDROID_SINGLETON_STATIC_INSTANCE(EventLog)28 ANDROID_SINGLETON_STATIC_INSTANCE(EventLog)
29
30
31 EventLog::EventLog() {
32 }
33
doLogFrameDurations(const String8 & window,const int32_t * durations,size_t numDurations)34 void EventLog::doLogFrameDurations(const String8& window,
35 const int32_t* durations, size_t numDurations) {
36 EventLog::TagBuffer buffer(LOGTAG_SF_FRAME_DUR);
37 buffer.startList(1 + numDurations);
38 buffer.writeString8(window);
39 for (size_t i = 0; i < numDurations; i++) {
40 buffer.writeInt32(durations[i]);
41 }
42 buffer.endList();
43 buffer.log();
44 }
45
logFrameDurations(const String8 & window,const int32_t * durations,size_t numDurations)46 void EventLog::logFrameDurations(const String8& window,
47 const int32_t* durations, size_t numDurations) {
48 EventLog::getInstance().doLogFrameDurations(window, durations,
49 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
writeString8(const String8 & value)116 void EventLog::TagBuffer::writeString8(const String8& value) {
117 if (mOverflow) return;
118 const int32_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.string(), stringLen);
127 mPos += needed;
128 }
129
130 // ---------------------------------------------------------------------------
131 }// namespace android
132
133 // ---------------------------------------------------------------------------
134