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