blob: a251056d9e81f219967b53834a0621f7551bb3f7 [file] [log] [blame]
Yao Chenab273e22017-09-06 12:53:50 -07001/*
2 * Copyright (C) 2017 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 <android/os/DropBoxManager.h>
18#include <cutils/log.h>
19
20#include "DropboxWriter.h"
21
22using android::os::DropBoxManager;
23using android::binder::Status;
24using android::sp;
25using android::String16;
26using std::vector;
27
28DropboxWriter::DropboxWriter(const string& tag)
29 : mTag(tag), mLogList(), mBufferSize(0) {
30}
31
32void DropboxWriter::addEntry(const StatsLogEntry& entry) {
33 flushIfNecessary(entry);
34 StatsLogEntry* newEntry = mLogList.add_stats_log_entry();
35 newEntry->CopyFrom(entry);
36 mBufferSize += entry.ByteSize();
37}
38
39void DropboxWriter::flushIfNecessary(const StatsLogEntry& entry) {
40 // The serialized size of the StatsLogList is approximately the sum of the serialized size of
41 // every StatsLogEntry inside it.
42 if (entry.ByteSize() + mBufferSize > kMaxSerializedBytes) {
43 flush();
44 }
45}
46
47void DropboxWriter::flush() {
48 // now we get an exact byte size of the output
49 const int numBytes = mLogList.ByteSize();
50 vector<uint8_t> buffer(numBytes);
51 sp<DropBoxManager> dropbox = new DropBoxManager();
52 mLogList.SerializeToArray(&buffer[0], numBytes);
53 Status status = dropbox->addData(String16(mTag.c_str()), &buffer[0],
54 numBytes, 0 /* no flag */);
55 if (!status.isOk()) {
56 ALOGE("failed to write to dropbox");
57 //TODO: What to do if flush fails??
58 }
59 mLogList.Clear();
60 mBufferSize = 0;
61}