blob: 37df83463e451fb9f27848e1ba635a4f0a50b060 [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 Chenef99c4f2017-09-22 16:26:54 -070021using android::String16;
Yao Chenab273e22017-09-06 12:53:50 -070022using android::binder::Status;
Bookatz906a35c2017-09-20 15:26:44 -070023using android::os::DropBoxManager;
Yao Chenab273e22017-09-06 12:53:50 -070024using android::sp;
Yao Chenab273e22017-09-06 12:53:50 -070025using std::vector;
26
Bookatz906a35c2017-09-20 15:26:44 -070027namespace android {
28namespace os {
29namespace statsd {
30
Yao Chenef99c4f2017-09-22 16:26:54 -070031DropboxWriter::DropboxWriter(const string& tag) : mTag(tag), mLogReport(), mBufferSize(0) {
Yao Chenab273e22017-09-06 12:53:50 -070032}
33
yro00698da2017-09-15 10:06:40 -070034void DropboxWriter::addStatsLogReport(const StatsLogReport& log) {
35 mLogReport = log;
36 flushIfNecessary(log);
37 mBufferSize += log.ByteSize();
Yao Chenab273e22017-09-06 12:53:50 -070038}
39
yro00698da2017-09-15 10:06:40 -070040void DropboxWriter::flushIfNecessary(const StatsLogReport& log) {
41 // TODO: Decide to flush depending on the serialized size of the StatsLogReport.
42 // if (entry.ByteSize() + mBufferSize > kMaxSerializedBytes) {
43 // flush();
44 // }
45 flush();
Yao Chenab273e22017-09-06 12:53:50 -070046}
47
48void DropboxWriter::flush() {
49 // now we get an exact byte size of the output
yro00698da2017-09-15 10:06:40 -070050 const int numBytes = mLogReport.ByteSize();
Yao Chenab273e22017-09-06 12:53:50 -070051 vector<uint8_t> buffer(numBytes);
52 sp<DropBoxManager> dropbox = new DropBoxManager();
yro00698da2017-09-15 10:06:40 -070053 mLogReport.SerializeToArray(&buffer[0], numBytes);
Yao Chenef99c4f2017-09-22 16:26:54 -070054 Status status = dropbox->addData(String16(mTag.c_str()), &buffer[0], numBytes, 0 /* no flag */);
Yao Chenab273e22017-09-06 12:53:50 -070055 if (!status.isOk()) {
56 ALOGE("failed to write to dropbox");
Yao Chenef99c4f2017-09-22 16:26:54 -070057 // TODO: What to do if flush fails??
Yao Chenab273e22017-09-06 12:53:50 -070058 }
yro00698da2017-09-15 10:06:40 -070059 mLogReport.Clear();
Yao Chenab273e22017-09-06 12:53:50 -070060 mBufferSize = 0;
61}
Bookatz906a35c2017-09-20 15:26:44 -070062
Yao Chenef99c4f2017-09-22 16:26:54 -070063} // namespace statsd
64} // namespace os
65} // namespace android