1 /* 2 * Copyright (c) 2022-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 FI_LOG_H 17 #define FI_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 namespace OHOS { 28 namespace Msdp { 29 inline constexpr uint32_t MSDP_DOMAIN_ID { 0xD002220 }; 30 } // namespace Msdp 31 } // namespace OHOS 32 33 #ifndef FI_FUNC_FMT 34 #define FI_FUNC_FMT "in %{public}s, " 35 #endif 36 37 #ifndef FI_FUNC_INFO 38 #define FI_FUNC_INFO __FUNCTION__ 39 #endif 40 41 #ifndef FI_FILE_NAME 42 #define FI_FILE_NAME (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__)) 43 #endif 44 45 #ifndef FI_LINE_INFO 46 #define FI_LINE_INFO FI_FILE_NAME, __LINE__ 47 #endif 48 49 #define FI_HILOGD(fmt, ...) do { \ 50 if (HiLogIsLoggable(OHOS::Msdp::MSDP_DOMAIN_ID, LABEL.tag, LOG_DEBUG)) { \ 51 OHOS::HiviewDFX::HiLog::Debug(LABEL, FI_FUNC_FMT fmt, FI_FUNC_INFO, ##__VA_ARGS__); \ 52 } \ 53 } while (0) 54 #define FI_HILOGI(fmt, ...) do { \ 55 OHOS::HiviewDFX::HiLog::Info(LABEL, FI_FUNC_FMT fmt, FI_FUNC_INFO, ##__VA_ARGS__); \ 56 } while (0) 57 #define FI_HILOGW(fmt, ...) do { \ 58 OHOS::HiviewDFX::HiLog::Warn(LABEL, FI_FUNC_FMT fmt, FI_FUNC_INFO, ##__VA_ARGS__); \ 59 } while (0) 60 #define FI_HILOGE(fmt, ...) do { \ 61 OHOS::HiviewDFX::HiLog::Error(LABEL, FI_FUNC_FMT fmt, FI_FUNC_INFO, ##__VA_ARGS__); \ 62 } while (0) 63 #define FI_HILOGF(fmt, ...) do { \ 64 OHOS::HiviewDFX::HiLog::Fatal(LABEL, FI_FUNC_FMT fmt, FI_FUNC_INFO, ##__VA_ARGS__); \ 65 } while (0) 66 67 namespace OHOS { 68 namespace Msdp { 69 class InnerFunctionTracer { 70 public: 71 using HilogFunc = std::function<int32_t(const char *)>; 72 73 public: InnerFunctionTracer(HilogFunc logfn,const char * tag,LogLevel level)74 InnerFunctionTracer(HilogFunc logfn, const char* tag, LogLevel level) 75 : logfunc_ { logfn }, tag_ { tag }, level_ { level } 76 { 77 if (HiLogIsLoggable(OHOS::Msdp::MSDP_DOMAIN_ID, tag_, level_)) { 78 if (logfunc_ != nullptr) { 79 logfunc_("in %{public}s, enter"); 80 } 81 } 82 } ~InnerFunctionTracer()83 ~InnerFunctionTracer() 84 { 85 if (HiLogIsLoggable(OHOS::Msdp::MSDP_DOMAIN_ID, tag_, level_)) { 86 if (logfunc_ != nullptr) { 87 logfunc_("in %{public}s, leave"); 88 } 89 } 90 } 91 private: 92 HilogFunc logfunc_ { nullptr }; 93 const char* tag_ { nullptr }; 94 LogLevel level_ { LOG_LEVEL_MIN }; 95 }; 96 } // namespace Msdp 97 } // namespace OHOS 98 99 #define CALL_DEBUG_ENTER OHOS::Msdp::InnerFunctionTracer ___innerFuncTracer_Debug___ \ 100 { std::bind(&OHOS::HiviewDFX::HiLog::Debug, LABEL, std::placeholders::_1, __FUNCTION__), LABEL.tag, LOG_DEBUG } 101 102 #define CALL_INFO_TRACE OHOS::Msdp::InnerFunctionTracer ___innerFuncTracer_Info___ \ 103 { std::bind(&OHOS::HiviewDFX::HiLog::Info, LABEL, std::placeholders::_1, __FUNCTION__), LABEL.tag, LOG_INFO } 104 105 #define CALL_TEST_DEBUG OHOS::Msdp::InnerFunctionTracer ___innerFuncTracer_Info___ \ 106 { std::bind(&OHOS::HiviewDFX::HiLog::Info, LABEL, std::placeholders::_1, \ 107 (test_info_ == nullptr ? "TestBody" : test_info_->name())), LABEL.tag, LOG_DEBUG } 108 #endif // FI_LOG_H 109