Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 291fd678 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I8c58d6bd,I0a19fd87

* changes:
  bootstat: drop event_log_list_builder
  liblog: logd: logcat: Split out log/logger.h into public and private.
parents 63660add 343b76e4
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

bootstat_lib_src_files = [
    "boot_event_record_store.cpp",
    "event_log_list_builder.cpp",
    "histogram_logger.cpp",
    "uptime_parser.cpp",
]
@@ -88,7 +87,6 @@ cc_test {
    ],
    srcs: [
        "boot_event_record_store_test.cpp",
        "event_log_list_builder_test.cpp",
        "testrunner.cpp",
    ],
}
+0 −1
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@
#include <cutils/properties.h>

#include "boot_event_record_store.h"
#include "event_log_list_builder.h" /* ToDo: switch to liblog implementation */
#include "histogram_logger.h"
#include "uptime_parser.h"

+0 −108
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "event_log_list_builder.h"

#include <cinttypes>
#include <memory>
#include <string>

#include <log/log.h>
#include <android-base/logging.h>

namespace {

const size_t MAX_EVENT_PAYLOAD_SIZE = 512 - 1;  // Leave room for final '\n'.
const size_t EVENT_TYPE_SIZE = 1;  // Size in bytes of the event type marker.

}  // namespace

EventLogListBuilder::EventLogListBuilder()
    : payload_count_(0),
      payload_size_(0),
      payload_(std::make_unique<uint8_t[]>(MAX_EVENT_PAYLOAD_SIZE)) {
  memset(payload_.get(), 0, MAX_EVENT_PAYLOAD_SIZE);

  // Set up the top-level EventLog data type.
  AppendByte(EVENT_TYPE_LIST);

  // Skip over the byte prepresenting the number of items in the list. This
  // value is set in Release().
  payload_size_++;
}

bool EventLogListBuilder::Append(int value) {
  DCHECK_NE(static_cast<uint8_t*>(nullptr), payload_.get());

  if (!IsSpaceAvailable(sizeof(value) + EVENT_TYPE_SIZE)) {
    return false;
  }

  AppendByte(EVENT_TYPE_INT);
  AppendData(&value, sizeof(value));

  payload_count_++;
  return true;
}

bool EventLogListBuilder::Append(const std::string& value) {
  DCHECK_NE(static_cast<uint8_t*>(nullptr), payload_.get());

  int len = value.length();
  if (!IsSpaceAvailable(sizeof(len) + len)) {
    return false;
  }

  AppendByte(EVENT_TYPE_STRING);
  AppendData(&len, sizeof(len));
  AppendData(value.c_str(), len);

  payload_count_++;
  return true;
}

void EventLogListBuilder::Release(std::unique_ptr<uint8_t[]>* log,
                                  size_t* size) {
  // Finalize the log payload.
  payload_[1] = payload_count_;

  // Return the log payload.
  *size = payload_size_;
  *log = std::move(payload_);
}

void EventLogListBuilder::AppendData(const void* data, size_t size) {
  DCHECK_LT(payload_size_ + size, MAX_EVENT_PAYLOAD_SIZE);
  memcpy(&payload_[payload_size_], data, size);
  payload_size_ += size;
}

void EventLogListBuilder::AppendByte(uint8_t byte) {
  DCHECK_LT(payload_size_ + sizeof(byte), MAX_EVENT_PAYLOAD_SIZE);
  payload_[payload_size_++] = byte;
}

bool EventLogListBuilder::IsSpaceAvailable(size_t value_size) {
  size_t space_needed = value_size + EVENT_TYPE_SIZE;
  if (payload_size_ + space_needed > MAX_EVENT_PAYLOAD_SIZE) {
    size_t remaining = MAX_EVENT_PAYLOAD_SIZE - payload_size_;
    LOG(WARNING) << "Not enough space for value. remain=" <<
        remaining << "; needed=" << space_needed;
    return false;
  }

  return true;
}

bootstat/event_log_list_builder.h

deleted100644 → 0
+0 −70
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef EVENT_LOG_LIST_BUILDER_H_
#define EVENT_LOG_LIST_BUILDER_H_

#include <cstdint>
#include <memory>

#include <android-base/macros.h>

// EventLogListBuilder provides a mechanism to build an EventLog list
// consisting of int and string EventLog values.
//
// NOTE: This class does not provide the ability to append an embedded list,
// i.e., a list containing a list.
class EventLogListBuilder {
 public:
  EventLogListBuilder();

  // Append a single value of a specified type.
  bool Append(int value);
  bool Append(const std::string& value);

  // Finalizes construction of the EventLog list and releases the data
  // to the caller. Caller takes ownership of the payload. No further calls
  // to append* may be made once the payload is acquired by the caller.
  void Release(std::unique_ptr<uint8_t[]>* log, size_t* size);

 private:
  // Appends |data| of the given |size| to the payload.
  void AppendData(const void* data, size_t size);

  // Appends a single byte to the payload.
  void AppendByte(uint8_t byte);

  // Returns true iff the remaining capacity in |payload_| is large enough to
  // accommodate |value_size| bytes. The space required to log the event type
  // is included in the internal calculation so must not be passed in to
  // |value_size|.
  bool IsSpaceAvailable(size_t value_size);

  // The number of items in the EventLog list.
  size_t payload_count_;

  // The size of the data stored in |payload_|. Used to track where to insert
  // new data.
  size_t payload_size_;

  // The payload constructed by calls to log*. The payload may only contain
  // MAX_EVENT_PAYLOAD (512) bytes.
  std::unique_ptr<uint8_t[]> payload_;

  DISALLOW_COPY_AND_ASSIGN(EventLogListBuilder);
};

 #endif  // EVENT_LOG_LIST_BUILDER_H_
+0 −114
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "event_log_list_builder.h"

#include <inttypes.h>

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <log/log.h>

using testing::ElementsAreArray;

TEST(EventLogListBuilder, Empty) {
  EventLogListBuilder builder;

  const uint8_t EXPECTED_LOG[] = {
    EVENT_TYPE_LIST,
    0,  // Number of items in the list.
  };

  std::unique_ptr<uint8_t[]> log;
  size_t size;
  builder.Release(&log, &size);
  EXPECT_EQ(2U, size);

  uint8_t* log_data = log.get();
  EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size),
              ElementsAreArray(EXPECTED_LOG));
}

TEST(EventLogListBuilder, SingleInt) {
  EventLogListBuilder builder;

  const uint8_t EXPECTED_LOG[] = {
    EVENT_TYPE_LIST,
    1,                // Number of items in the list.
    EVENT_TYPE_INT,
    42, 0, 0, 0,      // 4 byte integer value.
  };

  builder.Append(42);

  std::unique_ptr<uint8_t[]> log;
  size_t size;
  builder.Release(&log, &size);
  EXPECT_EQ(7U, size);

  uint8_t* log_data = log.get();
  EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size),
              ElementsAreArray(EXPECTED_LOG));
}

TEST(EventLogListBuilder, SingleString) {
  EventLogListBuilder builder;

  const uint8_t EXPECTED_LOG[] = {
    EVENT_TYPE_LIST,
    1,                        // Number of items in the list.
    EVENT_TYPE_STRING,
    5, 0, 0, 0,               // 4 byte length of the string.
    'D', 'r', 'o', 'i', 'd',
  };

  builder.Append("Droid");

  std::unique_ptr<uint8_t[]> log;
  size_t size;
  builder.Release(&log, &size);
  EXPECT_EQ(12U, size);

  uint8_t* log_data = log.get();
  EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size),
              ElementsAreArray(EXPECTED_LOG));
}

TEST(EventLogListBuilder, IntThenString) {
  EventLogListBuilder builder;

  const uint8_t EXPECTED_LOG[] = {
    EVENT_TYPE_LIST,
    2,                        // Number of items in the list.
    EVENT_TYPE_INT,
    42, 0, 0, 0,              // 4 byte integer value.
    EVENT_TYPE_STRING,
    5, 0, 0, 0,               // 4 byte length of the string.
    'D', 'r', 'o', 'i', 'd',
  };

  builder.Append(42);
  builder.Append("Droid");

  std::unique_ptr<uint8_t[]> log;
  size_t size;
  builder.Release(&log, &size);
  EXPECT_EQ(17U, size);

  uint8_t* log_data = log.get();
  EXPECT_THAT(std::vector<uint8_t>(log_data, log_data + size),
              ElementsAreArray(EXPECTED_LOG));
}
Loading