blob: a532fc130ffed169199afe2baf162b01350a930d [file] [log] [blame]
Mathias Agopian85cce372013-06-04 21:50:31 -07001/*
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>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070019#include <log/log.h>
Mathias Agopian85cce372013-06-04 21:50:31 -070020
21#include "EventLog.h"
22
Mathias Agopian85cce372013-06-04 21:50:31 -070023namespace android {
Mathias Agopian85cce372013-06-04 21:50:31 -070024
25ANDROID_SINGLETON_STATIC_INSTANCE(EventLog)
26
27
28EventLog::EventLog() {
29}
30
Dominik Laskowski87a07e42019-10-10 20:38:02 -070031void EventLog::doLogFrameDurations(const std::string_view& name, const int32_t* durations,
32 size_t numDurations) {
Jamie Gennis6547ff42013-07-16 20:12:42 -070033 EventLog::TagBuffer buffer(LOGTAG_SF_FRAME_DUR);
34 buffer.startList(1 + numDurations);
Dominik Laskowski87a07e42019-10-10 20:38:02 -070035 buffer.writeString(name);
Jamie Gennis6547ff42013-07-16 20:12:42 -070036 for (size_t i = 0; i < numDurations; i++) {
37 buffer.writeInt32(durations[i]);
38 }
Mathias Agopian85cce372013-06-04 21:50:31 -070039 buffer.endList();
40 buffer.log();
41}
42
Dominik Laskowski87a07e42019-10-10 20:38:02 -070043void EventLog::logFrameDurations(const std::string_view& name, const int32_t* durations,
44 size_t numDurations) {
45 EventLog::getInstance().doLogFrameDurations(name, durations, numDurations);
Mathias Agopian85cce372013-06-04 21:50:31 -070046}
47
48// ---------------------------------------------------------------------------
49
50EventLog::TagBuffer::TagBuffer(int32_t tag)
51 : mPos(0), mTag(tag), mOverflow(false) {
52}
53
54void EventLog::TagBuffer::log() {
55 if (mOverflow) {
56 ALOGW("couldn't log to binary event log: overflow.");
57 } else if (android_bWriteLog(mTag, mStorage, mPos) < 0) {
58 ALOGE("couldn't log to EventLog: %s", strerror(errno));
59 }
60 // purge the buffer
61 mPos = 0;
62 mOverflow = false;
63}
64
65void EventLog::TagBuffer::startList(int8_t count) {
66 if (mOverflow) return;
67 const size_t needed = 1 + sizeof(count);
68 if (mPos + needed > STORAGE_MAX_SIZE) {
69 mOverflow = true;
70 return;
71 }
72 mStorage[mPos + 0] = EVENT_TYPE_LIST;
73 mStorage[mPos + 1] = count;
74 mPos += needed;
75}
76
77void EventLog::TagBuffer::endList() {
78 if (mOverflow) return;
79 const size_t needed = 1;
80 if (mPos + needed > STORAGE_MAX_SIZE) {
81 mOverflow = true;
82 return;
83 }
84 mStorage[mPos + 0] = '\n';
85 mPos += needed;
86}
87
88void EventLog::TagBuffer::writeInt32(int32_t value) {
89 if (mOverflow) return;
90 const size_t needed = 1 + sizeof(value);
91 if (mPos + needed > STORAGE_MAX_SIZE) {
92 mOverflow = true;
93 return;
94 }
95 mStorage[mPos + 0] = EVENT_TYPE_INT;
96 memcpy(&mStorage[mPos + 1], &value, sizeof(value));
97 mPos += needed;
98}
99
100void EventLog::TagBuffer::writeInt64(int64_t value) {
101 if (mOverflow) return;
102 const size_t needed = 1 + sizeof(value);
103 if (mPos + needed > STORAGE_MAX_SIZE) {
104 mOverflow = true;
105 return;
106 }
107 mStorage[mPos + 0] = EVENT_TYPE_LONG;
108 memcpy(&mStorage[mPos + 1], &value, sizeof(value));
109 mPos += needed;
110}
111
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700112void EventLog::TagBuffer::writeString(const std::string_view& value) {
Mathias Agopian85cce372013-06-04 21:50:31 -0700113 if (mOverflow) return;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700114 const size_t stringLen = value.length();
Mathias Agopian85cce372013-06-04 21:50:31 -0700115 const size_t needed = 1 + sizeof(int32_t) + stringLen;
116 if (mPos + needed > STORAGE_MAX_SIZE) {
117 mOverflow = true;
118 return;
119 }
120 mStorage[mPos + 0] = EVENT_TYPE_STRING;
121 memcpy(&mStorage[mPos + 1], &stringLen, sizeof(int32_t));
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700122 memcpy(&mStorage[mPos + 5], value.data(), stringLen);
Mathias Agopian85cce372013-06-04 21:50:31 -0700123 mPos += needed;
124}
125
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700126} // namespace android