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 COMMON_UTILITIES_HPP
17 #define COMMON_UTILITIES_HPP
18 
19 #include <array>
20 #include <chrono>
21 #include <string_view>
22 
23 #ifdef __OHOS__
24 #include "hilog/log.h"
25 #endif
26 
27 #define FORCE_INLINE __attribute__((always_inline)) inline
28 
29 namespace OHOS::uitest {
30     using CStr = const char *;
31     constexpr size_t INDEX_ZERO = 0;
32     constexpr size_t INDEX_ONE = 1;
33     constexpr size_t INDEX_TWO = 2;
34     constexpr size_t INDEX_THREE = 3;
35     constexpr size_t INDEX_FOUR = 4;
36     constexpr size_t INDEX_FIVE = 5;
37     constexpr size_t INDEX_SIX = 6;
38     constexpr size_t INDEX_SEVEN = 7;
39     constexpr size_t INDEX_EIGHT = 8;
40     constexpr size_t INDEX_NINE = 9;
41     constexpr int32_t ZERO = 0;
42     constexpr int32_t ONE = 1;
43     constexpr int32_t TWO = 2;
44     constexpr int32_t THREE = 3;
45     constexpr int32_t FOUR = 4;
46     constexpr int32_t FIVE = 5;
47     constexpr int32_t SIX = 6;
48     constexpr int32_t SEVEN = 7;
49     constexpr int32_t EIGHT = 8;
50 
51     /**Get current time millisecond.*/
GetCurrentMillisecond()52     inline uint64_t GetCurrentMillisecond()
53     {
54         using namespace std::chrono;
55         return time_point_cast<milliseconds>(steady_clock::now()).time_since_epoch().count();
56     }
57 
58     /**Get current time microseconds.*/
GetCurrentMicroseconds()59     inline uint64_t GetCurrentMicroseconds()
60     {
61         using namespace std::chrono;
62         return time_point_cast<microseconds>(steady_clock::now()).time_since_epoch().count();
63     }
64 
65     // log tag length limit
66     constexpr uint8_t MAX_LOG_TAG_LEN = 64;
67 
68     /**Generates log-tag by fileName and lineNumber, must be 'constexpr' to ensure the efficiency of Logger.*/
GenLogTag(std::string_view fp, std::string_view func)69     constexpr std::array<char, MAX_LOG_TAG_LEN> GenLogTag(std::string_view fp, std::string_view func)
70     {
71         constexpr uint8_t MAX_CONTENT_LEN = MAX_LOG_TAG_LEN - 1;
72         std::array<char, MAX_LOG_TAG_LEN> chars = {0};
73         size_t pos = fp.find_last_of('/');
74         if (pos == std::string_view::npos) {
75             pos = 0;
76         }
77         uint8_t writeCursor = 0;
78         chars[writeCursor++] = '[';
79         for (size_t offSet = pos + 1; offSet < fp.length() && writeCursor < MAX_CONTENT_LEN; offSet++) {
80             chars[writeCursor++] = fp[offSet];
81         }
82         if (writeCursor < MAX_CONTENT_LEN) {
83             chars[writeCursor++] = ':';
84         }
85         if (writeCursor < MAX_CONTENT_LEN) {
86             chars[writeCursor++] = '(';
87         }
88         for (size_t offSet = 0; offSet < func.length() && writeCursor < MAX_CONTENT_LEN; offSet++) {
89             chars[writeCursor++] = func[offSet];
90         }
91         if (writeCursor < MAX_CONTENT_LEN) {
92             chars[writeCursor++] = ')';
93         }
94         if (writeCursor < MAX_CONTENT_LEN) {
95             chars[writeCursor++] = ']';
96         }
97         // record the actual tag-length in the end byte
98         chars[MAX_CONTENT_LEN] = writeCursor;
99         return chars;
100     }
101 
102     // log level
103     enum LogRank : uint8_t {
104         DEBUG = 3, INFO = 4, WARN = 5, ERROR = 6
105     };
106 
107 #ifdef __OHOS__
108 #ifndef LOG_TAG
109 #define LOG_TAG "UiTestKit"
110 #endif
111 #undef LOG_DOMAIN
112 #define LOG_DOMAIN 0xD003100
113 // print pretty log with pretty format, auto-generate tag by fileName and functionName at compile time
114 #define LOG(LEVEL, FMT, VARS...) do { \
115     static constexpr auto tagChars= GenLogTag(__FILE__, __FUNCTION__); \
116     static constexpr int8_t tagLen = tagChars[MAX_LOG_TAG_LEN - 1];   \
117     if constexpr (tagLen > 0) { \
118         auto tag = std::string_view(tagChars.data(), tagLen); \
119         static constexpr LogType type = LogType::LOG_CORE; \
120         HILOG_##LEVEL(type, "%{public}s " FMT, tag.data(), ##VARS); \
121     } \
122 }while (0)
123 #else
124 // nop logger
125 #define LOG(LEVEL, FMT, VARS...) do {}while (0)
126 #endif
127 
128 // print debug log
129 #define LOG_D(FMT, VARS...) LOG(DEBUG, FMT, ##VARS)
130 // print info log
131 #define LOG_I(FMT, VARS...) LOG(INFO, FMT, ##VARS)
132 // print warning log
133 #define LOG_W(FMT, VARS...) LOG(WARN, FMT, ##VARS)
134 // print error log
135 #define LOG_E(FMT, VARS...) LOG(ERROR, FMT, ##VARS)
136 }
137 
138 #define DCHECK(cond) \
139 do { \
140     if (!(cond)) { \
141         LOG_E("DCHECK FAILED, %{public}s %{public}d: %{public}s", __FILE__, __LINE__, #cond); \
142         _Exit(0); \
143     } \
144 } while (0)
145 
146 #endif