blob: 3b609524e8f987b3297aff326391d2746640dc84 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Mathias Agopian85cce372013-06-04 21:50:31 -070021#include <stdio.h>
22#include <stdlib.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070023#include <log/log.h>
Mathias Agopian85cce372013-06-04 21:50:31 -070024
25#include "EventLog.h"
26
Mathias Agopian85cce372013-06-04 21:50:31 -070027namespace android {
Mathias Agopian85cce372013-06-04 21:50:31 -070028
29ANDROID_SINGLETON_STATIC_INSTANCE(EventLog)
30
31
32EventLog::EventLog() {
33}
34
Dominik Laskowski87a07e42019-10-10 20:38:02 -070035void EventLog::doLogFrameDurations(const std::string_view& name, const int32_t* durations,
36 size_t numDurations) {
Jamie Gennis6547ff42013-07-16 20:12:42 -070037 EventLog::TagBuffer buffer(LOGTAG_SF_FRAME_DUR);
38 buffer.startList(1 + numDurations);
Dominik Laskowski87a07e42019-10-10 20:38:02 -070039 buffer.writeString(name);
Jamie Gennis6547ff42013-07-16 20:12:42 -070040 for (size_t i = 0; i < numDurations; i++) {
41 buffer.writeInt32(durations[i]);
42 }
Mathias Agopian85cce372013-06-04 21:50:31 -070043 buffer.endList();
44 buffer.log();
45}
46
Dominik Laskowski87a07e42019-10-10 20:38:02 -070047void EventLog::logFrameDurations(const std::string_view& name, const int32_t* durations,
48 size_t numDurations) {
49 EventLog::getInstance().doLogFrameDurations(name, durations, numDurations);
Mathias Agopian85cce372013-06-04 21:50:31 -070050}
51
52// ---------------------------------------------------------------------------
53
54EventLog::TagBuffer::TagBuffer(int32_t tag)
55 : mPos(0), mTag(tag), mOverflow(false) {
56}
57
58void 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
69void 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
81void 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
92void 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
104void 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
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700116void EventLog::TagBuffer::writeString(const std::string_view& value) {
Mathias Agopian85cce372013-06-04 21:50:31 -0700117 if (mOverflow) return;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700118 const size_t stringLen = value.length();
Mathias Agopian85cce372013-06-04 21:50:31 -0700119 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));
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700126 memcpy(&mStorage[mPos + 5], value.data(), stringLen);
Mathias Agopian85cce372013-06-04 21:50:31 -0700127 mPos += needed;
128}
129
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700130} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800131
132// TODO(b/129481165): remove the #pragma below and fix conversion issues
133#pragma clang diagnostic pop // ignored "-Wconversion"