• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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
17 #define COMMON_UTILITIES
18 
19 #include <array>
20 #include <string_view>
21 #include "nlohmann/json.hpp"
22 
23 #ifdef __OHOS__
24 #include "hilog/log.h"
25 #endif
26 
27 #define FORCE_INLINE __attribute__((always_inline)) inline
28 
29 namespace OHOS::perftest {
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 int32_t ZERO = 0;
40     constexpr int32_t ONE = 1;
41     constexpr int32_t TWO = 2;
42     constexpr int32_t THREE = 3;
43     constexpr int32_t ONE_HUNDRED = 100;
44 
ReadArgFromJson(const nlohmann::json & json,const std::string arg,const T defValue)45     template <typename T> T ReadArgFromJson(const nlohmann::json &json, const std::string arg, const T defValue)
46     {
47         if (json.type() == nlohmann::detail::value_t::object && json.contains(arg)) {
48             nlohmann::json val = json[arg];
49             return val.get<T>();
50         }
51         return defValue;
52     }
53 
54     // log tag length limit
55     constexpr uint8_t MAX_LOG_TAG_LEN = 64;
56 
57     /**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)58     constexpr std::array<char, MAX_LOG_TAG_LEN> GenLogTag(std::string_view fp, std::string_view func)
59     {
60         constexpr uint8_t MAX_CONTENT_LEN = MAX_LOG_TAG_LEN - 1;
61         std::array<char, MAX_LOG_TAG_LEN> chars = {0};
62         size_t pos = fp.find_last_of('/');
63         if (pos == std::string_view::npos) {
64             pos = 0;
65         }
66         uint8_t writeCursor = 0;
67         chars[writeCursor++] = '[';
68         for (size_t offSet = pos + 1; offSet < fp.length() && writeCursor < MAX_CONTENT_LEN; offSet++) {
69             chars[writeCursor++] = fp[offSet];
70         }
71         if (writeCursor < MAX_CONTENT_LEN) {
72             chars[writeCursor++] = ':';
73         }
74         if (writeCursor < MAX_CONTENT_LEN) {
75             chars[writeCursor++] = '(';
76         }
77         for (size_t offSet = 0; offSet < func.length() && writeCursor < MAX_CONTENT_LEN; offSet++) {
78             chars[writeCursor++] = func[offSet];
79         }
80         if (writeCursor < MAX_CONTENT_LEN) {
81             chars[writeCursor++] = ')';
82         }
83         if (writeCursor < MAX_CONTENT_LEN) {
84             chars[writeCursor++] = ']';
85         }
86         // record the actual tag-length in the end byte
87         chars[MAX_CONTENT_LEN] = writeCursor;
88         return chars;
89     }
90 
91     // log level
92     enum LogRank : uint8_t {
93         DEBUG = 3, INFO = 4, WARN = 5, ERROR = 6
94     };
95 
96 #ifdef __OHOS__
97 #ifndef LOG_TAG
98 #define LOG_TAG "PerfTest"
99 #endif
100 #undef LOG_DOMAIN
101 #define LOG_DOMAIN 0xD003120
102 // print pretty log with pretty format, auto-generate tag by fileName and functionName at compile time
103 #define LOG(LEVEL, FMT, VARS...) do { \
104     static constexpr auto tagChars= GenLogTag(__FILE__, __FUNCTION__); \
105     static constexpr int8_t tagLen = tagChars[MAX_LOG_TAG_LEN - 1];   \
106     if constexpr (tagLen > 0) { \
107         auto tag = std::string_view(tagChars.data(), tagLen); \
108         static constexpr LogType type = LogType::LOG_CORE; \
109         HILOG_##LEVEL(type, "%{public}s " FMT, tag.data(), ##VARS); \
110     } \
111 }while (0)
112 #else
113 // nop logger
114 #define LOG(LEVEL, FMT, VARS...) do {}while (0)
115 #endif
116 
117 // print debug log
118 #define LOG_D(FMT, VARS...) LOG(DEBUG, FMT, ##VARS)
119 // print info log
120 #define LOG_I(FMT, VARS...) LOG(INFO, FMT, ##VARS)
121 // print warning log
122 #define LOG_W(FMT, VARS...) LOG(WARN, FMT, ##VARS)
123 // print error log
124 #define LOG_E(FMT, VARS...) LOG(ERROR, FMT, ##VARS)
125 }
126 
127 #define DCHECK(cond) \
128 do { \
129     if (!(cond)) { \
130         LOG_E("DCHECK FAILED, %{public}s %{public}d: %{public}s", __FILE__, __LINE__, #cond); \
131         _Exit(0); \
132     } \
133 } while (0)
134 
135 #endif