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 #define DEBUG false  // STOPSHIP if true
18 #include "Log.h"
19 
20 #include "SimpleLogMatchingTracker.h"
21 
22 namespace android {
23 namespace os {
24 namespace statsd {
25 
26 using std::unordered_map;
27 using std::vector;
28 
29 
SimpleLogMatchingTracker(const int64_t & id,const int index,const SimpleAtomMatcher & matcher,const UidMap & uidMap)30 SimpleLogMatchingTracker::SimpleLogMatchingTracker(const int64_t& id, const int index,
31                                                    const SimpleAtomMatcher& matcher,
32                                                    const UidMap& uidMap)
33     : LogMatchingTracker(id, index), mMatcher(matcher), mUidMap(uidMap) {
34     if (!matcher.has_atom_id()) {
35         mInitialized = false;
36     } else {
37         mAtomIds.insert(matcher.atom_id());
38         mInitialized = true;
39     }
40 }
41 
~SimpleLogMatchingTracker()42 SimpleLogMatchingTracker::~SimpleLogMatchingTracker() {
43 }
44 
init(const vector<AtomMatcher> & allLogMatchers,const vector<sp<LogMatchingTracker>> & allTrackers,const unordered_map<int64_t,int> & matcherMap,vector<bool> & stack)45 bool SimpleLogMatchingTracker::init(const vector<AtomMatcher>& allLogMatchers,
46                                     const vector<sp<LogMatchingTracker>>& allTrackers,
47                                     const unordered_map<int64_t, int>& matcherMap,
48                                     vector<bool>& stack) {
49     // no need to do anything.
50     return mInitialized;
51 }
52 
onLogEvent(const LogEvent & event,const vector<sp<LogMatchingTracker>> & allTrackers,vector<MatchingState> & matcherResults)53 void SimpleLogMatchingTracker::onLogEvent(const LogEvent& event,
54                                           const vector<sp<LogMatchingTracker>>& allTrackers,
55                                           vector<MatchingState>& matcherResults) {
56     if (matcherResults[mIndex] != MatchingState::kNotComputed) {
57         VLOG("Matcher %lld already evaluated ", (long long)mId);
58         return;
59     }
60 
61     if (mAtomIds.find(event.GetTagId()) == mAtomIds.end()) {
62         matcherResults[mIndex] = MatchingState::kNotMatched;
63         return;
64     }
65 
66     bool matched = matchesSimple(mUidMap, mMatcher, event);
67     matcherResults[mIndex] = matched ? MatchingState::kMatched : MatchingState::kNotMatched;
68     VLOG("Stats SimpleLogMatcher %lld matched? %d", (long long)mId, matched);
69 }
70 
71 }  // namespace statsd
72 }  // namespace os
73 }  // namespace android
74