From 1f71255ebf525c6662147e663e95ecf1062cd5dd Mon Sep 17 00:00:00 2001 From: panzhenyu1 Date: Thu, 8 Sep 2022 17:07:18 +0800 Subject: [PATCH] Enable debugger to print log on Android related issue: https://gitee.com/openharmony/arkui_ace_engine/issues/I5QBV6 Signed-off-by: panzhenyu1 Change-Id: I835bda3fbfcd4df78278bbc6c614cb3653f59910 --- inspector/BUILD.gn | 9 ++++ inspector/inspector.cpp | 2 +- inspector/library_loader.cpp | 2 +- inspector/log_wrapper.cpp | 51 ++++++++++++++++++++ inspector/{hilog_wrapper.h => log_wrapper.h} | 36 ++++++++++++-- inspector/ws_server.cpp | 2 +- 6 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 inspector/log_wrapper.cpp rename inspector/{hilog_wrapper.h => log_wrapper.h} (58%) diff --git a/inspector/BUILD.gn b/inspector/BUILD.gn index 911b8942..b2b0a511 100644 --- a/inspector/BUILD.gn +++ b/inspector/BUILD.gn @@ -53,6 +53,12 @@ ohos_shared_library("ark_debugger") { deps = [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_$platform", ] + } else if (target_os == "android") { + defines += [ + "ANDROID_PLATFORM", + "UNIX_PLATFORM", + ] + aosp_deps = [ "shared_library:liblog" ] } else { external_deps = [ "hiviewdfx_hilog_native:libhilog" ] defines += [ "UNIX_PLATFORM" ] @@ -69,6 +75,9 @@ ohos_shared_library("ark_debugger") { "library_loader.cpp", "ws_server.cpp", ] + if (target_os == "android") { + sources += [ "log_wrapper.cpp" ] + } configs = [ ":ark_debugger_config" ] diff --git a/inspector/inspector.cpp b/inspector/inspector.cpp index 45ee4023..f9652773 100644 --- a/inspector/inspector.cpp +++ b/inspector/inspector.cpp @@ -20,7 +20,7 @@ #include #include -#include "hilog_wrapper.h" +#include "log_wrapper.h" #include "library_loader.h" namespace OHOS::ArkCompiler::Toolchain { diff --git a/inspector/library_loader.cpp b/inspector/library_loader.cpp index 4b8e0cac..ab9d39d2 100644 --- a/inspector/library_loader.cpp +++ b/inspector/library_loader.cpp @@ -15,7 +15,7 @@ #include "library_loader.h" -#include "hilog_wrapper.h" +#include "log_wrapper.h" #if defined(UNIX_PLATFORM) #include diff --git a/inspector/log_wrapper.cpp b/inspector/log_wrapper.cpp new file mode 100644 index 00000000..5e24b268 --- /dev/null +++ b/inspector/log_wrapper.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "log_wrapper.h" + +#ifdef ANDROID_PLATFORM +#include +#include +#endif + +namespace OHOS::ArkCompiler::Toolchain { +#ifdef ANDROID_PLATFORM +const char* tag = "ArkCompiler"; + +void StripString(const std::string& prefix, std::string& str) +{ + for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) { + str.erase(pos, prefix.size()); + } +} + +std::string StripFormatString(const char* fmt) +{ + std::string newFmt(fmt); + StripString("{public}", newFmt); + StripString("{private}", newFmt); + return newFmt; +} + +void AndroidLog::PrintLog(LogLevel level, const char* fmt, ...) +{ + std::string formatted = StripFormatString(fmt); + va_list args; + va_start(args, fmt); + __android_log_vprint(static_cast(level), tag, formatted.c_str(), args); + va_end(args); +} +#endif +} // namespace OHOS::ArkCompiler::Toolchain \ No newline at end of file diff --git a/inspector/hilog_wrapper.h b/inspector/log_wrapper.h similarity index 58% rename from inspector/hilog_wrapper.h rename to inspector/log_wrapper.h index 910784bf..cc62b132 100644 --- a/inspector/hilog_wrapper.h +++ b/inspector/log_wrapper.h @@ -13,10 +13,12 @@ * limitations under the License. */ -#ifndef ARKCOMPILER_TOOLCHAIN_INSPECTOR_HILOG_WRAPPER_H -#define ARKCOMPILER_TOOLCHAIN_INSPECTOR_HILOG_WRAPPER_H +#ifndef ARKCOMPILER_TOOLCHAIN_INSPECTOR_LOG_WRAPPER_H +#define ARKCOMPILER_TOOLCHAIN_INSPECTOR_LOG_WRAPPER_H +#if !defined(ANDROID_PLATFORM) #include "hilog/log.h" +#endif namespace OHOS::ArkCompiler::Toolchain { #ifdef LOGF @@ -35,16 +37,44 @@ namespace OHOS::ArkCompiler::Toolchain { #undef LOGD #endif +#if defined(ANDROID_PLATFORM) +enum class LogLevel { + UNKNOWN, + DEFAULT, + VERBOSE, + DEBUG, + INFO, + WARN, + ERROR, + FATAL +}; +class AndroidLog { +public: + AndroidLog() = default; + ~AndroidLog() = default; + + static void PrintLog(LogLevel level, const char* fmt, ...); +}; +#else static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD003F00, "ArkCompiler" }; +#endif +#if defined(ANDROID_PLATFORM) +#define LOGF(fmt, ...) AndroidLog::PrintLog(LogLevel::FATAL, fmt, ##__VA_ARGS__) +#define LOGE(fmt, ...) AndroidLog::PrintLog(LogLevel::ERROR, fmt, ##__VA_ARGS__) +#define LOGW(fmt, ...) AndroidLog::PrintLog(LogLevel::WARN, fmt, ##__VA_ARGS__) +#define LOGI(fmt, ...) AndroidLog::PrintLog(LogLevel::INFO, fmt, ##__VA_ARGS__) +#define LOGD(fmt, ...) AndroidLog::PrintLog(LogLevel::DEBUG, fmt, ##__VA_ARGS__) +#else #define LOGF(fmt, ...) OHOS::HiviewDFX::HiLog::Fatal(LABEL, fmt, ##__VA_ARGS__) #define LOGE(fmt, ...) OHOS::HiviewDFX::HiLog::Error(LABEL, fmt, ##__VA_ARGS__) #define LOGW(fmt, ...) OHOS::HiviewDFX::HiLog::Warn(LABEL, fmt, ##__VA_ARGS__) #define LOGI(fmt, ...) OHOS::HiviewDFX::HiLog::Info(LABEL, fmt, ##__VA_ARGS__) #define LOGD(fmt, ...) OHOS::HiviewDFX::HiLog::Debug(LABEL, fmt, ##__VA_ARGS__) +#endif } // namespace OHOS::ArkCompiler::Toolchain -#endif // ARKCOMPILER_TOOLCHAIN_INSPECTOR_HILOG_WRAPPER_H +#endif // ARKCOMPILER_TOOLCHAIN_INSPECTOR_LOG_WRAPPER_H diff --git a/inspector/ws_server.cpp b/inspector/ws_server.cpp index f1d7b1ce..ee1ea985 100644 --- a/inspector/ws_server.cpp +++ b/inspector/ws_server.cpp @@ -19,7 +19,7 @@ #include #include -#include "hilog_wrapper.h" +#include "log_wrapper.h" namespace OHOS::ArkCompiler::Toolchain { void WsServer::RunServer() -- Gitee