• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
GetInstance()19 SubEventParser& SubEventParser::GetInstance()
20 {
21     static SubEventParser instance;
22     return instance;
23 }
24 
SubEventParser()25 SubEventParser::SubEventParser()
26 {
27     HILOG_INFO(LOG_CORE, "SubEventParser create!");
28 }
29 
~SubEventParser()30 SubEventParser::~SubEventParser()
31 {
32     HILOG_INFO(LOG_CORE, "SubEventParser destroy!");
33 }
34 
IsSupport(uint32_t eventId) const35 bool SubEventParser::IsSupport(uint32_t eventId) const
36 {
37     return idToFunctions_.count(eventId);
38 }
39 
IsSupport(const std::string & eventName) const40 bool SubEventParser::IsSupport(const std::string& eventName) const
41 {
42     return nameToFunctions_.count(eventName) > 0;
43 }
44 
ParseEvent(FtraceEvent & event,uint8_t data[],size_t size,const EventFormat & format) const45 bool SubEventParser::ParseEvent(FtraceEvent& event, uint8_t data[], size_t size, const EventFormat& format) const
46 {
47     auto iter = idToFunctions_.find(format.eventId);
48     if (iter != idToFunctions_.end()) {
49         iter->second(event, data, size, format);
50         return true;
51     }
52     return false;
53 }
54 
SetupEvent(const EventFormat & format)55 bool SubEventParser::SetupEvent(const EventFormat& format)
56 {
57     auto it = nameToFunctions_.find(format.eventName);
58     if (it != nameToFunctions_.end()) {
59         idToFunctions_[format.eventId] = it->second;
60         return true;
61     }
62     return false;
63 }
64 
RegisterParseFunction(const std::string & name,ParseFunction && fun)65 void SubEventParser::RegisterParseFunction(const std::string& name, ParseFunction&& fun)
66 {
67     CHECK_TRUE(nameToFunctions_.count(name) == 0, NO_RETVAL, "parse function for %s already registered!", name.c_str());
68     nameToFunctions_[name] = fun;
69 }
70 
UnregisterParseFunction(const std::string & name)71 void SubEventParser::UnregisterParseFunction(const std::string& name)
72 {
73     CHECK_TRUE(nameToFunctions_.count(name) > 0, NO_RETVAL, "parse function for %s not registered!", name.c_str());
74     nameToFunctions_.erase(name);
75 }
76 
SubEventParserRegisterar(const std::string & name,SubEventParser::ParseFunction && func)77 SubEventParserRegisterar::SubEventParserRegisterar(const std::string& name, SubEventParser::ParseFunction&& func)
78     : name_(name)
79 {
80     SubEventParser::GetInstance().RegisterParseFunction(name, std::move(func));
81 }
82 
~SubEventParserRegisterar()83 SubEventParserRegisterar::~SubEventParserRegisterar()
84 {
85     SubEventParser::GetInstance().UnregisterParseFunction(name_);
86 }
87 FTRACE_NS_END
88