From 63786c5d8043a9c95881e71cd8b2a34fe671bf51 Mon Sep 17 00:00:00 2001 From: tangfan Date: Thu, 11 Apr 2024 15:17:02 +0800 Subject: [PATCH 1/3] short log print Signed-off-by: tangfan --- common/include/input_hub.cpp | 71 +++++++++++++++++++++++++----------- common/include/input_hub.h | 1 + 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index f6190e4..a88c867 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -59,6 +59,7 @@ InputHub::~InputHub() int32_t InputHub::Initialize() { + logCount_.store(1); epollFd_ = epoll_create1(EPOLL_CLOEXEC); if (epollFd_ < 0) { DHLOGE("Could not create epoll instance: %{public}s", ConvertErrNo().c_str()); @@ -539,6 +540,10 @@ int32_t InputHub::OpenInputDeviceLocked(const std::string &devicePath) } std::lock_guard my_lock(operationMutex_); + logCount_++; + if (logCount_ >= INT32_MAX - 1) { + logCount_ = 1; + } DHLOGD("Opening device start: %{public}s", devicePath.c_str()); int fd = OpenInputDeviceFdByPath(devicePath); if (fd == UN_INIT_FD_VALUE) { @@ -559,7 +564,9 @@ int32_t InputHub::OpenInputDeviceLocked(const std::string &devicePath) if (MakeDevice(fd, std::move(device)) < 0) { CloseFd(fd); - DHLOGI("Opening device error: %{public}s", devicePath.c_str()); + if (logCount_ % 100 == 0) { + DHLOGI("Opening device error: %{public}s", devicePath.c_str()); + } return ERR_DH_INPUT_HUB_MAKE_DEVICE_FAIL; } @@ -635,11 +642,15 @@ int32_t InputHub::QueryInputDeviceInfo(int fd, std::unique_ptr &device) void InputHub::QueryEventInfo(int fd, std::unique_ptr &device) { - DHLOGI("QueryEventInfo: devName: %{public}s, dhId: %{public}s!", device->identifier.name.c_str(), - GetAnonyString(device->identifier.descriptor).c_str()); + if (logCount_ % 100 == 0) { + DHLOGI("QueryEventInfo: devName: %{public}s, dhId: %{public}s!", device->identifier.name.c_str(), + GetAnonyString(device->identifier.descriptor).c_str()); + } struct libevdev *dev = GetLibEvDev(fd); if (dev == nullptr) { - DHLOGE("dev is nullptr"); + if (logCount_ % 100 == 0) { + DHLOGE("dev is nullptr"); + } return; } GetEventTypes(dev, device->identifier); @@ -722,7 +733,9 @@ struct libevdev* InputHub::GetLibEvDev(int fd) int rc = 1; rc = libevdev_new_from_fd(fd, &dev); if (rc < 0) { - DHLOGE("Failed to init libevdev (%{public}s)", strerror(-rc)); + if (logCount_ % 100 == 0) { + DHLOGE("Failed to init libevdev (%{public}s)", strerror(-rc)); + } return nullptr; } return dev; @@ -742,7 +755,9 @@ void InputHub::GetEventTypes(struct libevdev *dev, InputDevice &identifier) int32_t InputHub::GetEventKeys(struct libevdev *dev, InputDevice &identifier) { if (!libevdev_has_event_type(dev, EV_KEY)) { - DHLOGE("The device doesn't has EV_KEY type!"); + if (logCount_ % 100 == 0) { + DHLOGE("The device doesn't has EV_KEY type!"); + } return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; } for (uint32_t eventKey = 0; eventKey < KEY_CNT; eventKey++) { @@ -758,7 +773,9 @@ int32_t InputHub::GetEventKeys(struct libevdev *dev, InputDevice &identifier) int32_t InputHub::GetABSInfo(struct libevdev *dev, InputDevice &identifier) { if (!libevdev_has_event_type(dev, EV_ABS)) { - DHLOGE("The device doesn't has EV_ABS type!"); + if (logCount_ % 100 == 0) { + DHLOGE("The device doesn't has EV_ABS type!"); + } return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; } DHLOGI("The device has abs info, devName: %{public}s, dhId: %{public}s!", @@ -787,7 +804,9 @@ int32_t InputHub::GetABSInfo(struct libevdev *dev, InputDevice &identifier) int32_t InputHub::GetRELTypes(struct libevdev *dev, InputDevice &identifier) { if (!libevdev_has_event_type(dev, EV_REL)) { - DHLOGE("The device doesn't has EV_REL type!"); + if (logCount_ % 100 == 0) { + DHLOGE("The device doesn't has EV_REL type!"); + } return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; } for (uint32_t code = 0; code < REL_CNT; code++) { @@ -844,7 +863,9 @@ int32_t InputHub::MakeDevice(int fd, std::unique_ptr device) // If the device isn't recognized as something we handle, don't monitor it. if (device->classes == 0) { - DHLOGI("Dropping device: name='%{public}s'", device->identifier.name.c_str()); + if (logCount_ % 100 == 0) { + DHLOGI("Dropping device: name='%{public}s'", device->identifier.name.c_str()); + } return ERR_DH_INPUT_HUB_MAKE_DEVICE_FAIL; } @@ -953,8 +974,10 @@ void InputHub::GenerateDescriptor(InputDevice &identifier) const } identifier.descriptor = DH_ID_PREFIX + Sha256(rawDescriptor); - DHLOGI("Created descriptor: raw=%{public}s, cooked=%{public}s", rawDescriptor.c_str(), - GetAnonyString(identifier.descriptor).c_str()); + if (logCount_ % 100 == 0) { + DHLOGI("Created descriptor: raw=%{public}s, cooked=%{public}s", rawDescriptor.c_str(), + GetAnonyString(identifier.descriptor).c_str()); + } } int32_t InputHub::RegisterDeviceForEpollLocked(const Device &device) @@ -1322,16 +1345,18 @@ bool InputHub::IsAllDevicesStoped() void InputHub::RecordDeviceLog(const std::string &devicePath, const InputDevice &identifier) { - DHLOGI("add device: %{public}s\n", devicePath.c_str()); - DHLOGI(" bus: %{public}04x\n" - " vendor %{public}04x\n" - " product %{public}04x\n" - " version %{public}04x\n", - identifier.bus, identifier.vendor, identifier.product, identifier.version); - DHLOGI(" name: \"%{public}s\"\n", identifier.name.c_str()); - DHLOGI(" physicalPath: \"%{public}s\"\n", identifier.physicalPath.c_str()); - DHLOGI(" unique id: \"%{public}s\"\n", identifier.uniqueId.c_str()); - DHLOGI(" descriptor: \"%{public}s\"\n", GetAnonyString(identifier.descriptor).c_str()); + if (logCount_ % 100 == 0) { + DHLOGI("add device: %{public}s\n", devicePath.c_str()); + DHLOGI(" bus: %{public}04x\n" + " vendor %{public}04x\n" + " product %{public}04x\n" + " version %{public}04x\n", + identifier.bus, identifier.vendor, identifier.product, identifier.version); + DHLOGI(" name: \"%{public}s\"\n", identifier.name.c_str()); + DHLOGI(" physicalPath: \"%{public}s\"\n", identifier.physicalPath.c_str()); + DHLOGI(" unique id: \"%{public}s\"\n", identifier.uniqueId.c_str()); + DHLOGI(" descriptor: \"%{public}s\"\n", GetAnonyString(identifier.descriptor).c_str()); + } } void InputHub::RecordChangeEventLog(const RawEvent &event) @@ -1599,7 +1624,9 @@ InputHub::Device::Device(int fd, const std::string &path) : next(nullptr), fd(fd), path(path), identifier({}), classes(0), enabled(false), isShare(false), isVirtual(fd < 0) { // Figure out the kinds of events the device reports. - DHLOGI("Ctor Device for get event mask, fd: %{public}d, path: %{public}s", fd, path.c_str()); + if (logCount_ % 100 == 0) { + DHLOGI("Ctor Device for get event mask, fd: %{public}d, path: %{public}s", fd, path.c_str()); + } ioctl(fd, EVIOCGBIT(0, sizeof(evBitmask)), evBitmask); ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keyBitmask)), keyBitmask); ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absBitmask)), absBitmask); diff --git a/common/include/input_hub.h b/common/include/input_hub.h index 6e7f67d..06e0141 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -240,6 +240,7 @@ private: std::atomic isStartCollectEvent_; std::atomic isStartCollectHandler_; std::unordered_map sharedDHIds_; + static std::atomic logCount_; }; } // namespace DistributedInput } // namespace DistributedHardware -- Gitee From cfc5db5372301517925241f7a12fa8a3b3e6d2ed Mon Sep 17 00:00:00 2001 From: tangfan Date: Fri, 12 Apr 2024 16:31:33 +0800 Subject: [PATCH 2/3] short log Signed-off-by: tangfan --- common/include/input_hub.cpp | 1 - common/include/input_hub.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index a88c867..adece5f 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -59,7 +59,6 @@ InputHub::~InputHub() int32_t InputHub::Initialize() { - logCount_.store(1); epollFd_ = epoll_create1(EPOLL_CLOEXEC); if (epollFd_ < 0) { DHLOGE("Could not create epoll instance: %{public}s", ConvertErrNo().c_str()); diff --git a/common/include/input_hub.h b/common/include/input_hub.h index 06e0141..51b3e39 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -33,6 +33,7 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { +static std::atomic logCount_ = {1}; struct AffectDhIds { std::vector sharingDhIds; std::vector noSharingDhIds; @@ -240,7 +241,6 @@ private: std::atomic isStartCollectEvent_; std::atomic isStartCollectHandler_; std::unordered_map sharedDHIds_; - static std::atomic logCount_; }; } // namespace DistributedInput } // namespace DistributedHardware -- Gitee From 17aa244d6c1b4a7213bb8c90a731134a1735bd69 Mon Sep 17 00:00:00 2001 From: tangfan Date: Fri, 12 Apr 2024 17:36:18 +0800 Subject: [PATCH 3/3] short log Signed-off-by: tangfan --- common/include/input_hub.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index adece5f..a7750cd 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -41,6 +41,7 @@ namespace DistributedHardware { namespace DistributedInput { namespace { const uint32_t SLEEP_TIME_US = 100 * 1000; +const int32_t PER_FIFTY_TIME = 50; const std::string MOUSE_NODE_KEY = "mouse"; } @@ -563,7 +564,7 @@ int32_t InputHub::OpenInputDeviceLocked(const std::string &devicePath) if (MakeDevice(fd, std::move(device)) < 0) { CloseFd(fd); - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGI("Opening device error: %{public}s", devicePath.c_str()); } return ERR_DH_INPUT_HUB_MAKE_DEVICE_FAIL; @@ -641,13 +642,13 @@ int32_t InputHub::QueryInputDeviceInfo(int fd, std::unique_ptr &device) void InputHub::QueryEventInfo(int fd, std::unique_ptr &device) { - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGI("QueryEventInfo: devName: %{public}s, dhId: %{public}s!", device->identifier.name.c_str(), GetAnonyString(device->identifier.descriptor).c_str()); } struct libevdev *dev = GetLibEvDev(fd); if (dev == nullptr) { - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGE("dev is nullptr"); } return; @@ -732,7 +733,7 @@ struct libevdev* InputHub::GetLibEvDev(int fd) int rc = 1; rc = libevdev_new_from_fd(fd, &dev); if (rc < 0) { - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGE("Failed to init libevdev (%{public}s)", strerror(-rc)); } return nullptr; @@ -754,7 +755,7 @@ void InputHub::GetEventTypes(struct libevdev *dev, InputDevice &identifier) int32_t InputHub::GetEventKeys(struct libevdev *dev, InputDevice &identifier) { if (!libevdev_has_event_type(dev, EV_KEY)) { - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGE("The device doesn't has EV_KEY type!"); } return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; @@ -772,7 +773,7 @@ int32_t InputHub::GetEventKeys(struct libevdev *dev, InputDevice &identifier) int32_t InputHub::GetABSInfo(struct libevdev *dev, InputDevice &identifier) { if (!libevdev_has_event_type(dev, EV_ABS)) { - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGE("The device doesn't has EV_ABS type!"); } return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; @@ -803,7 +804,7 @@ int32_t InputHub::GetABSInfo(struct libevdev *dev, InputDevice &identifier) int32_t InputHub::GetRELTypes(struct libevdev *dev, InputDevice &identifier) { if (!libevdev_has_event_type(dev, EV_REL)) { - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGE("The device doesn't has EV_REL type!"); } return ERR_DH_INPUT_HUB_QUERY_INPUT_DEVICE_INFO_FAIL; @@ -862,7 +863,7 @@ int32_t InputHub::MakeDevice(int fd, std::unique_ptr device) // If the device isn't recognized as something we handle, don't monitor it. if (device->classes == 0) { - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGI("Dropping device: name='%{public}s'", device->identifier.name.c_str()); } return ERR_DH_INPUT_HUB_MAKE_DEVICE_FAIL; @@ -973,7 +974,7 @@ void InputHub::GenerateDescriptor(InputDevice &identifier) const } identifier.descriptor = DH_ID_PREFIX + Sha256(rawDescriptor); - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGI("Created descriptor: raw=%{public}s, cooked=%{public}s", rawDescriptor.c_str(), GetAnonyString(identifier.descriptor).c_str()); } @@ -1344,7 +1345,7 @@ bool InputHub::IsAllDevicesStoped() void InputHub::RecordDeviceLog(const std::string &devicePath, const InputDevice &identifier) { - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGI("add device: %{public}s\n", devicePath.c_str()); DHLOGI(" bus: %{public}04x\n" " vendor %{public}04x\n" @@ -1623,7 +1624,7 @@ InputHub::Device::Device(int fd, const std::string &path) : next(nullptr), fd(fd), path(path), identifier({}), classes(0), enabled(false), isShare(false), isVirtual(fd < 0) { // Figure out the kinds of events the device reports. - if (logCount_ % 100 == 0) { + if (logCount_ % PER_FIFTY_TIME == 0) { DHLOGI("Ctor Device for get event mask, fd: %{public}d, path: %{public}s", fd, path.c_str()); } ioctl(fd, EVIOCGBIT(0, sizeof(evBitmask)), evBitmask); -- Gitee