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