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