1 /*
2 * Copyright (c) 2022 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
16 #ifndef EVENT_LOG_HELPER_H
17 #define EVENT_LOG_HELPER_H
18
19 #include <memory>
20
21 #include "define_multimodal.h"
22 #include "input_event.h"
23 #include "key_event.h"
24 #include "mmi_log.h"
25 #include "pointer_event.h"
26
27 namespace OHOS {
28 namespace MMI {
29 class EventLogHelper final {
30 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "EventLogHelper" };
31
32 public:
33 template <class T> static void PrintEventData(std::shared_ptr<T> event, int32_t actionType, int32_t itemNum);
34 template <class T> static void PrintEventData(std::shared_ptr<T> event);
35
36 private:
PrintInfoLog(const std::shared_ptr<KeyEvent> event)37 static void PrintInfoLog(const std::shared_ptr<KeyEvent> event)
38 {
39 std::vector<KeyEvent::KeyItem> eventItems{ event->GetKeyItems() };
40 std::string isSimulate = event->HasFlag(InputEvent::EVENT_FLAG_SIMULATE) ? "true" : "false";
41 MMI_HILOGI("InputTracking id:%{public}d, KeyCode:%{public}d,ActionTime:%{public}" PRId64
42 ",EventType:%{public}s,KeyAction:%{public}s,NumLock:%{public}d,CapsLock:%{public}d,"
43 "ScrollLock:%{public}d,keyItemsCount:%{public}zu,DisplayId:%{public}d,IsSimulate:%{public}s",
44 event->GetId(), event->GetKeyCode(), event->GetActionTime(),
45 InputEvent::EventTypeToString(event->GetEventType()),
46 KeyEvent::ActionToString(event->GetKeyAction()), event->GetFunctionKey(KeyEvent::NUM_LOCK_FUNCTION_KEY),
47 event->GetFunctionKey(KeyEvent::CAPS_LOCK_FUNCTION_KEY),
48 event->GetFunctionKey(KeyEvent::SCROLL_LOCK_FUNCTION_KEY), eventItems.size(),
49 event->GetTargetDisplayId(), isSimulate.c_str());
50 for (const auto &item : eventItems) {
51 MMI_HILOGI("DeviceNumber:%{public}d,KeyCode:%{public}d,DownTime:%{public}" PRId64 ",IsPressed:%{public}d,",
52 item.GetDeviceId(), item.GetKeyCode(), item.GetDownTime(), item.IsPressed());
53 }
54 std::vector<int32_t> pressedKeys = event->GetPressedKeys();
55 std::vector<int32_t>::const_iterator cItr = pressedKeys.cbegin();
56 if (cItr != pressedKeys.cend()) {
57 std::string tmpStr = "Pressed keyCode: [" + std::to_string(*(cItr++));
58 for (; cItr != pressedKeys.cend(); ++cItr) {
59 tmpStr += ("," + std::to_string(*cItr));
60 }
61 MMI_HILOGI("%{public}s]", tmpStr.c_str());
62 }
63 }
64
Print(const std::shared_ptr<KeyEvent> event)65 static void Print(const std::shared_ptr<KeyEvent> event)
66 {
67 if (!HiLogIsLoggable(OHOS::MMI::MMI_LOG_DOMAIN, LABEL.tag, LOG_DEBUG) &&
68 event->GetKeyCode() != KeyEvent::KEYCODE_POWER) {
69 return;
70 }
71 std::vector<KeyEvent::KeyItem> eventItems{ event->GetKeyItems() };
72 MMI_HILOGD("KeyCode:%{public}d,KeyIntention:%{public}d,ActionTime:%{public}" PRId64
73 ",ActionStartTime:%{public}" PRId64
74 ",EventType:%{public}s,Flag:%{public}d,KeyAction:%{public}s,NumLock:%{public}d,"
75 "CapsLock:%{public}d,ScrollLock:%{public}d,EventNumber:%{public}d,keyItemsCount:%{public}zu",
76 event->GetKeyCode(), event->GetKeyIntention(), event->GetActionTime(), event->GetActionStartTime(),
77 InputEvent::EventTypeToString(event->GetEventType()), event->GetFlag(),
78 KeyEvent::ActionToString(event->GetKeyAction()), event->GetFunctionKey(KeyEvent::NUM_LOCK_FUNCTION_KEY),
79 event->GetFunctionKey(KeyEvent::CAPS_LOCK_FUNCTION_KEY),
80 event->GetFunctionKey(KeyEvent::SCROLL_LOCK_FUNCTION_KEY),
81 event->GetId(), eventItems.size());
82 for (const auto &item : eventItems) {
83 MMI_HILOGI("DeviceNumber:%{public}d,KeyCode:%{public}d,DownTime:%{public}" PRId64 ",IsPressed:%{public}d,"
84 "GetUnicode:%{public}d", item.GetDeviceId(), item.GetKeyCode(), item.GetDownTime(), item.IsPressed(),
85 item.GetUnicode());
86 }
87 std::vector<int32_t> pressedKeys = event->GetPressedKeys();
88 std::vector<int32_t>::const_iterator cItr = pressedKeys.cbegin();
89 if (cItr != pressedKeys.cend()) {
90 std::string tmpStr = "Pressed keyCode: [" + std::to_string(*(cItr++));
91 for (; cItr != pressedKeys.cend(); ++cItr) {
92 tmpStr += ("," + std::to_string(*cItr));
93 }
94 MMI_HILOGI("%{public}s]", tmpStr.c_str());
95 }
96 }
97
98 __attribute__((no_sanitize("cfi")))
PrintInfoLog(const std::shared_ptr<PointerEvent> event)99 static void PrintInfoLog(const std::shared_ptr<PointerEvent> event)
100 {
101 if (event->GetPointerAction() == PointerEvent::POINTER_ACTION_MOVE ||
102 event->GetPointerAction() == PointerEvent::POINTER_ACTION_PULL_MOVE) {
103 return;
104 }
105 std::vector<int32_t> pointerIds{ event->GetPointerIds() };
106 std::string isSimulate = event->HasFlag(InputEvent::EVENT_FLAG_SIMULATE) ? "true" : "false";
107 MMI_HILOGI("InputTracking id:%{public}d, EventType:%{public}s,ActionTime:%{public}" PRId64
108 ",PointerAction:%{public}s, SourceType:%{public}s, DisplayId:%{public}d, IsSimulate:%{public}s",
109 event->GetId(), InputEvent::EventTypeToString(event->GetEventType()), event->GetActionTime(),
110 event->DumpPointerAction(), event->DumpSourceType(), event->GetTargetDisplayId(), isSimulate.c_str());
111 for (const auto &pointerId : pointerIds) {
112 PointerEvent::PointerItem item;
113 if (!event->GetPointerItem(pointerId, item)) {
114 MMI_HILOGE("Invalid pointer: %{public}d.", pointerId);
115 return;
116 }
117 MMI_HILOGI("pointerId:%{public}d,DownTime:%{public}" PRId64 ",IsPressed:%{public}d,DisplayX:%{public}d,"
118 "DisplayY:%{public}d,Pressure:%{public}.2f,LongAxis:%{public}d,ShortAxis:%{public}d",
119 pointerId, item.GetDownTime(), item.IsPressed(), item.GetDisplayX(), item.GetDisplayY(),
120 item.GetPressure(), item.GetLongAxis(), item.GetShortAxis());
121 }
122 std::vector<int32_t> pressedKeys = event->GetPressedKeys();
123 std::vector<int32_t>::const_iterator cItr = pressedKeys.cbegin();
124 if (cItr != pressedKeys.cend()) {
125 std::string tmpStr = "Pressed keyCode: [" + std::to_string(*(cItr++));
126 for (; cItr != pressedKeys.cend(); ++cItr) {
127 tmpStr += ("," + std::to_string(*cItr));
128 }
129 MMI_HILOGI("%{public}s]", tmpStr.c_str());
130 }
131 }
132
Print(const std::shared_ptr<PointerEvent> event)133 static void Print(const std::shared_ptr<PointerEvent> event)
134 {
135 std::vector<int32_t> pointerIds{ event->GetPointerIds() };
136 std::string str;
137 std::vector<uint8_t> buffer = event->GetBuffer();
138 for (const auto &buff : buffer) {
139 str += std::to_string(buff);
140 }
141 MMI_HILOGD("EventType:%{public}s,ActionTime:%{public}" PRId64 ",SensorInputTime:%{public}" PRIu64
142 ",Action:%{public}d,ActionStartTime:%{public}" PRId64 ",Flag:%{public}d,PointerAction:%{public}s,"
143 "SourceType:%{public}s,ButtonId:%{public}d,VerticalAxisValue:%{public}.2f,"
144 "HorizontalAxisValue:%{public}.2f,PinchAxisValue:%{public}.2f,"
145 "XAbsValue:%{public}.2f,YAbsValue:%{public}.2f,ZAbsValue:%{public}.2f,"
146 "RzAbsValue:%{public}.2f,GasAbsValue:%{public}.2f,BrakeAbsValue:%{public}.2f,"
147 "Hat0xAbsValue:%{public}.2f,Hat0yAbsValue:%{public}.2f,ThrottleAbsValue:%{public}.2f,"
148 "PointerId:%{public}d,PointerCount:%{public}zu,EventNumber:%{public}d,"
149 "BufferCount:%{public}zu,Buffer:%{public}s",
150 InputEvent::EventTypeToString(event->GetEventType()), event->GetActionTime(), event->GetSensorInputTime(),
151 event->GetAction(), event->GetActionStartTime(), event->GetFlag(),
152 event->DumpPointerAction(), event->DumpSourceType(),
153 event->GetButtonId(), event->GetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL),
154 event->GetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL),
155 event->GetAxisValue(PointerEvent::AXIS_TYPE_PINCH), event->GetAxisValue(PointerEvent::AXIS_TYPE_ABS_X),
156 event->GetAxisValue(PointerEvent::AXIS_TYPE_ABS_Y), event->GetAxisValue(PointerEvent::AXIS_TYPE_ABS_Z),
157 event->GetAxisValue(PointerEvent::AXIS_TYPE_ABS_RZ), event->GetAxisValue(PointerEvent::AXIS_TYPE_ABS_GAS),
158 event->GetAxisValue(PointerEvent::AXIS_TYPE_ABS_BRAKE),
159 event->GetAxisValue(PointerEvent::AXIS_TYPE_ABS_HAT0X),
160 event->GetAxisValue(PointerEvent::AXIS_TYPE_ABS_HAT0Y),
161 event->GetAxisValue(PointerEvent::AXIS_TYPE_ABS_THROTTLE), event->GetPointerId(), pointerIds.size(),
162 event->GetId(), buffer.size(), str.c_str());
163
164 for (const auto &pointerId : pointerIds) {
165 PointerEvent::PointerItem item;
166 if (!event->GetPointerItem(pointerId, item)) {
167 MMI_HILOGE("Invalid pointer: %{public}d.", pointerId);
168 return;
169 }
170 MMI_HILOGD("pointerId:%{public}d,DownTime:%{public}" PRId64 ",IsPressed:%{public}d,DisplayX:%{public}d,"
171 "DisplayY:%{public}d,WindowX:%{public}d,WindowY:%{public}d,Width:%{public}d,Height:%{public}d,"
172 "TiltX:%{public}.2f,TiltY:%{public}.2f,ToolDisplayX:%{public}d,ToolDisplayY:%{public}d,"
173 "ToolWindowX:%{public}d,ToolWindowY:%{public}d,ToolWidth:%{public}d,ToolHeight:%{public}d,"
174 "Pressure:%{public}.2f,ToolType:%{public}d,LongAxis:%{public}d,ShortAxis:%{public}d,RawDx:%{public}d,"
175 "RawDy:%{public}d",
176 pointerId, item.GetDownTime(), item.IsPressed(), item.GetDisplayX(), item.GetDisplayY(),
177 item.GetWindowX(), item.GetWindowY(), item.GetWidth(), item.GetHeight(), item.GetTiltX(),
178 item.GetTiltY(), item.GetToolDisplayX(), item.GetToolDisplayY(), item.GetToolWindowX(),
179 item.GetToolWindowY(), item.GetToolWidth(), item.GetToolHeight(), item.GetPressure(),
180 item.GetToolType(), item.GetLongAxis(), item.GetShortAxis(), item.GetRawDx(), item.GetRawDy());
181 }
182 std::vector<int32_t> pressedKeys = event->GetPressedKeys();
183 std::vector<int32_t>::const_iterator cItr = pressedKeys.cbegin();
184 if (cItr != pressedKeys.cend()) {
185 std::string tmpStr = "Pressed keyCode: [" + std::to_string(*(cItr++));
186 for (; cItr != pressedKeys.cend(); ++cItr) {
187 tmpStr += ("," + std::to_string(*cItr));
188 }
189 MMI_HILOGD("%{public}s]", tmpStr.c_str());
190 }
191 }
192 };
193
PrintEventData(std::shared_ptr<T> event,int32_t actionType,int32_t itemNum)194 template <class T> void EventLogHelper::PrintEventData(std::shared_ptr<T> event, int32_t actionType, int32_t itemNum)
195 {
196 CHKPV(event);
197 PrintInfoLog(event);
198 if (HiLogIsLoggable(OHOS::MMI::MMI_LOG_DOMAIN, LABEL.tag, LOG_DEBUG)) {
199 static int64_t nowTimeUSec = 0;
200 static int32_t dropped = 0;
201 if (event->GetAction() == EVENT_TYPE_POINTER) {
202 if ((actionType == POINTER_ACTION_MOVE) && (event->GetActionTime() - nowTimeUSec <= TIMEOUT)) {
203 ++dropped;
204 return;
205 }
206 if (actionType == POINTER_ACTION_UP && itemNum == FINAL_FINGER) {
207 MMI_HILOGD("This touch process discards %{public}d high frequent events", dropped);
208 dropped = 0;
209 }
210 nowTimeUSec = event->GetActionTime();
211 }
212 EventLogHelper::Print(event);
213 }
214 }
215
PrintEventData(std::shared_ptr<T> event)216 template <class T> void EventLogHelper::PrintEventData(std::shared_ptr<T> event)
217 {
218 CHKPV(event);
219 PrintInfoLog(event);
220 if (HiLogIsLoggable(OHOS::MMI::MMI_LOG_DOMAIN, LABEL.tag, LOG_DEBUG) ||
221 (event->GetAction() == InputEvent::EVENT_TYPE_KEY)) {
222 EventLogHelper::Print(event);
223 }
224 }
225 } // namespace MMI
226 } // namespace OHOS
227 #endif // EVENT_LOG_HELPER_H
228