1 /*
2 * Copyright (c) 2024 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_AAFWK_HILOG_TAG_WRAPPER_H
17 #define OHOS_AAFWK_HILOG_TAG_WRAPPER_H
18
19 #include <cinttypes>
20 #include <map>
21
22 #include "hilog/log.h"
23
24 #ifndef AAFWK_FUNC_FMT
25 #define AAFWK_FUNC_FMT "[%{public}s:%{public}d]"
26 #endif
27
28 #ifndef AAFWK_FILE_NAME
29 #define AAFWK_FILE_NAME \
30 (__builtin_strrchr(__FILE_NAME__, '/') ? __builtin_strrchr(__FILE_NAME__, '/') + 1 : __FILE_NAME__)
31 #endif
32
33 #ifndef AAFWK_FUNC_INFO
34 #define AAFWK_FUNC_INFO AAFWK_FILE_NAME, __LINE__
35 #endif
36
37
38 namespace OHOS::AAFwk {
39 enum class AAFwkLogTag : uint32_t {
40 DEFAULT = 0xD001300, // 0XD001300
41 ABILITY,
42 TEST,
43 AA_TOOL,
44 ABILITY_SIM,
45
46 APPDFR = DEFAULT + 0x10, // 0xD001310
47 APPMGR,
48 DBOBSMGR,
49 DIALOG,
50 QUICKFIX,
51 URIPERMMGR,
52 BUNDLEMGRHELPER,
53 APPKIT,
54
55 JSENV = DEFAULT + 0x20, // 0xD001320
56 JSRUNTIME,
57 FA,
58 INTENT,
59 JSNAPI,
60 CJRUNTIME,
61
62 DELEGATOR = DEFAULT + 0x30, // 0xD001330
63 CONTEXT,
64 UIABILITY,
65 WANT,
66 MISSION,
67 CONNECTION,
68 ABILITYMGR,
69 ECOLOGICAL_RULE,
70 DATA_ABILITY,
71
72 EXT = DEFAULT + 0x40, // 0xD001340
73 AUTOFILL_EXT,
74 SERVICE_EXT,
75 FORM_EXT,
76 SHARE_EXT,
77 UI_EXT,
78 ACTION_EXT,
79 EMBEDDED_EXT,
80 UISERVC_EXT,
81 FORM_EDIT_EXT,
82
83 WANTAGENT = DEFAULT + 0x50, // 0xD001350
84 AUTOFILLMGR,
85 EXTMGR,
86 SER_ROUTER,
87 AUTO_STARTUP,
88 STARTUP,
89 RECOVERY,
90 PROCESSMGR,
91 CONTINUATION,
92 DISTRIBUTED,
93 FREE_INSTALL,
94 KEEP_ALIVE,
95 QUERY_ERMS,
96
97 LOCAL_CALL = DEFAULT + 0x60, // 0xD001360
98
99 END = 256, // N.B. never use it
100 };
101
GetOffset(AAFwkLogTag tag,AAFwkLogTag base)102 inline uint32_t GetOffset(AAFwkLogTag tag, AAFwkLogTag base)
103 {
104 return static_cast<uint32_t>(tag) - static_cast<uint32_t>(base);
105 }
106
GetDomainName0(AAFwkLogTag tag)107 inline const char* GetDomainName0(AAFwkLogTag tag)
108 {
109 const char* tagNames[] = { "AAFwk", "Ability", "Test", "AATool", "Simulator" };
110 uint32_t offset = GetOffset(tag, AAFwkLogTag::DEFAULT);
111 if (offset >= sizeof(tagNames) / sizeof(const char*)) {
112 return "UN";
113 }
114 return tagNames[offset];
115 }
116
GetDomainName1(AAFwkLogTag tag)117 inline const char* GetDomainName1(AAFwkLogTag tag)
118 {
119 const char* tagNames[] = { "AppDfr", "AppMS", "DbObsMgr", "Dialog", "Quickfix",
120 "UriPermMgr", "BMSHelper", "AppKit" };
121 uint32_t offset = GetOffset(tag, AAFwkLogTag::APPDFR);
122 if (offset >= sizeof(tagNames) / sizeof(const char*)) {
123 return "UN";
124 }
125 return tagNames[offset];
126 }
127
GetDomainName2(AAFwkLogTag tag)128 inline const char* GetDomainName2(AAFwkLogTag tag)
129 {
130 const char* tagNames[] = { "JsEnv", "JsRuntime", "FA", "Intent", "JsNapi" };
131 uint32_t offset = GetOffset(tag, AAFwkLogTag::JSENV);
132 if (offset >= sizeof(tagNames) / sizeof(const char*)) {
133 return "UN";
134 }
135 return tagNames[offset];
136 }
137
GetDomainName3(AAFwkLogTag tag)138 inline const char* GetDomainName3(AAFwkLogTag tag)
139 {
140 const char* tagNames[] = { "Delegator", "Context", "UIAbility", "Want", "Mission",
141 "Connection", "AMS", "EcologicalRule", "DataAbility" };
142 uint32_t offset = GetOffset(tag, AAFwkLogTag::DELEGATOR);
143 if (offset >= sizeof(tagNames) / sizeof(const char*)) {
144 return "UN";
145 }
146 return tagNames[offset];
147 }
148
GetDomainName4(AAFwkLogTag tag)149 inline const char* GetDomainName4(AAFwkLogTag tag)
150 {
151 const char* tagNames[] = { "Ext", "AutoFillExt", "ServiceExt", "FormExt", "ShareExt",
152 "UIExt", "ActionExt", "EmbeddedExt", "UIServiceExt" };
153 uint32_t offset = GetOffset(tag, AAFwkLogTag::EXT);
154 if (offset >= sizeof(tagNames) / sizeof(const char*)) {
155 return "UN";
156 }
157 return tagNames[offset];
158 }
159
GetDomainName5(AAFwkLogTag tag)160 inline const char* GetDomainName5(AAFwkLogTag tag)
161 {
162 const char* tagNames[] = { "WantAgent", "AutoFillMgr", "ExtMgr", "ServiceRouter",
163 "AutoStartup", "Startup", "Recovery", "ProcessMgr", "Continuation",
164 "Distributed", "FreeInstall", "KeepAlive", "QueryERMS" };
165 uint32_t offset = GetOffset(tag, AAFwkLogTag::WANTAGENT);
166 if (offset >= sizeof(tagNames) / sizeof(const char*)) {
167 return "UN";
168 }
169 return tagNames[offset];
170 }
171
GetDomainName6(AAFwkLogTag tag)172 inline const char* GetDomainName6(AAFwkLogTag tag)
173 {
174 const char* tagNames[] = { "LocalCall" };
175 uint32_t offset = GetOffset(tag, AAFwkLogTag::LOCAL_CALL);
176 if (offset >= sizeof(tagNames) / sizeof(const char*)) {
177 return "UN";
178 }
179 return tagNames[offset];
180 }
181
182 constexpr uint32_t BASE_DEFAULT = 0;
183 constexpr uint32_t BASE_APPDFR = 1;
184 constexpr uint32_t BASE_JSENV = 2;
185 constexpr uint32_t BASE_DELEGATOR = 3;
186 constexpr uint32_t BASE_EXT = 4;
187 constexpr uint32_t BASE_WANTAGENT = 5;
188 constexpr uint32_t BASE_LOCAL_CALL = 6;
189
GetTagInfoFromDomainId(AAFwkLogTag tag)190 static inline const char* GetTagInfoFromDomainId(AAFwkLogTag tag)
191 {
192 uint32_t offset = GetOffset(tag, AAFwkLogTag::DEFAULT);
193 uint32_t base = offset >> 4;
194 switch (base) {
195 case BASE_DEFAULT: return GetDomainName0(tag);
196 case BASE_APPDFR: return GetDomainName1(tag);
197 case BASE_JSENV: return GetDomainName2(tag);
198 case BASE_DELEGATOR: return GetDomainName3(tag);
199 case BASE_EXT: return GetDomainName4(tag);
200 case BASE_WANTAGENT: return GetDomainName5(tag);
201 case BASE_LOCAL_CALL: return GetDomainName6(tag);
202 default: return "UN";
203 }
204 }
205 } // OHOS::AAFwk
206
207 using AAFwkTag = OHOS::AAFwk::AAFwkLogTag;
208
209 #define AAFWK_PRINT_LOG(level, tag, fmt, ...) \
210 do { \
211 AAFwkTag logTag = tag; \
212 ((void)HILOG_IMPL(LOG_CORE, level, static_cast<uint32_t>(logTag), \
213 OHOS::AAFwk::GetTagInfoFromDomainId(logTag), AAFWK_FUNC_FMT fmt, AAFWK_FUNC_INFO, ##__VA_ARGS__)); \
214 } while (0)
215
216 #define TAG_LOGD(tag, fmt, ...) AAFWK_PRINT_LOG(LOG_DEBUG, tag, fmt, ##__VA_ARGS__)
217 #define TAG_LOGI(tag, fmt, ...) AAFWK_PRINT_LOG(LOG_INFO, tag, fmt, ##__VA_ARGS__)
218 #define TAG_LOGW(tag, fmt, ...) AAFWK_PRINT_LOG(LOG_WARN, tag, fmt, ##__VA_ARGS__)
219 #define TAG_LOGE(tag, fmt, ...) AAFWK_PRINT_LOG(LOG_ERROR, tag, fmt, ##__VA_ARGS__)
220 #define TAG_LOGF(tag, fmt, ...) AAFWK_PRINT_LOG(LOG_FATAL, tag, fmt, ##__VA_ARGS__)
221
222 #endif // OHOS_AAFWK_HILOG_TAG_WRAPPER_H
223