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