1 /*
2 * Copyright (c) 2021-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 BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EVENT_HANDLER_UTILS_H
17 #define BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EVENT_HANDLER_UTILS_H
18
19 #include <cerrno>
20 #include <chrono>
21 #include <cstring>
22 #include <string>
23
24 #include "hilog/log.h"
25 #include "hitrace/trace.h"
26 #include "inner_event.h"
27
28 #define DEFINE_HILOG_LABEL(name) \
29 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, LOG_DOMAIN, name}
30
31 #define HILOGE(...) OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, ##__VA_ARGS__)
32 #define HILOGW(...) OHOS::HiviewDFX::HiLog::Warn(LOG_LABEL, ##__VA_ARGS__)
33 #define HILOGI(...) OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, ##__VA_ARGS__)
34 #define HILOGD(...) OHOS::HiviewDFX::HiLog::Debug(LOG_LABEL, ##__VA_ARGS__)
35
36 namespace OHOS {
37 namespace AppExecFwk {
38 inline const int64_t NANOSECONDS_PER_ONE_MILLISECOND = 1000000;
39 inline const int64_t NANOSECONDS_PER_ONE_SECOND = 1000000000;
40 inline const int32_t INFINITE_TIMEOUT = -1;
41 inline const uint8_t MAX_ERRORMSG_LEN = 128;
42
43 // Help to convert time point into delay time from now.
TimePointToTimeOut(const InnerEvent::TimePoint & when)44 static inline int64_t TimePointToTimeOut(const InnerEvent::TimePoint &when)
45 {
46 InnerEvent::TimePoint now = InnerEvent::Clock::now();
47 if (when <= now) {
48 return 0;
49 }
50
51 auto duration = when - now;
52 return std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
53 }
54
NanosecondsToTimeout(int64_t nanoseconds)55 static inline int32_t NanosecondsToTimeout(int64_t nanoseconds)
56 {
57 if (nanoseconds < 0) {
58 return INFINITE_TIMEOUT;
59 }
60
61 int64_t milliseconds = nanoseconds / NANOSECONDS_PER_ONE_MILLISECOND;
62 if ((nanoseconds % NANOSECONDS_PER_ONE_MILLISECOND) > 0) {
63 milliseconds += 1;
64 }
65
66 return (milliseconds > INT32_MAX) ? INT32_MAX : static_cast<int32_t>(milliseconds);
67 }
68
69 using HiTraceChain = OHOS::HiviewDFX::HiTraceChain;
70
AllowHiTraceOutPut(const std::shared_ptr<HiTraceId> & traceId,bool isSyncEvent)71 static inline bool AllowHiTraceOutPut(const std::shared_ptr<HiTraceId>& traceId, bool isSyncEvent)
72 {
73 if ((!traceId) || (!traceId->IsValid())) {
74 return false;
75 }
76 if ((!isSyncEvent) && (!traceId->IsFlagEnabled(HITRACE_FLAG_INCLUDE_ASYNC))) {
77 return false;
78 }
79 return true;
80 }
81
HiTracePointerOutPut(const std::shared_ptr<HiTraceId> & spanId,const InnerEvent::Pointer & event,const char * action,HiTraceTracepointType type)82 static inline void HiTracePointerOutPut(const std::shared_ptr<HiTraceId>& spanId,
83 const InnerEvent::Pointer& event, const char* action, HiTraceTracepointType type)
84 {
85 if (!event->HasTask()) {
86 HiTraceChain::Tracepoint(type, *spanId, "%s event, event id: %d", action, event->GetInnerEventId());
87 } else if (!event->GetTaskName().empty()) {
88 HiTraceChain::Tracepoint(type, *spanId, "%s task with name, name: %s", action, event->GetTaskName().c_str());
89 } else {
90 HiTraceChain::Tracepoint(type, *spanId, "%s UnNamed Task", action);
91 }
92 }
93
94 static inline void GetLastErr(char *errmsg, size_t size = MAX_ERRORMSG_LEN)
95 {
96 size = size > MAX_ERRORMSG_LEN ? MAX_ERRORMSG_LEN : size;
97 strerror_r(errno, errmsg, size);
98 }
99 } // namespace AppExecFwk
100 } // namespace OHOS
101
102 #endif // #ifndef BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EVENT_HANDLER_UTILS_H
103