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