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