James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "event_log_list_builder.h" |
| 18 | |
| 19 | #include <cinttypes> |
| 20 | #include <string> |
| 21 | #include <base/logging.h> |
| 22 | #include <log/log.h> |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const size_t MAX_EVENT_PAYLOAD_SIZE = 512 - 1; // Leave room for final '\n'. |
| 27 | const size_t EVENT_TYPE_SIZE = 1; // Size in bytes of the event type marker. |
| 28 | |
| 29 | } // namespace |
| 30 | |
| 31 | EventLogListBuilder::EventLogListBuilder() |
| 32 | : payload_count_(0), |
| 33 | payload_size_(0), |
| 34 | payload_(std::make_unique<uint8_t[]>(MAX_EVENT_PAYLOAD_SIZE)) { |
| 35 | memset(payload_.get(), 0, MAX_EVENT_PAYLOAD_SIZE); |
| 36 | |
| 37 | // Set up the top-level EventLog data type. |
| 38 | AppendByte(EVENT_TYPE_LIST); |
| 39 | |
| 40 | // Skip over the byte prepresenting the number of items in the list. This |
| 41 | // value is set in Release(). |
| 42 | payload_size_++; |
| 43 | } |
| 44 | |
| 45 | bool EventLogListBuilder::Append(int value) { |
| 46 | DCHECK_NE(static_cast<uint8_t*>(nullptr), payload_.get()); |
| 47 | |
| 48 | if (!IsSpaceAvailable(sizeof(value) + EVENT_TYPE_SIZE)) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | AppendByte(EVENT_TYPE_INT); |
| 53 | AppendData(&value, sizeof(value)); |
| 54 | |
| 55 | payload_count_++; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | bool EventLogListBuilder::Append(const std::string& value) { |
| 60 | DCHECK_NE(static_cast<uint8_t*>(nullptr), payload_.get()); |
| 61 | |
| 62 | int len = value.length(); |
| 63 | if (!IsSpaceAvailable(sizeof(len) + len)) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | AppendByte(EVENT_TYPE_STRING); |
| 68 | AppendData(&len, sizeof(len)); |
| 69 | AppendData(value.c_str(), len); |
| 70 | |
| 71 | payload_count_++; |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | void EventLogListBuilder::Release(std::unique_ptr<uint8_t[]>* log, |
| 76 | size_t* size) { |
| 77 | // Finalize the log payload. |
| 78 | payload_[1] = payload_count_; |
| 79 | |
| 80 | // Return the log payload. |
| 81 | *size = payload_size_; |
| 82 | *log = std::move(payload_); |
| 83 | } |
| 84 | |
| 85 | void EventLogListBuilder::AppendData(const void* data, size_t size) { |
| 86 | DCHECK_LT(payload_size_ + size, MAX_EVENT_PAYLOAD_SIZE); |
| 87 | memcpy(&payload_[payload_size_], data, size); |
| 88 | payload_size_ += size; |
| 89 | } |
| 90 | |
| 91 | void EventLogListBuilder::AppendByte(uint8_t byte) { |
| 92 | DCHECK_LT(payload_size_ + sizeof(byte), MAX_EVENT_PAYLOAD_SIZE); |
| 93 | payload_[payload_size_++] = byte; |
| 94 | } |
| 95 | |
| 96 | bool EventLogListBuilder::IsSpaceAvailable(size_t value_size) { |
| 97 | size_t space_needed = value_size + EVENT_TYPE_SIZE; |
| 98 | if (payload_size_ + space_needed > MAX_EVENT_PAYLOAD_SIZE) { |
| 99 | size_t remaining = MAX_EVENT_PAYLOAD_SIZE - payload_size_; |
| 100 | LOG(WARNING) << "Not enough space for value. remain=" << |
| 101 | remaining << "; needed=" << space_needed; |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |