1 /*
2  * Copyright (c) 2023-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 MMI_LOG_H
17 #define MMI_LOG_H
18 
19 #include <cstdarg>
20 #include <cstdio>
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 struct HiLogLabel {
25     int32_t log;
26     int32_t domain;
27     const char* tag;
28 };
29 } // namespace HiviewDFX
30 } // namespace OHOS
31 
ReplaceAll(std::string & str,const std::string & what,const std::string & with)32 inline void ReplaceAll(std::string& str, const std::string& what, const std::string& with)
33 {
34     for (size_t pos = 0; str.npos != (pos = str.find(what, pos)); pos += with.size()) {
35         str.replace(pos, what.size(), with);
36     }
37 }
38 
PrintLog(const OHOS::HiviewDFX::HiLogLabel & label,const char * type,const char * fmt,...)39 inline void PrintLog(const OHOS::HiviewDFX::HiLogLabel& label, const char* type, const char* fmt, ...)
40 {
41     std::string fmts{fmt};
42     ReplaceAll(fmts, "{public}", "");
43     ReplaceAll(fmts, "{private}", "");
44     fmts.append("\n");
45     va_list args;
46     va_start(args, fmt);
47     std::printf("%s/%s: ", label.tag, type);
48     std::vprintf(fmts.c_str(), args);
49     va_end(args);
50 }
51 
52 #define MMI_HILOGD(fmt, ...) PrintLog(LABEL, "D", fmt, ##__VA_ARGS__)
53 #define MMI_HILOGI(fmt, ...) PrintLog(LABEL, "I", fmt, ##__VA_ARGS__)
54 #define MMI_HILOGW(fmt, ...) PrintLog(LABEL, "W", fmt, ##__VA_ARGS__)
55 #define MMI_HILOGE(fmt, ...) PrintLog(LABEL, "E", fmt, ##__VA_ARGS__)
56 #define MMI_HILOGF(fmt, ...) PrintLog(LABEL, "F", fmt, ##__VA_ARGS__)
57 #define MMI_HILOGDK(fmt, ...) PrintLog(LABEL, "D", fmt, ##__VA_ARGS__)
58 #define MMI_HILOGIK(fmt, ...) PrintLog(LABEL, "I", fmt, ##__VA_ARGS__)
59 #define MMI_HILOGWK(fmt, ...) PrintLog(LABEL, "W", fmt, ##__VA_ARGS__)
60 #define MMI_HILOGEK(fmt, ...) PrintLog(LABEL, "E", fmt, ##__VA_ARGS__)
61 #define MMI_HILOGFK(fmt, ...) PrintLog(LABEL, "F", fmt, ##__VA_ARGS__)
62 #define CALL_DEBUG_ENTER (void)LABEL
63 #define CALL_INFO_TRACE (void)LABEL
64 #define CALL_TEST_DEBUG (void)LABEL
65 
66 constexpr int32_t LOG_CORE = 0;
67 namespace OHOS {
68 namespace MMI {
69 constexpr int32_t MMI_LOG_DOMAIN = 0;
70 } // namespace MMI
71 } // namespace OHOS
72 #endif // MMI_LOG_H
73