• 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 #ifndef EVENT_FORMATTER_H
16 #define EVENT_FORMATTER_H
17 
18 #include <functional>
19 #include <map>
20 #include "ftrace_namespace.h"
21 #include "trace_plugin_result.pb.h"
22 
23 FTRACE_NS_BEGIN
24 using FormatEventFn = std::function<std::string(const FtraceEvent&)>;
25 using CheckEventFn = std::function<bool(const FtraceEvent&)>;
26 
27 class EventFormatterRegisterar;
28 
29 class EventFormatter {
30 public:
31     static EventFormatter& GetInstance();
32 
33     // std::string FormatResult(const TracePluginResult& result);
34 
35     std::string FormatEvent(const FtraceEvent& ftraceEvent);
36 
37 protected:
38     friend class EventFormatterRegisterar;
39     void RegisterEvent(const std::string& name, const CheckEventFn& check, const FormatEventFn& format);
40     void UnregisterEvent(const std::string& name);
41 
42 private:
43     struct EventRegistry {
44         CheckEventFn checkEvent_;
45         FormatEventFn formatEvent_;
46     };
47     std::map<std::string, EventRegistry> eventRegistries_;
48 };
49 
50 class EventFormatterRegisterar {
51 public:
52     EventFormatterRegisterar(const std::string& name, const CheckEventFn& check, const FormatEventFn& format);
53     ~EventFormatterRegisterar();
54 
55 private:
56     std::string name_;
57 };
58 FTRACE_NS_END
59 
60 #define REGISTER_FTRACE_EVENT_FORMATTER(event, check, format) \
61     static EventFormatterRegisterar ftrace##event##FormatterRegister##__LINE__(#event, check, format);
62 
63 #endif // EVENT_FORMATTER_H