blob: f877ef30432c0362f0cd47b9688052c32fafa049 [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 <StatsLogProcessor.h>
18
Yao Chen44cf27c2017-09-14 22:32:50 -070019#include <cutils/log.h>
20#include <frameworks/base/cmds/statsd/src/stats_log.pb.h>
yro00698da2017-09-15 10:06:40 -070021#include <log/log_event_list.h>
Yao Chen44cf27c2017-09-14 22:32:50 -070022#include <metrics/CountMetricProducer.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070023#include <utils/Errors.h>
Yao Chenab273e22017-09-06 12:53:50 -070024
25using namespace android;
Yao Chen44cf27c2017-09-14 22:32:50 -070026using std::make_unique;
27using std::unique_ptr;
28using std::vector;
Bookatz906a35c2017-09-20 15:26:44 -070029
30namespace android {
31namespace os {
32namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070033
David Chende701692017-10-05 13:16:02 -070034StatsLogProcessor::StatsLogProcessor(const sp<UidMap> &uidMap)
35 : m_dropbox_writer("all-logs"), m_UidMap(uidMap)
36{
Yao Chen44cf27c2017-09-14 22:32:50 -070037 // hardcoded config
38 // this should be called from StatsService when it receives a statsd_config
39 UpdateConfig(0, buildFakeConfig());
Yao Chenab273e22017-09-06 12:53:50 -070040}
41
Yao Chenef99c4f2017-09-22 16:26:54 -070042StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -070043}
44
Yao Chen44cf27c2017-09-14 22:32:50 -070045// TODO: what if statsd service restarts? How do we know what logs are already processed before?
Yao Chenef99c4f2017-09-22 16:26:54 -070046void StatsLogProcessor::OnLogEvent(const log_msg& msg) {
Yao Chen44cf27c2017-09-14 22:32:50 -070047 // TODO: Use EventMetric to filter the events we want to log.
48 EventMetricData eventMetricData = parse(msg);
49 m_dropbox_writer.addEventMetricData(eventMetricData);
Yao Chenab273e22017-09-06 12:53:50 -070050
Yao Chen44cf27c2017-09-14 22:32:50 -070051 // pass the event to metrics managers.
52 for (auto& pair : mMetricsManagers) {
53 pair.second->onLogEvent(msg);
Yao Chenab273e22017-09-06 12:53:50 -070054 }
55}
56
Yao Chen44cf27c2017-09-14 22:32:50 -070057void StatsLogProcessor::UpdateConfig(const int config_source, const StatsdConfig& config) {
58 auto it = mMetricsManagers.find(config_source);
59 if (it != mMetricsManagers.end()) {
60 it->second->finish();
61 }
62
David Chen0656b7a2017-09-13 15:53:39 -070063 ALOGD("Updated configuration for source %i", config_source);
Yao Chen44cf27c2017-09-14 22:32:50 -070064
Yao Chencaf339d2017-10-06 16:01:10 -070065 unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config);
66 if (newMetricsManager->isConfigValid()) {
67 mMetricsManagers.insert({config_source, std::move(newMetricsManager)});
68 ALOGD("StatsdConfig valid");
69 } else {
70 // If there is any error in the config, don't use it.
71 ALOGD("StatsdConfig NOT valid");
72 }
yro00698da2017-09-15 10:06:40 -070073}
Bookatz906a35c2017-09-20 15:26:44 -070074
Yao Chenef99c4f2017-09-22 16:26:54 -070075} // namespace statsd
76} // namespace os
77} // namespace android