• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <cinttypes>
19 #include <functional>
20 #include <future>
21 #include <string>
22 #include <sstream>
23 
24 #include "hilog/log.h"
25 
26 #include "util.h"
27 #include "klog.h"
28 
29 namespace OHOS {
30 namespace MMI {
31 inline constexpr uint32_t MMI_LOG_DOMAIN = 0xD002800;
32 } // namespace MMI
33 } // namespace OHOS
34 
35 #ifndef MMI_FUNC_FMT
36 #define MMI_FUNC_FMT "in %{public}s, "
37 #endif
38 
39 #ifndef MMI_FUNC_INFO
40 #define MMI_FUNC_INFO __FUNCTION__
41 #endif
42 
43 #ifndef MMI_FILE_NAME
44 #define MMI_FILE_NAME   (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__))
45 #endif
46 
47 #ifndef MMI_LINE_INFO
48 #define MMI_LINE_INFO   MMI_FILE_NAME, __LINE__
49 #endif
50 
51 #define MMI_HILOGD(fmt, ...) do { \
52     if (HiLogIsLoggable(OHOS::MMI::MMI_LOG_DOMAIN, LABEL.tag, LOG_DEBUG)) { \
53         ::OHOS::HiviewDFX::HiLog::Debug(LABEL, MMI_FUNC_FMT fmt, MMI_FUNC_INFO, ##__VA_ARGS__); \
54     } \
55 } while (0)
56 #define MMI_HILOGI(fmt, ...) do { \
57     ::OHOS::HiviewDFX::HiLog::Info(LABEL, MMI_FUNC_FMT fmt, MMI_FUNC_INFO, ##__VA_ARGS__); \
58 } while (0)
59 #define MMI_HILOGW(fmt, ...) do { \
60     ::OHOS::HiviewDFX::HiLog::Warn(LABEL, MMI_FUNC_FMT fmt, MMI_FUNC_INFO, ##__VA_ARGS__); \
61 } while (0)
62 #define MMI_HILOGE(fmt, ...) do { \
63     ::OHOS::HiviewDFX::HiLog::Error(LABEL, MMI_FUNC_FMT fmt, MMI_FUNC_INFO, ##__VA_ARGS__); \
64 } while (0)
65 #define MMI_HILOGF(fmt, ...) do { \
66     ::OHOS::HiviewDFX::HiLog::Fatal(LABEL, MMI_FUNC_FMT fmt, MMI_FUNC_INFO, ##__VA_ARGS__); \
67 } while (0)
68 
69 #define MMI_HILOGDK(fmt, ...) do { \
70     KMSG_LOGD(fmt, ##__VA_ARGS__); \
71     MMI_HILOGD(fmt, ##__VA_ARGS__); \
72 } while (0)
73 
74 #define MMI_HILOGIK(fmt, ...) do { \
75     KMSG_LOGI(fmt, ##__VA_ARGS__); \
76     MMI_HILOGI(fmt, ##__VA_ARGS__); \
77 } while (0)
78 
79 #define MMI_HILOGWK(fmt, ...) do { \
80     KMSG_LOGW(fmt, ##__VA_ARGS__); \
81     MMI_HILOGW(fmt, ##__VA_ARGS__); \
82 } while (0)
83 
84 #define MMI_HILOGEK(fmt, ...) do { \
85     KMSG_LOGE(fmt, ##__VA_ARGS__); \
86     MMI_HILOGE(fmt, ##__VA_ARGS__); \
87 } while (0)
88 
89 #define MMI_HILOGFK(fmt, ...) do { \
90     KMSG_LOGF(fmt, ##__VA_ARGS__); \
91     MMI_HILOGF(fmt, ##__VA_ARGS__); \
92 } while (0)
93 
94 namespace OHOS {
95 namespace MMI {
96 inline constexpr int32_t EVENT_TYPE_POINTER = 0X00020000;
97 inline constexpr int32_t TIMEOUT = 100000;
98 inline constexpr int32_t POINTER_ACTION_UP = 4;
99 inline constexpr int32_t POINTER_ACTION_MOVE = 3;
100 inline constexpr int32_t FINAL_FINGER = 1;
101 
102 class InnerFunctionTracer {
103 public:
104     using HilogFunc = std::function<int(const char *)>;
105 
106 public:
InnerFunctionTracer(HilogFunc logfn,const char * tag,LogLevel level)107     InnerFunctionTracer(HilogFunc logfn, const char* tag, LogLevel level)
108         : logfn_ { logfn }, tag_ { tag }, level_ { level }
109     {
110         if (HiLogIsLoggable(OHOS::MMI::MMI_LOG_DOMAIN, tag_, level_)) {
111             if (logfn_ != nullptr) {
112                 logfn_("in %{public}s, enter");
113             }
114         }
115     }
~InnerFunctionTracer()116     ~InnerFunctionTracer()
117     {
118         if (HiLogIsLoggable(OHOS::MMI::MMI_LOG_DOMAIN, tag_, level_)) {
119             if (logfn_ != nullptr) {
120                 logfn_("in %{public}s, leave");
121             }
122         }
123     }
124 private:
125     HilogFunc logfn_ { nullptr };
126     const char* tag_ { nullptr };
127     LogLevel level_ { LOG_LEVEL_MIN };
128 };
129 } // namespace MMI
130 } // namespace OHOS
131 
132 #define CALL_DEBUG_ENTER        ::OHOS::MMI::InnerFunctionTracer ___innerFuncTracer_Debug___    \
133     { std::bind(&::OHOS::HiviewDFX::HiLog::Debug, LABEL, std::placeholders::_1, __FUNCTION__), LABEL.tag, LOG_DEBUG }
134 
135 #define CALL_INFO_TRACE         ::OHOS::MMI::InnerFunctionTracer ___innerFuncTracer_Info___     \
136     { std::bind(&::OHOS::HiviewDFX::HiLog::Info, LABEL, std::placeholders::_1, __FUNCTION__), LABEL.tag, LOG_INFO }
137 #endif // MMI_LOG_H
138