• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 OHOS_FORM_FWK_FMS_LOG_WRAPPER_H
17 #define OHOS_FORM_FWK_FMS_LOG_WRAPPER_H
18 
19 #define CONFIG_HILOG
20 #ifdef CONFIG_HILOG
21 #include <cinttypes>
22 #include <functional>
23 
24 #include "hilog/log.h"
25 
26 #ifdef HILOG_FATAL
27 #undef HILOG_FATAL
28 #endif
29 
30 #ifdef HILOG_ERROR
31 #undef HILOG_ERROR
32 #endif
33 
34 #ifdef HILOG_WARN
35 #undef HILOG_WARN
36 #endif
37 
38 #ifdef HILOG_INFO
39 #undef HILOG_INFO
40 #endif
41 
42 #ifdef HILOG_DEBUG
43 #undef HILOG_DEBUG
44 #endif
45 
46 #ifndef FMS_LOG_DOMAIN
47 #define FMS_LOG_DOMAIN 0xD001301
48 #endif
49 
50 #ifndef FMS_LOG_TAG
51 #define FMS_LOG_TAG "FormManagerService"
52 #endif
53 
54 #ifndef FMS_FUNC_FMT
55 #define FMS_FUNC_FMT "[%{public}s(%{public}s:%{public}d)]"
56 #endif
57 
58 #ifndef FMS_FILE_NAME
59 #define FMS_FILE_NAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
60 #endif
61 
62 #ifndef FMS_FUNC_INFO
63 #define FMS_FUNC_INFO FMS_FILE_NAME, __FUNCTION__, __LINE__
64 #endif
65 
66 #define HILOG_ERROR(fmt, ...) do { \
67     (void)HILOG_IMPL(LOG_CORE, LOG_ERROR, FMS_LOG_DOMAIN, FMS_LOG_TAG, \
68     FMS_FUNC_FMT fmt, FMS_FUNC_INFO, ##__VA_ARGS__);                   \
69 } while (0)
70 
71 #define HILOG_WARN(fmt, ...) do { \
72     (void)HILOG_IMPL(LOG_CORE, LOG_WARN, FMS_LOG_DOMAIN, FMS_LOG_TAG, \
73     FMS_FUNC_FMT fmt, FMS_FUNC_INFO, ##__VA_ARGS__);                  \
74 } while (0)
75 
76 #define HILOG_INFO(fmt, ...) do { \
77     (void)HILOG_IMPL(LOG_CORE, LOG_INFO, FMS_LOG_DOMAIN, FMS_LOG_TAG, \
78     FMS_FUNC_FMT fmt, FMS_FUNC_INFO, ##__VA_ARGS__);                  \
79 } while (0)
80 
81 #define HILOG_DEBUG(fmt, ...) do { \
82     (void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, FMS_LOG_DOMAIN, FMS_LOG_TAG, \
83     FMS_FUNC_FMT fmt, FMS_FUNC_INFO, ##__VA_ARGS__);                   \
84 } while (0)
85 
86 #define HILOG_FATAL(fmt, ...) do { \
87     (void)HILOG_IMPL(LOG_CORE, LOG_FATAL, FMS_LOG_DOMAIN, FMS_LOG_TAG, \
88     FMS_FUNC_FMT fmt, FMS_FUNC_INFO, ##__VA_ARGS__);                   \
89 } while (0)
90 
91 
92 namespace OHOS {
93 namespace AppExecFwk {
94 
95 class InnerFunctionTracer {
96 public:
97     using HilogFunc = std::function<int(const char *)>;
98 
InnerFunctionTracer(HilogFunc logfn,const char * tag,LogLevel level)99     InnerFunctionTracer(HilogFunc logfn, const char* tag, LogLevel level)
100         : logfn_(logfn), tag_(tag), level_(level)
101     {
102         if (HiLogIsLoggable(FMS_LOG_DOMAIN, tag_, level_)) {
103             if (logfn_ != nullptr) {
104                 logfn_(FMS_FUNC_FMT "enter");
105             }
106         }
107     }
~InnerFunctionTracer()108     ~InnerFunctionTracer()
109     {
110         if (HiLogIsLoggable(FMS_LOG_DOMAIN, tag_, level_)) {
111             if (logfn_ != nullptr) {
112                 logfn_(FMS_FUNC_FMT "leave");
113             }
114         }
115     }
116 private:
117     HilogFunc logfn_ { nullptr };
118     const char* tag_ { nullptr };
119     LogLevel level_ { LOG_LEVEL_MIN };
120 };
121 } // namespace AppExecFwk
122 } // namespace OHOS
123 
124 #ifndef FMS_CALL_DEBUG_ENTER
125 #define FMS_CALL_DEBUG_ENTER    ::OHOS::AppExecFwk::InnerFunctionTracer ___innerFuncTracer_Debug___     \
126     { std::bind(&HiLogPrint, LOG_CORE, LOG_DEBUG, FMS_LOG_DOMAIN, FMS_LOG_TAG, std::placeholders::_1,   \
127       FMS_FUNC_INFO), FMS_LOG_TAG, LOG_DEBUG }
128 #endif // FMS_CALL_DEBUG_ENTER
129 
130 #ifndef FMS_CALL_INFO_ENTER
131 #define FMS_CALL_INFO_ENTER     ::OHOS::AppExecFwk::InnerFunctionTracer ___innerFuncTracer_Debug___     \
132     { std::bind(&HiLogPrint, LOG_CORE, LOG_INFO, FMS_LOG_DOMAIN, FMS_LOG_TAG, std::placeholders::_1,    \
133       FMS_FUNC_INFO), FMS_LOG_TAG, LOG_INFO }
134 #endif // FMS_CALL_INFO_ENTER
135 
136 #else
137 
138 #define HILOG_FATAL(...)
139 #define HILOG_ERROR(...)
140 #define HILOG_WARN(...)
141 #define HILOG_INFO(...)
142 #define HILOG_DEBUG(...)
143 
144 #endif  // CONFIG_HILOG
145 #endif  // OHOS_FORM_FWK_FMS_LOG_WRAPPER_H
146