Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 18 | |
| 19 | #include "DropboxWriter.h" |
| 20 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 21 | using android::String16; |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 22 | using android::binder::Status; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 23 | using android::os::DropBoxManager; |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 24 | using android::sp; |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 25 | using std::vector; |
| 26 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace os { |
| 29 | namespace statsd { |
| 30 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 31 | DropboxWriter::DropboxWriter(const string& tag) : mTag(tag), mLogReport(), mBufferSize(0) { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 32 | } |
| 33 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 34 | void DropboxWriter::addStatsLogReport(const StatsLogReport& log) { |
| 35 | mLogReport = log; |
| 36 | flushIfNecessary(log); |
| 37 | mBufferSize += log.ByteSize(); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 38 | } |
| 39 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 40 | void 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 Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void DropboxWriter::flush() { |
| 49 | // now we get an exact byte size of the output |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 50 | const int numBytes = mLogReport.ByteSize(); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 51 | vector<uint8_t> buffer(numBytes); |
| 52 | sp<DropBoxManager> dropbox = new DropBoxManager(); |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 53 | mLogReport.SerializeToArray(&buffer[0], numBytes); |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 54 | Status status = dropbox->addData(String16(mTag.c_str()), &buffer[0], numBytes, 0 /* no flag */); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 55 | if (!status.isOk()) { |
| 56 | ALOGE("failed to write to dropbox"); |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 57 | // TODO: What to do if flush fails?? |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 58 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 59 | mLogReport.Clear(); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 60 | mBufferSize = 0; |
| 61 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 62 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 63 | } // namespace statsd |
| 64 | } // namespace os |
| 65 | } // namespace android |