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 
82     WANTAGENT = DEFAULT + 0x50, // 0xD001350
83     AUTOFILLMGR,
84     EXTMGR,
85     SER_ROUTER,
86     AUTO_STARTUP,
87     STARTUP,
88     RECOVERY,
89     PROCESSMGR,
90     CONTINUATION,
91     DISTRIBUTED,
92     FREE_INSTALL,
93     KEEP_ALIVE,
94 
95     LOCAL_CALL = DEFAULT + 0x60, // 0xD001360
96     FILE_MONITOR,
97 
98     END = 256,               // N.B. never use it
99 };
100 
GetOffset(AAFwkLogTag tag,AAFwkLogTag base)101 inline uint32_t GetOffset(AAFwkLogTag tag, AAFwkLogTag base)
102 {
103     return static_cast<uint32_t>(tag) - static_cast<uint32_t>(base);
104 }
105 
GetDomainName0(AAFwkLogTag tag)106 inline const char* GetDomainName0(AAFwkLogTag tag)
107 {
108     const char* tagNames[] = { "AAFwk", "Ability", "Test", "AATool", "Simulator" };
109     uint32_t offset = GetOffset(tag, AAFwkLogTag::DEFAULT);
110     if (offset >= sizeof(tagNames) / sizeof(const char*)) {
111         return "UN";
112     }
113     return tagNames[offset];
114 }
115 
GetDomainName1(AAFwkLogTag tag)116 inline const char* GetDomainName1(AAFwkLogTag tag)
117 {
118     const char* tagNames[] = { "AppDfr", "AppMS", "DbObsMgr", "Dialog", "Quickfix",
119         "UriPermMgr", "BMSHelper", "AppKit" };
120     uint32_t offset = GetOffset(tag, AAFwkLogTag::APPDFR);
121     if (offset >= sizeof(tagNames) / sizeof(const char*)) {
122         return "UN";
123     }
124     return tagNames[offset];
125 }
126 
GetDomainName2(AAFwkLogTag tag)127 inline const char* GetDomainName2(AAFwkLogTag tag)
128 {
129     const char* tagNames[] = { "JsEnv", "JsRuntime", "FA", "Intent", "JsNapi" };
130     uint32_t offset = GetOffset(tag, AAFwkLogTag::JSENV);
131     if (offset >= sizeof(tagNames) / sizeof(const char*)) {
132         return "UN";
133     }
134     return tagNames[offset];
135 }
136 
GetDomainName3(AAFwkLogTag tag)137 inline const char* GetDomainName3(AAFwkLogTag tag)
138 {
139     const char* tagNames[] = { "Delegator", "Context", "UIAbility", "Want", "Mission",
140         "Connection", "AMS", "EcologicalRule", "DataAbility" };
141     uint32_t offset = GetOffset(tag, AAFwkLogTag::DELEGATOR);
142     if (offset >= sizeof(tagNames) / sizeof(const char*)) {
143         return "UN";
144     }
145     return tagNames[offset];
146 }
147 
GetDomainName4(AAFwkLogTag tag)148 inline const char* GetDomainName4(AAFwkLogTag tag)
149 {
150     const char* tagNames[] = { "Ext", "AutoFillExt", "ServiceExt", "FormExt", "ShareExt",
151         "UIExt", "ActionExt", "EmbeddedExt", "UIServiceExt" };
152     uint32_t offset = GetOffset(tag, AAFwkLogTag::EXT);
153     if (offset >= sizeof(tagNames) / sizeof(const char*)) {
154         return "UN";
155     }
156     return tagNames[offset];
157 }
158 
GetDomainName5(AAFwkLogTag tag)159 inline const char* GetDomainName5(AAFwkLogTag tag)
160 {
161     const char* tagNames[] = { "WantAgent", "AutoFillMgr", "ExtMgr", "ServiceRouter",
162         "AutoStartup", "Startup", "Recovery", "ProcessMgr", "Continuation",
163         "Distributed", "FreeInstall", "KeepAlive" };
164     uint32_t offset = GetOffset(tag, AAFwkLogTag::WANTAGENT);
165     if (offset >= sizeof(tagNames) / sizeof(const char*)) {
166         return "UN";
167     }
168     return tagNames[offset];
169 }
170 
GetDomainName6(AAFwkLogTag tag)171 inline const char* GetDomainName6(AAFwkLogTag tag)
172 {
173     const char* tagNames[] = { "LocalCall", "FileMonitor" };
174     uint32_t offset = GetOffset(tag, AAFwkLogTag::LOCAL_CALL);
175     if (offset >= sizeof(tagNames) / sizeof(const char*)) {
176         return "UN";
177     }
178     return tagNames[offset];
179 }
180 
181 constexpr uint32_t BASE_DEFAULT = 0;
182 constexpr uint32_t BASE_APPDFR = 1;
183 constexpr uint32_t BASE_JSENV = 2;
184 constexpr uint32_t BASE_DELEGATOR = 3;
185 constexpr uint32_t BASE_EXT = 4;
186 constexpr uint32_t BASE_WANTAGENT = 5;
187 constexpr uint32_t BASE_LOCAL_CALL = 6;
188 
GetTagInfoFromDomainId(AAFwkLogTag tag)189 static inline const char* GetTagInfoFromDomainId(AAFwkLogTag tag)
190 {
191     uint32_t offset = GetOffset(tag, AAFwkLogTag::DEFAULT);
192     uint32_t base = offset >> 4;
193     switch (base) {
194         case BASE_DEFAULT: return GetDomainName0(tag);
195         case BASE_APPDFR: return GetDomainName1(tag);
196         case BASE_JSENV: return GetDomainName2(tag);
197         case BASE_DELEGATOR: return GetDomainName3(tag);
198         case BASE_EXT: return GetDomainName4(tag);
199         case BASE_WANTAGENT: return GetDomainName5(tag);
200         case BASE_LOCAL_CALL: return GetDomainName6(tag);
201         default: return "UN";
202     }
203 }
204 } // OHOS::AAFwk
205 
206 using AAFwkTag = OHOS::AAFwk::AAFwkLogTag;
207 
208 #define AAFWK_PRINT_LOG(level, tag, fmt, ...)                                                           \
209     do {                                                                                                \
210         AAFwkTag logTag = tag;                                                                          \
211         ((void)HILOG_IMPL(LOG_CORE, level, static_cast<uint32_t>(logTag),                                  \
212         OHOS::AAFwk::GetTagInfoFromDomainId(logTag), AAFWK_FUNC_FMT fmt, AAFWK_FUNC_INFO, ##__VA_ARGS__)); \
213     } while (0)
214 
215 #define TAG_LOGD(tag, fmt, ...) AAFWK_PRINT_LOG(LOG_DEBUG, tag, fmt, ##__VA_ARGS__)
216 #define TAG_LOGI(tag, fmt, ...) AAFWK_PRINT_LOG(LOG_INFO,  tag, fmt, ##__VA_ARGS__)
217 #define TAG_LOGW(tag, fmt, ...) AAFWK_PRINT_LOG(LOG_WARN,  tag, fmt, ##__VA_ARGS__)
218 #define TAG_LOGE(tag, fmt, ...) AAFWK_PRINT_LOG(LOG_ERROR, tag, fmt, ##__VA_ARGS__)
219 #define TAG_LOGF(tag, fmt, ...) AAFWK_PRINT_LOG(LOG_FATAL, tag, fmt, ##__VA_ARGS__)
220 
221 #endif  // OHOS_AAFWK_HILOG_TAG_WRAPPER_H
222