From 981251d54145d5c6db1d445f29a71098e9aa90cb Mon Sep 17 00:00:00 2001 From: xuanyue Date: Mon, 10 Aug 2020 10:52:08 +0800 Subject: [PATCH] add android log print method --- mindspore/lite/CMakeLists.txt | 2 +- mindspore/lite/src/CMakeLists.txt | 6 +- mindspore/lite/src/common/log_adapter.cc | 160 +++++++++++++++++++++++ 3 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 mindspore/lite/src/common/log_adapter.cc diff --git a/mindspore/lite/CMakeLists.txt b/mindspore/lite/CMakeLists.txt index b11adfbbb0..1174764d69 100644 --- a/mindspore/lite/CMakeLists.txt +++ b/mindspore/lite/CMakeLists.txt @@ -68,7 +68,7 @@ set(ANF_SRC ${CCSRC_DIR}/gvar/logging_level.cc ${CCSRC_DIR}/gvar/typeid_manager.cc ${CMAKE_CURRENT_SOURCE_DIR}/../core/base/base.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../core/utils/log_adapter.cc + ${CMAKE_CURRENT_SOURCE_DIR}/src/common/log_adapter.cc ) if (BUILD_CONVERTER) if (PLATFORM_ARM64 OR PLATFORM_ARM32) diff --git a/mindspore/lite/src/CMakeLists.txt b/mindspore/lite/src/CMakeLists.txt index 8b96e731ab..12e532e217 100644 --- a/mindspore/lite/src/CMakeLists.txt +++ b/mindspore/lite/src/CMakeLists.txt @@ -78,11 +78,11 @@ target_link_libraries(mindspore-lite ) add_subdirectory(runtime/kernel/arm) +if (PLATFORM_ARM32 OR PLATFORM_ARM64) + target_link_libraries(mindspore-lite log) +endif() if (BUILD_MINDDATA) target_link_libraries(mindspore-lite minddata-eager minddata-lite) - if (PLATFORM_ARM32 OR PLATFORM_ARM64) - target_link_libraries(mindspore-lite log) - endif() endif () add_subdirectory(ops) diff --git a/mindspore/lite/src/common/log_adapter.cc b/mindspore/lite/src/common/log_adapter.cc new file mode 100644 index 0000000000..5529ff8a8d --- /dev/null +++ b/mindspore/lite/src/common/log_adapter.cc @@ -0,0 +1,160 @@ +/** + * Copyright 2019 Huawei Technologies 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 "utils/log_adapter.h" + +#include +#include +#include +#include +#ifdef ENABLE_ARM +#include +#endif + +// namespace to support utils module definitionnamespace mindspore constexpr const char *ANDROID_LOG_TAG = "MS_LITE"; +namespace mindspore { +constexpr const char *ANDROID_LOG_TAG = "MS_LITE"; + +int EnvToInt(const char *env) { + if (env == nullptr) + return -1; + if (strcmp(env, "DEBUG") == 0) + return 0; + if (strcmp(env, "INFO") == 0) + return 1; + if (strcmp(env, "WARNING") == 0) + return 2; + if (strcmp(env, "ERROR") == 0) + return 3; + return -1; +} + +bool IsPrint(int level) { + static const char *env = std::getenv("MSLOG"); + static int ms_level = EnvToInt(env); + if (ms_level < 0) { + ms_level = 1; + printf("please set env MSLOG DEBUG/INFO/WARNING/ERROR\n"); + printf("the default is INFO\n"); + } + return level >= ms_level; +} + +#ifdef ENABLE_ARM +// convert MsLogLevel to corresponding android level +static int GetAndroidLogLevel(MsLogLevel level) { + switch (level) { + case DEBUG: + return ANDROID_LOG_DEBUG; + case INFO: + return ANDROID_LOG_INFO; + case WARNING: + return ANDROID_LOG_WARN; + case ERROR: + default: + return ANDROID_LOG_ERROR; + } +} +#endif + +const char *EnumStrForMsLogLevel(MsLogLevel level) { + if (level == DEBUG) { + return "DEBUG"; + } else if (level == INFO) { + return "INFO"; + } else if (level == WARNING) { + return "WARNING"; + } else if (level == ERROR) { + return "ERROR"; + } else if (level == EXCEPTION) { + return "EXCEPTION"; + } else { + return "NO_LEVEL"; + } +} + +static std::string ExceptionTypeToString(ExceptionType type) { +#define _TO_STRING(x) #x + // clang-format off + static const char *const type_names[] = { + _TO_STRING(NoExceptionType), + _TO_STRING(UnknownError), + _TO_STRING(ArgumentError), + _TO_STRING(NotSupportError), + _TO_STRING(NotExistsError), + _TO_STRING(AlreadyExistsError), + _TO_STRING(UnavailableError), + _TO_STRING(DeviceProcessError), + _TO_STRING(AbortedError), + _TO_STRING(TimeOutError), + _TO_STRING(ResourceUnavailable), + _TO_STRING(NoPermissionError), + _TO_STRING(IndexError), + _TO_STRING(ValueError), + _TO_STRING(TypeError), + _TO_STRING(AttributeError), + }; + // clang-format on +#undef _TO_STRING + if (type < UnknownError || type > AttributeError) { + type = UnknownError; + } + return std::string(type_names[type]); +} + +void LogWriter::OutputLog(const std::ostringstream &msg) const { +if (IsPrint(log_level_)) { +// #ifdef USE_ANDROID_LOG +#ifdef ENABLE_ARM + __android_log_print(GetAndroidLogLevel(log_level_), ANDROID_LOG_TAG, "[%s:%d] %s] %s", location_.file_, + location_.line_, location_.func_, msg.str().c_str()); +#else + printf("%s [%s:%d] %s] %s\n:", EnumStrForMsLogLevel(log_level_), location_.file_, location_.line_, location_.func_, + msg.str().c_str()); +#endif +} +} + +void LogWriter::operator<(const LogStream &stream) const noexcept { + std::ostringstream msg; + msg << stream.sstream_->rdbuf(); + OutputLog(msg); +} + +void LogWriter::operator^(const LogStream &stream) const { + std::ostringstream msg; + msg << stream.sstream_->rdbuf(); + OutputLog(msg); + + std::ostringstream oss; + oss << location_.file_ << ":" << location_.line_ << " " << location_.func_ << "] "; + if (exception_type_ != NoExceptionType && exception_type_ != IndexError && exception_type_ != TypeError && + exception_type_ != ValueError && exception_type_ != AttributeError) { + oss << ExceptionTypeToString(exception_type_) << " "; + } + oss << msg.str(); + + if (trace_provider_ != nullptr) { + trace_provider_(oss); + } + + if (exception_handler_ != nullptr) { + exception_handler_(exception_type_, oss.str()); + } + throw std::runtime_error(oss.str()); +} +} // namespace mindspore +