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