1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "sub_event_parser.h"
16 #include "logging.h"
17
18 FTRACE_NS_BEGIN
SubEventParser()19 SubEventParser::SubEventParser()
20 {
21 HILOG_INFO(LOG_CORE, "SubEventParser create!");
22 }
23
~SubEventParser()24 SubEventParser::~SubEventParser()
25 {
26 HILOG_INFO(LOG_CORE, "SubEventParser destroy!");
27 }
28
IsSupport(const std::string & eventName) const29 bool SubEventParser::IsSupport(const std::string& eventName) const
30 {
31 return (nameToFunctions_.find(eventName) != nameToFunctions_.end());
32 }
33
SetupEvent(const EventFormat & format)34 bool SubEventParser::SetupEvent(const EventFormat& format)
35 {
36 auto it = nameToFunctions_.find(format.eventName);
37 if (it == nameToFunctions_.end()) {
38 HILOG_INFO(LOG_CORE, "SetupEvent: event(%s) is not supported", format.eventName.c_str());
39 return false;
40 }
41
42 it->second.format = format;
43 if (format.eventName == "sched_switch") {
44 schedSwitchCtx = &it->second;
45 schedSwitchEventID = format.eventId;
46 } else if (format.eventName == "sched_waking") {
47 schedWakingCtx = &it->second;
48 schedWakingEventID = format.eventId;
49 } else if (format.eventName == "sched_wakup") {
50 schedWakupCtx = &it->second;
51 schedWakupEventID = format.eventId;
52 } else {
53 idToFunctions_[format.eventId] = &it->second;
54 }
55 return true;
56 }
57
RegisterParseFunction(const std::string & name,ParseFunction && fun)58 void SubEventParser::RegisterParseFunction(const std::string& name, ParseFunction&& fun)
59 {
60 CHECK_TRUE(nameToFunctions_.count(name) == 0, NO_RETVAL, "parse function for %s already registered!", name.c_str());
61 nameToFunctions_[name] = {{}, fun};
62 }
63
UnregisterParseFunction(const std::string & name)64 void SubEventParser::UnregisterParseFunction(const std::string& name)
65 {
66 CHECK_TRUE(nameToFunctions_.count(name) > 0, NO_RETVAL, "parse function for %s not registered!", name.c_str());
67 nameToFunctions_.erase(name);
68 }
69
SubEventParserRegisterar(const std::string & name,SubEventParser::ParseFunction && func)70 SubEventParserRegisterar::SubEventParserRegisterar(const std::string& name, SubEventParser::ParseFunction&& func)
71 : name_(name)
72 {
73 SubEventParser::GetInstance().RegisterParseFunction(name, std::move(func));
74 }
75
~SubEventParserRegisterar()76 SubEventParserRegisterar::~SubEventParserRegisterar()
77 {
78 SubEventParser::GetInstance().UnregisterParseFunction(name_);
79 }
80 FTRACE_NS_END
81