• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 __FFRT_LOG_API_H__
17 #define __FFRT_LOG_API_H__
18 
19 #ifdef OHOS_STANDARD_SYSTEM
20 #include <array>
21 #ifdef FFRT_ENG_DEBUG
22 #include <info/fatal_message.h>
23 #endif
24 #include <string_view>
25 #include "hilog/log.h"
26 #include "internal_inc/osal.h"
27 #include "dfx/bbox/fault_logger_fd_manager.h"
28 #else
29 #include "log_base.h"
30 #endif
31 
32 #define FFRT_LOG_ERROR (0)
33 #define FFRT_LOG_WARN (1)
34 #define FFRT_LOG_INFO (2)
35 #define FFRT_LOG_DEBUG (3)
36 #define FFRT_LOG_LEVEL_MAX (FFRT_LOG_DEBUG + 1)
37 
38 unsigned int GetLogId(void);
39 bool IsInWhitelist(void);
40 void InitWhiteListFlag(void);
41 
42 #ifdef OHOS_STANDARD_SYSTEM
43 template<size_t N>
convertFmtToPublic(const char (& str)[N])44 constexpr auto convertFmtToPublic(const char(&str)[N])
45 {
46     constexpr std::string_view fmtpub = "{public}";
47     std::array<char, (N / 2) * fmtpub.size() + N> res{};
48     for (size_t i = 0, j = 0; i < N; ++i) {
49         res[j++] = str[i];
50         if (str[i] != '%') {
51             continue;
52         }
53 
54         if (str[i + 1] != '%' && str[i + 1] != '{') {
55             for (size_t k = 0; k < fmtpub.size(); ++k) {
56                 res[j++] = fmtpub[k];
57             }
58         } else {
59             res[j++] = str[i + 1];
60             i += 1;
61         }
62     }
63 
64     return res;
65 }
66 
67 #ifdef HILOG_FMTID
68 #define HILOG_IMPL_STD_ARRAY(type, level, fmt, ...) \
69     do { \
70         constexpr HILOG_FMT_IN_SECTION static auto hilogFmt = fmt; \
71         FmtId fmtid{ HILOG_UUID, HILOG_FMT_OFFSET(hilogFmt.data()) }; \
72         HiLogPrintDict(type, level, 0xD001719, "ffrt", &fmtid, hilogFmt.data(), ##__VA_ARGS__); \
73     } while (0)
74 #else
75 #define HILOG_IMPL_STD_ARRAY(type, level, fmt, ...) \
76     do { \
77         HiLogPrint(type, level, 0xD001719, "ffrt", fmt.data(), ##__VA_ARGS__); \
78     } while (0)
79 #endif
80 
81 #if (FFRT_LOG_LEVEL >= FFRT_LOG_DEBUG)
82 #define FFRT_LOGD(format, ...) \
83     do { \
84         if (unlikely(IsInWhitelist())) { \
85             constexpr auto fmtPub = convertFmtToPublic("%u:%s:%d " format); \
86             HILOG_IMPL_STD_ARRAY(LOG_CORE, LOG_DEBUG, fmtPub, GetLogId(), __func__, __LINE__, ##__VA_ARGS__); \
87         } \
88     } while (0)
89 #else
90 #define FFRT_LOGD(format, ...)
91 #endif
92 
93 #if (FFRT_LOG_LEVEL >= FFRT_LOG_INFO)
94 #define FFRT_LOGI(format, ...) \
95     do { \
96         constexpr auto fmtPub = convertFmtToPublic("%u:%s:%d " format); \
97         HILOG_IMPL_STD_ARRAY(LOG_CORE, LOG_INFO, fmtPub, GetLogId(), __func__, __LINE__, ##__VA_ARGS__); \
98     } while (0)
99 #else
100 #define FFRT_LOGI(format, ...)
101 #endif
102 
103 #if (FFRT_LOG_LEVEL >= FFRT_LOG_WARN)
104 #define FFRT_LOGW(format, ...) \
105     do { \
106         constexpr auto fmtPub = convertFmtToPublic("%u:%s:%d " format); \
107         HILOG_IMPL_STD_ARRAY(LOG_CORE, LOG_WARN, fmtPub, GetLogId(), __func__, __LINE__, ##__VA_ARGS__); \
108     } while (0)
109 #else
110 #define FFRT_LOGW(format, ...)
111 #endif
112 
113 #define FFRT_LOGE(format, ...) \
114     do { \
115         constexpr auto fmtPub = convertFmtToPublic("%u:%s:%d " format); \
116         HILOG_IMPL_STD_ARRAY(LOG_CORE, LOG_ERROR, fmtPub, GetLogId(), __func__, __LINE__, ##__VA_ARGS__); \
117     } while (0)
118 #else
119 #if (FFRT_LOG_LEVEL >= FFRT_LOG_DEBUG)
120 #define FFRT_LOGD(format, ...) FFRT_LOG(FFRT_LOG_DEBUG, format, ##__VA_ARGS__)
121 #else
122 #define FFRT_LOGD(format, ...)
123 #endif
124 
125 #if (FFRT_LOG_LEVEL >= FFRT_LOG_INFO)
126 #define FFRT_LOGI(format, ...) FFRT_LOG(FFRT_LOG_INFO, format, ##__VA_ARGS__)
127 #else
128 #define FFRT_LOGI(format, ...)
129 #endif
130 
131 #if (FFRT_LOG_LEVEL >= FFRT_LOG_WARN)
132 #define FFRT_LOGW(format, ...) FFRT_LOG(FFRT_LOG_WARN, format, ##__VA_ARGS__)
133 #else
134 #define FFRT_LOGW(format, ...)
135 #endif
136 
137 #define FFRT_LOGE(format, ...) FFRT_LOG(FFRT_LOG_ERROR, format, ##__VA_ARGS__)
138 #endif
139 
140 
141 #ifdef OHOS_STANDARD_SYSTEM
142 #define FFRT_BBOX_LOG(format, ...) \
143     do { \
144         FFRT_LOGE(format, ##__VA_ARGS__); \
145         FaultLoggerFdManager::Instance().WriteFaultLogger(format, ##__VA_ARGS__); \
146     } while (0)
147 #else
148 #define FFRT_BBOX_LOG(format, ...) FFRT_LOGE(format, ##__VA_ARGS__)
149 #endif
150 
151 #define FFRT_COND_DO_ERR(cond, expr, format, ...) \
152     if (cond) {                                   \
153         FFRT_LOGE(format, ##__VA_ARGS__);         \
154         {                                         \
155             expr;                                 \
156         }                                         \
157     }
158 
159 // Do not use this Marco directly
160 #define COND_RETURN_(COND, ERRCODE, ...) \
161     if ((COND)) { \
162         FFRT_LOGE(__VA_ARGS__); \
163         return ERRCODE; \
164     }
165 
166 #define FFRT_COND_RETURN_ERROR(COND, ERRCODE, ...) \
167     COND_RETURN_((COND), ERRCODE, ##__VA_ARGS__)
168 
169 #define FFRT_COND_RETURN_VOID(COND, ...) \
170     if ((COND)) { \
171         FFRT_LOGE(__VA_ARGS__); \
172         return; \
173     }
174 
175 // Do not use this Marco directly
176 #define COND_GOTO_WITH_ERRCODE_(COND, LABEL, ERROR, ERRCODE, ...) \
177     if ((COND)) { \
178         FFRT_LOGE(__VA_ARGS__); \
179         ERROR = (ERRCODE); \
180         goto LABEL; \
181     }
182 
183 #define FFRT_COND_GOTO_ERROR(COND, LABEL, ERROR, ERRCODE, ...) \
184     COND_GOTO_WITH_ERRCODE_((COND), LABEL, ERROR, ERRCODE, ##__VA_ARGS__)
185 
186 #define FFRT_UNUSED(expr) \
187     do { \
188         (void)(expr); \
189     } while (0)
190 
191 #ifdef FFRT_ENG_DEBUG
192 #define FFRT_UNLIKELY_COND_DO_ABORT(cond, fmt, ...) \
193     do { \
194         if (unlikely(cond)) { \
195             char fatal_msg[256]; \
196             snprintf_s(fatal_msg, sizeof(fatal_msg), sizeof(fatal_msg) - 1, fmt, ##__VA_ARGS__); \
197             FFRT_LOGE(fmt, ##__VA_ARGS__); \
198             set_fatal_message(fatal_msg); \
199             abort(); \
200         } \
201     } while (0)
202 
203 #else
204 #define FFRT_UNLIKELY_COND_DO_ABORT(cond, fmt, ...) \
205     do { \
206         if (unlikely(cond)) { \
207             FFRT_LOGE(fmt, ##__VA_ARGS__); \
208         } \
209     } while (0)
210 #endif // FFRT_ENG_DEBUG
211 #endif // __FFRT_LOG_API_H__
212