123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "base/log/log_wrapper.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#ifdef _GNU_SOURCE
1923b3eb3cSopenharmony_ci#include <dlfcn.h>
2023b3eb3cSopenharmony_ci#endif
2123b3eb3cSopenharmony_ci#include <mutex>
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_ci#ifdef ACE_INSTANCE_LOG
2423b3eb3cSopenharmony_ci#include "core/common/container_scope.h"
2523b3eb3cSopenharmony_ci#endif
2623b3eb3cSopenharmony_ci
2723b3eb3cSopenharmony_cinamespace OHOS::Ace {
2823b3eb3cSopenharmony_cinamespace {
2923b3eb3cSopenharmony_ci#ifdef ACE_INSTANCE_LOG
3023b3eb3cSopenharmony_ciconstexpr const char* INSTANCE_ID_GEN_REASONS[] = {
3123b3eb3cSopenharmony_ci    "scope",
3223b3eb3cSopenharmony_ci    "active",
3323b3eb3cSopenharmony_ci    "default",
3423b3eb3cSopenharmony_ci    "singleton",
3523b3eb3cSopenharmony_ci    "foreground",
3623b3eb3cSopenharmony_ci    "undefined",
3723b3eb3cSopenharmony_ci};
3823b3eb3cSopenharmony_ci#endif
3923b3eb3cSopenharmony_ci} // namespace
4023b3eb3cSopenharmony_ci
4123b3eb3cSopenharmony_ciconst std::unordered_map<AceLogTag, const char*> g_DOMAIN_CONTENTS_MAP = {
4223b3eb3cSopenharmony_ci    { AceLogTag::ACE_DEFAULT_DOMAIN, "Ace" },
4323b3eb3cSopenharmony_ci    { AceLogTag::ACE_ALPHABET_INDEXER, "AceAlphabetIndexer" },
4423b3eb3cSopenharmony_ci    { AceLogTag::ACE_COUNTER, "AceCounter" },
4523b3eb3cSopenharmony_ci    { AceLogTag::ACE_SUB_WINDOW, "AceSubWindow" },
4623b3eb3cSopenharmony_ci    { AceLogTag::ACE_FORM, "AceForm" },
4723b3eb3cSopenharmony_ci    { AceLogTag::ACE_DRAG, "AceDrag" },
4823b3eb3cSopenharmony_ci    { AceLogTag::ACE_VIDEO, "AceVideo" },
4923b3eb3cSopenharmony_ci    { AceLogTag::ACE_COMPONENT_SNAPSHOT, "AceComponentSnapshot" },
5023b3eb3cSopenharmony_ci    { AceLogTag::ACE_CANVAS, "AceCanvas" },
5123b3eb3cSopenharmony_ci    { AceLogTag::ACE_REFRESH, "AceRefresh" },
5223b3eb3cSopenharmony_ci    { AceLogTag::ACE_SCROLL, "AceScroll" },
5323b3eb3cSopenharmony_ci    { AceLogTag::ACE_SCROLLABLE, "AceScrollable" },
5423b3eb3cSopenharmony_ci    { AceLogTag::ACE_FONT, "AceFont" },
5523b3eb3cSopenharmony_ci    { AceLogTag::ACE_OVERLAY, "AceOverlay" },
5623b3eb3cSopenharmony_ci    { AceLogTag::ACE_DIALOG_TIMEPICKER, "AceDialogTimePicker" },
5723b3eb3cSopenharmony_ci    { AceLogTag::ACE_DIALOG, "AceDialog" },
5823b3eb3cSopenharmony_ci    { AceLogTag::ACE_PANEL, "AcePanel" },
5923b3eb3cSopenharmony_ci    { AceLogTag::ACE_MENU, "AceMenu" },
6023b3eb3cSopenharmony_ci    { AceLogTag::ACE_TEXTINPUT, "AceTextInput" },
6123b3eb3cSopenharmony_ci    { AceLogTag::ACE_TEXT, "AceText" },
6223b3eb3cSopenharmony_ci    { AceLogTag::ACE_TEXT_FIELD, "AceTextField" },
6323b3eb3cSopenharmony_ci    { AceLogTag::ACE_SWIPER, "AceSwiper" },
6423b3eb3cSopenharmony_ci    { AceLogTag::ACE_TABS, "AceTabs" },
6523b3eb3cSopenharmony_ci    { AceLogTag::ACE_BLANK, "AceBlank" },
6623b3eb3cSopenharmony_ci    { AceLogTag::ACE_GRIDROW, "AceGridRow" },
6723b3eb3cSopenharmony_ci    { AceLogTag::ACE_INPUTTRACKING, "AceInputTracking" },
6823b3eb3cSopenharmony_ci    { AceLogTag::ACE_RICH_TEXT, "AceRichText" },
6923b3eb3cSopenharmony_ci    { AceLogTag::ACE_WEB, "AceWeb" },
7023b3eb3cSopenharmony_ci    { AceLogTag::ACE_FOCUS, "AceFocus" },
7123b3eb3cSopenharmony_ci    { AceLogTag::ACE_MOUSE, "AceMouse" },
7223b3eb3cSopenharmony_ci    { AceLogTag::ACE_GESTURE, "AceGesture" },
7323b3eb3cSopenharmony_ci    { AceLogTag::ACE_IMAGE, "AceImage" },
7423b3eb3cSopenharmony_ci    { AceLogTag::ACE_RATING, "AceRating" },
7523b3eb3cSopenharmony_ci    { AceLogTag::ACE_LIST, "AceList" },
7623b3eb3cSopenharmony_ci    { AceLogTag::ACE_NAVIGATION, "AceNavigation" },
7723b3eb3cSopenharmony_ci    { AceLogTag::ACE_WATERFLOW, "AceWaterFlow" },
7823b3eb3cSopenharmony_ci    { AceLogTag::ACE_ACCESSIBILITY, "AceAccessibility" },
7923b3eb3cSopenharmony_ci    { AceLogTag::ACE_ROUTER, "AceRouter" },
8023b3eb3cSopenharmony_ci    { AceLogTag::ACE_THEME, "AceTheme" },
8123b3eb3cSopenharmony_ci    { AceLogTag::ACE_BORDER_IMAGE, "AceBorderImage" },
8223b3eb3cSopenharmony_ci    { AceLogTag::ACE_GRID, "AceGrid" },
8323b3eb3cSopenharmony_ci    { AceLogTag::ACE_PLUGIN_COMPONENT, "AcePluginComponent" },
8423b3eb3cSopenharmony_ci    { AceLogTag::ACE_UIEXTENSIONCOMPONENT, "AceUiExtensionComponent" },
8523b3eb3cSopenharmony_ci    { AceLogTag::ACE_IF, "AceIf" },
8623b3eb3cSopenharmony_ci    { AceLogTag::ACE_FOREACH, "AceForEach" },
8723b3eb3cSopenharmony_ci    { AceLogTag::ACE_LAZY_FOREACH, "AceLazyForEach" },
8823b3eb3cSopenharmony_ci    { AceLogTag::ACE_GAUGE, "AceGauge" },
8923b3eb3cSopenharmony_ci    { AceLogTag::ACE_HYPERLINK, "AceHyperLink" },
9023b3eb3cSopenharmony_ci    { AceLogTag::ACE_ANIMATION, "AceAnimation" },
9123b3eb3cSopenharmony_ci    { AceLogTag::ACE_XCOMPONENT, "AceXcomponent" },
9223b3eb3cSopenharmony_ci    { AceLogTag::ACE_AUTO_FILL, "AceAutoFill" },
9323b3eb3cSopenharmony_ci    { AceLogTag::ACE_KEYBOARD, "AceKeyboard" },
9423b3eb3cSopenharmony_ci    { AceLogTag::ACE_UIEVENT, "AceUIEvent" },
9523b3eb3cSopenharmony_ci    { AceLogTag::ACE_UI_SERVICE, "AceUIService" },
9623b3eb3cSopenharmony_ci    { AceLogTag::ACE_DISPLAY_SYNC, "AceDisplaySync" },
9723b3eb3cSopenharmony_ci    { AceLogTag::ACE_RESOURCE, "AceResource" },
9823b3eb3cSopenharmony_ci    { AceLogTag::ACE_SIDEBAR, "AceSideBarContainer" },
9923b3eb3cSopenharmony_ci    { AceLogTag::ACE_GEOMETRY_TRANSITION, "AceGeometryTransition" },
10023b3eb3cSopenharmony_ci    { AceLogTag::ACE_DOWNLOAD_MANAGER, "DownloadManager" },
10123b3eb3cSopenharmony_ci    { AceLogTag::ACE_WINDOW_SCENE, "AceWindowScene" },
10223b3eb3cSopenharmony_ci    { AceLogTag::ACE_NODE_CONTAINER, "AceNodeContainer" },
10323b3eb3cSopenharmony_ci    { AceLogTag::ACE_NATIVE_NODE, "AceNativeNode" },
10423b3eb3cSopenharmony_ci    { AceLogTag::ACE_ISOLATED_COMPONENT, "AceIsolatedComponent" },
10523b3eb3cSopenharmony_ci    { AceLogTag::ACE_SECURITYUIEXTENSION, "AceSecurityUiExtensionComponent" },
10623b3eb3cSopenharmony_ci    { AceLogTag::ACE_MARQUEE, "AceMarquee" },
10723b3eb3cSopenharmony_ci    { AceLogTag::ACE_OBSERVER, "AceObserver" },
10823b3eb3cSopenharmony_ci    { AceLogTag::ACE_EMBEDDED_COMPONENT, "AceEmbeddedComponent" },
10923b3eb3cSopenharmony_ci    { AceLogTag::ACE_TEXT_CLOCK, "AceTextClock" },
11023b3eb3cSopenharmony_ci    { AceLogTag::ACE_FOLDER_STACK, "AceFolderStack" },
11123b3eb3cSopenharmony_ci    { AceLogTag::ACE_SELECT_COMPONENT, "AceSelectComponent" },
11223b3eb3cSopenharmony_ci    { AceLogTag::ACE_STATE_STYLE, "AceStateStyle" },
11323b3eb3cSopenharmony_ci    { AceLogTag::ACE_SEARCH, "AceSearch" },
11423b3eb3cSopenharmony_ci    { AceLogTag::ACE_STATE_MGMT, "AceStateMgmt" },
11523b3eb3cSopenharmony_ci    { AceLogTag::ACE_REPEAT, "AceRepeat" },
11623b3eb3cSopenharmony_ci    { AceLogTag::ACE_SHEET, "AceSheet" },
11723b3eb3cSopenharmony_ci    { AceLogTag::ACE_CANVAS_COMPONENT, "AceCanvasComponent" },
11823b3eb3cSopenharmony_ci    { AceLogTag::ACE_SCROLL_BAR, "AceScrollBar" },
11923b3eb3cSopenharmony_ci    { AceLogTag::ACE_MOVING_PHOTO, "AceMovingPhoto" },
12023b3eb3cSopenharmony_ci    { AceLogTag::ACE_ARK_COMPONENT, "AceArkComponent" },
12123b3eb3cSopenharmony_ci    { AceLogTag::ACE_WINDOW, "AceWindow" },
12223b3eb3cSopenharmony_ci    { AceLogTag::ACE_WINDOW_PIPELINE, "AceWindowPipeline" },
12323b3eb3cSopenharmony_ci    { AceLogTag::ACE_INPUTKEYFLOW, "InputKeyFlow"},
12423b3eb3cSopenharmony_ci    { AceLogTag::ACE_APPBAR, "AceAppBar"},
12523b3eb3cSopenharmony_ci    { AceLogTag::ACE_SELECT_OVERLAY, "AceSelectOverlay"},
12623b3eb3cSopenharmony_ci    { AceLogTag::ACE_CLIPBOARD, "AceClipBoard"},
12723b3eb3cSopenharmony_ci    { AceLogTag::ACE_SECURITY_COMPONENT, "AceSecurityComponent"},
12823b3eb3cSopenharmony_ci    { AceLogTag::ACE_LAYOUT_INSPECTOR, "AceLayoutInspector" },
12923b3eb3cSopenharmony_ci    { AceLogTag::ACE_MEDIA_QUERY, "AceMediaQuery" },
13023b3eb3cSopenharmony_ci    { AceLogTag::ACE_LAYOUT, "AceLayout" },
13123b3eb3cSopenharmony_ci    { AceLogTag::ACE_STYLUS, "AceStylus"},
13223b3eb3cSopenharmony_ci};
13323b3eb3cSopenharmony_ci// initial static member object
13423b3eb3cSopenharmony_ciLogLevel LogWrapper::level_ = LogLevel::DEBUG;
13523b3eb3cSopenharmony_ci
13623b3eb3cSopenharmony_cichar LogWrapper::GetSeparatorCharacter()
13723b3eb3cSopenharmony_ci{
13823b3eb3cSopenharmony_ci    return '/';
13923b3eb3cSopenharmony_ci}
14023b3eb3cSopenharmony_ci
14123b3eb3cSopenharmony_ci#ifdef ACE_INSTANCE_LOG
14223b3eb3cSopenharmony_ciint32_t LogWrapper::GetId()
14323b3eb3cSopenharmony_ci{
14423b3eb3cSopenharmony_ci    return ContainerScope::CurrentId();
14523b3eb3cSopenharmony_ci}
14623b3eb3cSopenharmony_ci
14723b3eb3cSopenharmony_ciconst std::string LogWrapper::GetIdWithReason()
14823b3eb3cSopenharmony_ci{
14923b3eb3cSopenharmony_ci    int32_t currentId = ContainerScope::CurrentId();
15023b3eb3cSopenharmony_ci    std::pair<int32_t, InstanceIdGenReason> idWithReason = ContainerScope::CurrentIdWithReason();
15123b3eb3cSopenharmony_ci    return std::to_string(currentId) + ":" + std::to_string(idWithReason.first) + ":" +
15223b3eb3cSopenharmony_ci           INSTANCE_ID_GEN_REASONS[static_cast<uint32_t>(idWithReason.second)];
15323b3eb3cSopenharmony_ci}
15423b3eb3cSopenharmony_ci#endif
15523b3eb3cSopenharmony_ci
15623b3eb3cSopenharmony_cibool LogBacktrace(size_t maxFrameNums)
15723b3eb3cSopenharmony_ci{
15823b3eb3cSopenharmony_ci    static const char* (*pfnGetTrace)(size_t, size_t);
15923b3eb3cSopenharmony_ci#ifdef _GNU_SOURCE
16023b3eb3cSopenharmony_ci    if (!pfnGetTrace) {
16123b3eb3cSopenharmony_ci        pfnGetTrace = (decltype(pfnGetTrace))dlsym(RTLD_DEFAULT, "GetTrace");
16223b3eb3cSopenharmony_ci    }
16323b3eb3cSopenharmony_ci#endif
16423b3eb3cSopenharmony_ci    if (!pfnGetTrace) {
16523b3eb3cSopenharmony_ci        return false;
16623b3eb3cSopenharmony_ci    }
16723b3eb3cSopenharmony_ci
16823b3eb3cSopenharmony_ci    static std::mutex mtx;
16923b3eb3cSopenharmony_ci    std::lock_guard lock(mtx);
17023b3eb3cSopenharmony_ci    size_t skipFrameNum = 2;
17123b3eb3cSopenharmony_ci    LOGI("Backtrace: skipFrameNum=%{public}zu maxFrameNums=%{public}zu\n%{public}s",
17223b3eb3cSopenharmony_ci        skipFrameNum, maxFrameNums, pfnGetTrace(skipFrameNum, maxFrameNums));
17323b3eb3cSopenharmony_ci    return true;
17423b3eb3cSopenharmony_ci}
17523b3eb3cSopenharmony_ci} // namespace OHOS::Ace
176