Enable debugger to print log on Android

related issue: https://gitee.com/openharmony/arkui_ace_engine/issues/I5QBV6

Signed-off-by: panzhenyu1 <panzhenyu1@huawei.com>
Change-Id: I835bda3fbfcd4df78278bbc6c614cb3653f59910
This commit is contained in:
panzhenyu1 2022-09-08 17:07:18 +08:00
parent c159788e3e
commit 1f71255ebf
6 changed files with 96 additions and 6 deletions

View File

@ -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" ]

View File

@ -20,7 +20,7 @@
#include <thread>
#include <unordered_map>
#include "hilog_wrapper.h"
#include "log_wrapper.h"
#include "library_loader.h"
namespace OHOS::ArkCompiler::Toolchain {

View File

@ -15,7 +15,7 @@
#include "library_loader.h"
#include "hilog_wrapper.h"
#include "log_wrapper.h"
#if defined(UNIX_PLATFORM)
#include <dlfcn.h>

51
inspector/log_wrapper.cpp Normal file
View File

@ -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 <android/log.h>
#include <string>
#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<int>(level), tag, formatted.c_str(), args);
va_end(args);
}
#endif
} // namespace OHOS::ArkCompiler::Toolchain

View File

@ -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

View File

@ -19,7 +19,7 @@
#include <iostream>
#include <sys/types.h>
#include "hilog_wrapper.h"
#include "log_wrapper.h"
namespace OHOS::ArkCompiler::Toolchain {
void WsServer::RunServer()