1 /*
2 * Copyright (C) 2021 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 <string>
20 #include "hilog/log.h"
21
22 namespace OHOS {
23 namespace {
24 constexpr uint32_t COMMON = 0xD002800;
25 }
26 static constexpr HiviewDFX::HiLogLabel MMI_COMMON_LABEL = { LOG_CORE, COMMON, "MMI" };
27 const std::string DOUBLE_COLON = "::";
28 #if defined(DEBUG)
29 #define MMI_LOGD(fmt, ...) \
30 HiviewDFX::HiLog::Debug(MMI_COMMON_LABEL, "File:%{public}s, Line:%{public}d, Function:%{public}s " fmt,\
31 __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
32
33 #define MMI_LOGI(fmt, ...) \
34 HiviewDFX::HiLog::Info(MMI_COMMON_LABEL, "File:%{public}s, Line:%{public}d, Function:%{public}s " fmt,\
35 __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
36
37 #define MMI_LOGW(fmt, ...) \
38 HiviewDFX::HiLog::Warn(MMI_COMMON_LABEL, "File:%{public}s, Line:%{public}d, Function:%{public}s " fmt,\
39 __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
40
41 #define MMI_LOGE(fmt, ...) \
42 HiviewDFX::HiLog::Error(MMI_COMMON_LABEL, "File:%{public}s, Line:%{public}d, Function:%{public}s " fmt,\
43 __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
44
45 #define MMI_LOGF(fmt, ...) \
46 HiviewDFX::HiLog::Fatal(MMI_COMMON_LABEL, "File:%{public}s, Line:%{public}d, Function:%{public}s " fmt,\
47 __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
48 #else
49
_FuncName(std::string && funcName)50 static const char* _FuncName(std::string &&funcName)
51 {
52 auto pos = funcName.find('(');
53 if (pos != std::string::npos) {
54 funcName.erase(funcName.begin() + pos, funcName.end());
55 }
56
57 pos = funcName.find(DOUBLE_COLON);
58 if (pos != std::string::npos) {
59 funcName.erase(funcName.begin(), funcName.begin() + pos + DOUBLE_COLON.size()); // need wrap "::" symbol
60 }
61
62 return funcName.c_str();
63 }
64
65 #define CLASS_FUNCTION _FuncName(std::string(__PRETTY_FUNCTION__))
66
67 #define MMI_LOGD(fmt, ...) \
68 HiviewDFX::HiLog::Debug(MMI_COMMON_LABEL, "%{public}s: " fmt, CLASS_FUNCTION, ##__VA_ARGS__)
69
70 #define MMI_LOGI(fmt, ...) \
71 HiviewDFX::HiLog::Info(MMI_COMMON_LABEL, "%{public}s: " fmt, CLASS_FUNCTION, ##__VA_ARGS__)
72
73 #define MMI_LOGW(fmt, ...) \
74 HiviewDFX::HiLog::Warn(MMI_COMMON_LABEL, "%{public}s: " fmt, CLASS_FUNCTION, ##__VA_ARGS__)
75
76 #define MMI_LOGE(fmt, ...) \
77 HiviewDFX::HiLog::Error(MMI_COMMON_LABEL, "%{public}s: " fmt, CLASS_FUNCTION, ##__VA_ARGS__)
78
79 #define MMI_LOGF(fmt, ...) \
80 HiviewDFX::HiLog::Fatal(MMI_COMMON_LABEL, "%{public}s: " fmt, CLASS_FUNCTION, ##__VA_ARGS__)
81 #endif
82 } // namespace OHOS
83
84 #endif // MMI_LOG_H
85