blob: 307e7712e5aaee557d4192506896900494813bc9 [file] [log] [blame]
Yao Chen482d2722017-09-12 13:25:43 -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#include <android/os/DropBoxManager.h>
17#include <android-base/file.h>
Yao Chen482d2722017-09-12 13:25:43 -070018#include <androidfw/ZipUtils.h>
Yao Chen482d2722017-09-12 13:25:43 -070019
20#include "DropboxReader.h"
21
22using android::sp;
23using android::String16;
24using android::binder::Status;
25using android::base::unique_fd;
26using android::os::DropBoxManager;
Yao Chen482d2722017-09-12 13:25:43 -070027using android::ZipUtils;
28using std::vector;
29
Bookatz906a35c2017-09-20 15:26:44 -070030namespace android {
31namespace os {
32namespace statsd {
33
Yao Chen482d2722017-09-12 13:25:43 -070034status_t DropboxReader::readStatsLogs(FILE* out, const string& tag, long msec) {
35 sp<DropBoxManager> dropbox = new DropBoxManager();
yro00698da2017-09-15 10:06:40 -070036 StatsLogReport logReport;
Yao Chen482d2722017-09-12 13:25:43 -070037
38 long timestamp = msec;
39 // instead of while(true), put a hard limit 1000. Dropbox won't have more than 1000 files.
40 for(int i = 0; i < 1000; i++ ) {
41 DropBoxManager::Entry entry;
42 Status status = dropbox->getNextEntry(String16(tag.c_str()),
43 timestamp, &entry);
44 if (!status.isOk()) {
45 ALOGD("No more entries, or failed to read. We can't tell unfortunately.");
46 return android::OK;
47 }
48
49 const unique_fd& fd = entry.getFd();
50
51 // use this timestamp for next query.
52 timestamp = entry.getTimestamp();
53
54 if (entry.getFlags() & DropBoxManager::IS_GZIPPED) {
yro00698da2017-09-15 10:06:40 -070055 if (!parseFromGzipFile(fd, logReport)) {
Yao Chen482d2722017-09-12 13:25:43 -070056 // Failed to parse from the file. Continue to fetch the next entry.
57 continue;
58 }
59 } else {
yro00698da2017-09-15 10:06:40 -070060 if (!parseFromFile(fd, logReport)) {
Yao Chen482d2722017-09-12 13:25:43 -070061 // Failed to parse from the file. Continue to fetch the next entry.
62 continue;
63 }
64 }
65
yro00698da2017-09-15 10:06:40 -070066 printLog(out, logReport);
Yao Chen482d2722017-09-12 13:25:43 -070067 }
68 return android::OK;
69}
70
yro00698da2017-09-15 10:06:40 -070071bool DropboxReader::parseFromGzipFile(const unique_fd& fd, StatsLogReport& logReport) {
Yao Chen482d2722017-09-12 13:25:43 -070072 FILE *file = fdopen(fd, "r");
73 bool result = false;
74 bool scanResult;
75 int method;
76 long compressedLen;
77 long uncompressedLen;
78 unsigned long crc32;
79 scanResult = ZipUtils::examineGzip(file, &method, &uncompressedLen,
80 &compressedLen, &crc32);
81 if (scanResult && method == kCompressDeflated) {
82 vector<uint8_t> buf(uncompressedLen);
83 if (ZipUtils::inflateToBuffer(file, &buf[0], uncompressedLen, compressedLen)) {
yro00698da2017-09-15 10:06:40 -070084 if (logReport.ParseFromArray(&buf[0], uncompressedLen)) {
Yao Chen482d2722017-09-12 13:25:43 -070085 result = true;
86 }
87 }
88 } else {
89 ALOGE("This isn't a valid deflated gzip file");
90 }
91 fclose(file);
92 return result;
93}
94
95// parse a non zipped file.
yro00698da2017-09-15 10:06:40 -070096bool DropboxReader::parseFromFile(const unique_fd& fd, StatsLogReport& logReport) {
Yao Chen482d2722017-09-12 13:25:43 -070097 string content;
98 if (!android::base::ReadFdToString(fd, &content)) {
99 ALOGE("Failed to read file");
100 return false;
101 }
yro00698da2017-09-15 10:06:40 -0700102 if (!logReport.ParseFromString(content)) {
Yao Chen482d2722017-09-12 13:25:43 -0700103 ALOGE("failed to parse log entry from data");
104 return false;
105 }
106 return true;
107}
108
yro00698da2017-09-15 10:06:40 -0700109void DropboxReader::printLog(FILE* out, const StatsLogReport& logReport) {
110 fprintf(out, "start_time_msec=%lld, end_time_msec=%lld, ",
111 logReport.start_report_millis(), logReport.end_report_millis());
112 for (int i = 0; i < logReport.event_metrics().data_size(); i++) {
113 EventMetricData eventMetricData = logReport.event_metrics().data(i);
114 for (int j = 0; j < eventMetricData.key_value_pair_size(); j++) {
115 fprintf(out, "key=%d, ", eventMetricData.key_value_pair(j).key());
116 fprintf(out, "value_str=%s ", eventMetricData.key_value_pair(j).value_str().c_str());
117 fprintf(out, "value_int=%lld ", eventMetricData.key_value_pair(j).value_int());
118 fprintf(out, "value_float=%f ", eventMetricData.key_value_pair(j).value_float());
Yao Chen482d2722017-09-12 13:25:43 -0700119 }
Yao Chen482d2722017-09-12 13:25:43 -0700120 }
yro00698da2017-09-15 10:06:40 -0700121 fprintf(out, "\n");
Yao Chen482d2722017-09-12 13:25:43 -0700122}
Bookatz906a35c2017-09-20 15:26:44 -0700123
124} // namespace statsd
125} // namespace os
126} // namespace android