blob: 1308ca1fb4ebf7ac9c70ac65295623930de95731 [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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017#include "Log.h"
Yao Chenab273e22017-09-06 12:53:50 -070018
Joe Onorato9fc9edf2017-10-15 20:08:52 -070019#include "StatsLogProcessor.h"
20#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
21#include "metrics/CountMetricProducer.h"
22#include "stats_util.h"
23
yro00698da2017-09-15 10:06:40 -070024#include <log/log_event_list.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070025#include <utils/Errors.h>
Yao Chenab273e22017-09-06 12:53:50 -070026
27using namespace android;
Yao Chen44cf27c2017-09-14 22:32:50 -070028using std::make_unique;
29using std::unique_ptr;
30using std::vector;
Bookatz906a35c2017-09-20 15:26:44 -070031
32namespace android {
33namespace os {
34namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070035
Joe Onorato9fc9edf2017-10-15 20:08:52 -070036StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap)
37 : m_dropbox_writer("all-logs"), mUidMap(uidMap) {
Yao Chenab273e22017-09-06 12:53:50 -070038}
39
Yao Chenef99c4f2017-09-22 16:26:54 -070040StatsLogProcessor::~StatsLogProcessor() {
Yao Chenab273e22017-09-06 12:53:50 -070041}
42
Yao Chen44cf27c2017-09-14 22:32:50 -070043// TODO: what if statsd service restarts? How do we know what logs are already processed before?
Yao Chenef99c4f2017-09-22 16:26:54 -070044void StatsLogProcessor::OnLogEvent(const log_msg& msg) {
Yao Chen44cf27c2017-09-14 22:32:50 -070045 // TODO: Use EventMetric to filter the events we want to log.
46 EventMetricData eventMetricData = parse(msg);
47 m_dropbox_writer.addEventMetricData(eventMetricData);
Yao Chenab273e22017-09-06 12:53:50 -070048
Yao Chen44cf27c2017-09-14 22:32:50 -070049 // pass the event to metrics managers.
50 for (auto& pair : mMetricsManagers) {
51 pair.second->onLogEvent(msg);
Yao Chenab273e22017-09-06 12:53:50 -070052 }
53}
54
Joe Onorato9fc9edf2017-10-15 20:08:52 -070055void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) {
56 auto it = mMetricsManagers.find(key);
Yao Chen44cf27c2017-09-14 22:32:50 -070057 if (it != mMetricsManagers.end()) {
58 it->second->finish();
59 }
60
Joe Onorato9fc9edf2017-10-15 20:08:52 -070061 ALOGD("Updated configuration for key %s", key.ToString().c_str());
Yao Chen44cf27c2017-09-14 22:32:50 -070062
Yao Chencaf339d2017-10-06 16:01:10 -070063 unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config);
64 if (newMetricsManager->isConfigValid()) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070065 mMetricsManagers[key] = std::move(newMetricsManager);
66 // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)});
Yao Chencaf339d2017-10-06 16:01:10 -070067 ALOGD("StatsdConfig valid");
68 } else {
69 // If there is any error in the config, don't use it.
70 ALOGD("StatsdConfig NOT valid");
71 }
yro00698da2017-09-15 10:06:40 -070072}
Bookatz906a35c2017-09-20 15:26:44 -070073
Joe Onorato9fc9edf2017-10-15 20:08:52 -070074void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
75 auto it = mMetricsManagers.find(key);
76 if (it != mMetricsManagers.end()) {
77 it->second->finish();
78 mMetricsManagers.erase(it);
79 }
80}
81
Yao Chenef99c4f2017-09-22 16:26:54 -070082} // namespace statsd
83} // namespace os
84} // namespace android