blob: b9d48fa362d57402eac90c743c2e4160e24eceb4 [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>
Yao Chenab273e22017-09-06 12:53:50 -070018
19#include "DropboxWriter.h"
20
Yao Chenab273e22017-09-06 12:53:50 -070021using android::binder::Status;
Bookatz906a35c2017-09-20 15:26:44 -070022using android::os::DropBoxManager;
Yao Chenab273e22017-09-06 12:53:50 -070023using android::sp;
24using android::String16;
25using std::vector;
26
Bookatz906a35c2017-09-20 15:26:44 -070027namespace android {
28namespace os {
29namespace statsd {
30
Yao Chenab273e22017-09-06 12:53:50 -070031DropboxWriter::DropboxWriter(const string& tag)
yro00698da2017-09-15 10:06:40 -070032 : mTag(tag), mLogReport(), mBufferSize(0) {
Yao Chenab273e22017-09-06 12:53:50 -070033}
34
yro00698da2017-09-15 10:06:40 -070035void DropboxWriter::addStatsLogReport(const StatsLogReport& log) {
36 mLogReport = log;
37 flushIfNecessary(log);
38 mBufferSize += log.ByteSize();
Yao Chenab273e22017-09-06 12:53:50 -070039}
40
yro00698da2017-09-15 10:06:40 -070041void DropboxWriter::flushIfNecessary(const StatsLogReport& log) {
42 // TODO: Decide to flush depending on the serialized size of the StatsLogReport.
43 // if (entry.ByteSize() + mBufferSize > kMaxSerializedBytes) {
44 // flush();
45 // }
46 flush();
Yao Chenab273e22017-09-06 12:53:50 -070047}
48
49void DropboxWriter::flush() {
50 // now we get an exact byte size of the output
yro00698da2017-09-15 10:06:40 -070051 const int numBytes = mLogReport.ByteSize();
Yao Chenab273e22017-09-06 12:53:50 -070052 vector<uint8_t> buffer(numBytes);
53 sp<DropBoxManager> dropbox = new DropBoxManager();
yro00698da2017-09-15 10:06:40 -070054 mLogReport.SerializeToArray(&buffer[0], numBytes);
Yao Chenab273e22017-09-06 12:53:50 -070055 Status status = dropbox->addData(String16(mTag.c_str()), &buffer[0],
56 numBytes, 0 /* no flag */);
57 if (!status.isOk()) {
58 ALOGE("failed to write to dropbox");
59 //TODO: What to do if flush fails??
60 }
yro00698da2017-09-15 10:06:40 -070061 mLogReport.Clear();
Yao Chenab273e22017-09-06 12:53:50 -070062 mBufferSize = 0;
63}
Bookatz906a35c2017-09-20 15:26:44 -070064
65} // namespace statsd
66} // namespace os
67} // namespace android