StatsLogProcessor.cpp revision c4dfae56c10a1dd571baa78c750f2e68c919d74f
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 "Log.h"
18
19#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
24#include <log/log_event_list.h>
25#include <utils/Errors.h>
26
27using namespace android;
28using std::make_unique;
29using std::unique_ptr;
30using std::vector;
31
32namespace android {
33namespace os {
34namespace statsd {
35
36StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap)
37    : m_dropbox_writer("all-logs"), mUidMap(uidMap) {
38}
39
40StatsLogProcessor::~StatsLogProcessor() {
41}
42
43// TODO: what if statsd service restarts? How do we know what logs are already processed before?
44void StatsLogProcessor::OnLogEvent(const LogEvent& msg) {
45    // TODO: Use EventMetric to filter the events we want to log.
46    /* TODO: Convert this when we have the generic protobuf writing library in.
47    EventMetricData eventMetricData = parse(msg);
48    m_dropbox_writer.addEventMetricData(eventMetricData);
49    */
50
51    // pass the event to metrics managers.
52    for (auto& pair : mMetricsManagers) {
53        pair.second->onLogEvent(msg);
54    }
55}
56
57void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) {
58    auto it = mMetricsManagers.find(key);
59    if (it != mMetricsManagers.end()) {
60        it->second->finish();
61    }
62
63    ALOGD("Updated configuration for key %s", key.ToString().c_str());
64
65    unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config);
66    if (newMetricsManager->isConfigValid()) {
67        mMetricsManagers[key] = std::move(newMetricsManager);
68        // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)});
69        ALOGD("StatsdConfig valid");
70    } else {
71        // If there is any error in the config, don't use it.
72        ALOGD("StatsdConfig NOT valid");
73    }
74}
75
76void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) {
77    auto it = mMetricsManagers.find(key);
78    if (it != mMetricsManagers.end()) {
79        it->second->finish();
80        mMetricsManagers.erase(it);
81    }
82}
83
84}  // namespace statsd
85}  // namespace os
86}  // namespace android
87