Mathias Agopian | 85cce37 | 2013-06-04 21:50:31 -0700 | [diff] [blame] | 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> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 19 | #include <log/log.h> |
Mathias Agopian | 85cce37 | 2013-06-04 21:50:31 -0700 | [diff] [blame] | 20 | |
| 21 | #include "EventLog.h" |
| 22 | |
Mathias Agopian | 85cce37 | 2013-06-04 21:50:31 -0700 | [diff] [blame] | 23 | namespace android { |
Mathias Agopian | 85cce37 | 2013-06-04 21:50:31 -0700 | [diff] [blame] | 24 | |
| 25 | ANDROID_SINGLETON_STATIC_INSTANCE(EventLog) |
| 26 | |
| 27 | |
| 28 | EventLog::EventLog() { |
| 29 | } |
| 30 | |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 31 | void EventLog::doLogFrameDurations(const std::string_view& name, const int32_t* durations, |
| 32 | size_t numDurations) { |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 33 | EventLog::TagBuffer buffer(LOGTAG_SF_FRAME_DUR); |
| 34 | buffer.startList(1 + numDurations); |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 35 | buffer.writeString(name); |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 36 | for (size_t i = 0; i < numDurations; i++) { |
| 37 | buffer.writeInt32(durations[i]); |
| 38 | } |
Mathias Agopian | 85cce37 | 2013-06-04 21:50:31 -0700 | [diff] [blame] | 39 | buffer.endList(); |
| 40 | buffer.log(); |
| 41 | } |
| 42 | |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 43 | void EventLog::logFrameDurations(const std::string_view& name, const int32_t* durations, |
| 44 | size_t numDurations) { |
| 45 | EventLog::getInstance().doLogFrameDurations(name, durations, numDurations); |
Mathias Agopian | 85cce37 | 2013-06-04 21:50:31 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // --------------------------------------------------------------------------- |
| 49 | |
| 50 | EventLog::TagBuffer::TagBuffer(int32_t tag) |
| 51 | : mPos(0), mTag(tag), mOverflow(false) { |
| 52 | } |
| 53 | |
| 54 | void 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 | |
| 65 | void 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 | |
| 77 | void 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 | |
| 88 | void 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 | |
| 100 | void 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 Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 112 | void EventLog::TagBuffer::writeString(const std::string_view& value) { |
Mathias Agopian | 85cce37 | 2013-06-04 21:50:31 -0700 | [diff] [blame] | 113 | if (mOverflow) return; |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 114 | const size_t stringLen = value.length(); |
Mathias Agopian | 85cce37 | 2013-06-04 21:50:31 -0700 | [diff] [blame] | 115 | 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 Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 122 | memcpy(&mStorage[mPos + 5], value.data(), stringLen); |
Mathias Agopian | 85cce37 | 2013-06-04 21:50:31 -0700 | [diff] [blame] | 123 | mPos += needed; |
| 124 | } |
| 125 | |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 126 | } // namespace android |