1 /*
2 * Copyright (c) 2021 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 #include "base/log/log_wrapper.h"
17
18 #include <cstring>
19 #include <map>
20 #include <unordered_map>
21
22 #include "hilog/log.h"
23
24 #ifdef ACE_INSTANCE_LOG
25 #include "core/common/container_scope.h"
26 #endif
27
28 extern "C" {
29 int HiLogPrintArgs(LogType type, LogLevel level, unsigned int domain, const char* tag, const char* fmt, va_list ap);
30 }
31
32 namespace OHOS::Ace {
33
34 namespace {
35
36 const ::LogLevel LOG_LEVELS[] = {
37 LOG_DEBUG,
38 LOG_INFO,
39 LOG_WARN,
40 LOG_ERROR,
41 LOG_FATAL,
42 };
43
44 const std::map<AceLogTag, const char*> DOMAIN_CONTENTS_MAP = {
45 { AceLogTag::DEFAULT, "Ace" },
46 { AceLogTag::ACE_ALPHABET_INDEXER, "AceAlphabetIndexer" },
47 { AceLogTag::ACE_COUNTER, "AceCounter" },
48 { AceLogTag::ACE_SUB_WINDOW, "AceSubWindow" },
49 { AceLogTag::ACE_FORM, "AceForm" },
50 { AceLogTag::ACE_DRAG, "AceDrag" },
51 { AceLogTag::ACE_VIDEO, "AceVideo" },
52 { AceLogTag::ACE_COMPONENT_SNAPSHOT, "AceComponentSnapshot" },
53 { AceLogTag::ACE_CANVAS, "AceCanvas" },
54 { AceLogTag::ACE_REFRESH, "AceRefresh" },
55 { AceLogTag::ACE_SCROLL, "AceScroll" },
56 { AceLogTag::ACE_SCROLLABLE, "AceScrollable" },
57 { AceLogTag::ACE_FONT, "AceFont" },
58 { AceLogTag::ACE_OVERLAY, "AceOverlay" },
59 { AceLogTag::ACE_DIALOG_TIMEPICKER, "AceDialogTimePicker" },
60 { AceLogTag::ACE_DIALOG, "AceDialog" },
61 { AceLogTag::ACE_PANEL, "AcePanel" },
62 { AceLogTag::ACE_MENU, "AceMenu" },
63 { AceLogTag::ACE_TEXTINPUT, "AceTextInput" },
64 { AceLogTag::ACE_TEXT, "AceText" },
65 { AceLogTag::ACE_TEXT_FIELD, "AceTextField" },
66 { AceLogTag::ACE_SWIPER, "AceSwiper" },
67 { AceLogTag::ACE_TABS, "AceTabs" },
68 { AceLogTag::ACE_BLANK, "AceBlank" },
69 { AceLogTag::ACE_GRIDROW, "AceGridRow" },
70 { AceLogTag::ACE_INPUTTRACKING, "AceInputTracking" },
71 { AceLogTag::ACE_RICH_TEXT, "AceRichText" },
72 { AceLogTag::ACE_WEB, "AceWeb" },
73 { AceLogTag::ACE_FOCUS, "AceFocus" },
74 { AceLogTag::ACE_MOUSE, "AceMouse" },
75 { AceLogTag::ACE_GESTURE, "AceGesture" },
76 { AceLogTag::ACE_IMAGE, "AceImage" },
77 { AceLogTag::ACE_RATING, "AceRating" },
78 { AceLogTag::ACE_LIST, "AceList" },
79 { AceLogTag::ACE_NAVIGATION, "AceNavigation" },
80 { AceLogTag::ACE_WATERFLOW, "AceWaterFlow" },
81 { AceLogTag::ACE_ACCESSIBILITY, "AceAccessibility" },
82 { AceLogTag::ACE_ROUTER, "AceRouter" },
83 { AceLogTag::ACE_THEME, "AceTheme" },
84 { AceLogTag::ACE_BORDER_IMAGE, "AceBorderImage" },
85 { AceLogTag::ACE_GRID, "AceGrid" },
86 { AceLogTag::ACE_PLUGIN_COMPONENT, "AcePluginComponent" },
87 { AceLogTag::ACE_UIEXTENSIONCOMPONENT, "AceUiExtensionComponent" },
88 { AceLogTag::ACE_IF, "AceIf" },
89 { AceLogTag::ACE_FOREACH, "AceForEach" },
90 { AceLogTag::ACE_LAZY_FOREACH, "AceLazyForEach" },
91 { AceLogTag::ACE_GAUGE, "AceGauge" },
92 { AceLogTag::ACE_HYPERLINK, "AceHyperLink" },
93 { AceLogTag::ACE_ANIMATION, "AceAnimation" },
94 { AceLogTag::ACE_XCOMPONENT, "AceXcomponent" },
95 { AceLogTag::ACE_AUTO_FILL, "AceAutoFill" },
96 { AceLogTag::ACE_KEYBOARD, "AceKeyboard" },
97 { AceLogTag::ACE_UIEVENT, "AceUIEvent" },
98 { AceLogTag::ACE_UI_SERVICE, "AceUIService" },
99 { AceLogTag::ACE_DISPLAY_SYNC, "AceDisplaySync" },
100 { AceLogTag::ACE_RESOURCE, "AceResource" },
101 { AceLogTag::ACE_SIDEBAR, "AceSideBarContainer" },
102 { AceLogTag::ACE_GEOMETRY_TRANSITION, "AceGeometryTransition" },
103 { AceLogTag::ACE_DOWNLOAD_MANAGER, "DownloadManager" },
104 { AceLogTag::ACE_WINDOW_SCENE, "AceWindowScene" },
105 { AceLogTag::ACE_NODE_CONTAINER, "AceNodeContainer" },
106 { AceLogTag::ACE_NATIVE_NODE, "AceNativeNode" },
107 { AceLogTag::ACE_DYNAMIC_COMPONENT, "AceDynamicComponent" },
108 { AceLogTag::ACE_MARQUEE, "AceMarquee" },
109 { AceLogTag::ACE_OBSERVER, "AceObserver" },
110 { AceLogTag::ACE_EMBEDDED_COMPONENT, "AceEmbeddedComponent" },
111 { AceLogTag::ACE_TEXT_CLOCK, "AceTextClock" },
112 { AceLogTag::ACE_FOLDER_STACK, "AceFolderStack" },
113 };
114
115 const char* APP_DOMAIN_CONTENT = "JSApp";
116
117 constexpr uint32_t LOG_DOMAINS[] = {
118 0xD003900,
119 0xC0D0,
120 };
121
122 constexpr LogType LOG_TYPES[] = {
123 LOG_CORE,
124 LOG_APP,
125 };
126
127 #ifdef ACE_INSTANCE_LOG
128 constexpr const char* INSTANCE_ID_GEN_REASONS[] = {
129 "scope",
130 "active",
131 "default",
132 "singleton",
133 "foreground",
134 "undefined",
135 };
136 #endif
137
138 } // namespace
139
140 // initial static member object
141 LogLevel LogWrapper::level_ = LogLevel::DEBUG;
142
GetSeparatorCharacter()143 char LogWrapper::GetSeparatorCharacter()
144 {
145 return '/';
146 }
147
PrintLog(LogDomain domain,LogLevel level,AceLogTag tag,const char * fmt,va_list args)148 void LogWrapper::PrintLog(LogDomain domain, LogLevel level, AceLogTag tag, const char* fmt, va_list args)
149 {
150 uint32_t hilogDomain = LOG_DOMAINS[static_cast<uint32_t>(domain)] + static_cast<uint32_t>(tag);
151 const char* domainContent = domain == LogDomain::FRAMEWORK ? DOMAIN_CONTENTS_MAP.at(tag) : APP_DOMAIN_CONTENT;
152 #ifdef ACE_PRIVATE_LOG
153 std::string newFmt(fmt);
154 ReplaceFormatString("{private}", "{public}", newFmt);
155 HiLogPrintArgs(LOG_TYPES[static_cast<uint32_t>(domain)], LOG_LEVELS[static_cast<uint32_t>(level)],
156 hilogDomain, domainContent, newFmt.c_str(), args);
157 #else
158 HiLogPrintArgs(LOG_TYPES[static_cast<uint32_t>(domain)], LOG_LEVELS[static_cast<uint32_t>(level)],
159 hilogDomain, domainContent, fmt, args);
160 #endif
161 }
162
163 #ifdef ACE_INSTANCE_LOG
GetId()164 int32_t LogWrapper::GetId()
165 {
166 return ContainerScope::CurrentId();
167 }
168
GetIdWithReason()169 const std::string LogWrapper::GetIdWithReason()
170 {
171 int32_t currentId = ContainerScope::CurrentId();
172 std::pair<int32_t, InstanceIdGenReason> idWithReason = ContainerScope::CurrentIdWithReason();
173 return std::to_string(currentId) + ":" + std::to_string(idWithReason.first) + ":" +
174 INSTANCE_ID_GEN_REASONS[static_cast<uint32_t>(idWithReason.second)];
175 }
176 #endif
177
178 } // namespace OHOS::Ace
179