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 | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 21 | using android::binder::Status; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame^] | 22 | using android::os::DropBoxManager; |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 23 | using android::sp; |
| 24 | using android::String16; |
| 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 | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 31 | DropboxWriter::DropboxWriter(const string& tag) |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 32 | : mTag(tag), mLogReport(), mBufferSize(0) { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 33 | } |
| 34 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 35 | void DropboxWriter::addStatsLogReport(const StatsLogReport& log) { |
| 36 | mLogReport = log; |
| 37 | flushIfNecessary(log); |
| 38 | mBufferSize += log.ByteSize(); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 39 | } |
| 40 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 41 | void 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 Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void DropboxWriter::flush() { |
| 50 | // now we get an exact byte size of the output |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 51 | const int numBytes = mLogReport.ByteSize(); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 52 | vector<uint8_t> buffer(numBytes); |
| 53 | sp<DropBoxManager> dropbox = new DropBoxManager(); |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 54 | mLogReport.SerializeToArray(&buffer[0], numBytes); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 55 | 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 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 61 | mLogReport.Clear(); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 62 | mBufferSize = 0; |
| 63 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame^] | 64 | |
| 65 | } // namespace statsd |
| 66 | } // namespace os |
| 67 | } // namespace android |