Add Apple log sink. (#7820)

Add a log sink for Apple platforms. This version uses NSLog().
This commit is contained in:
Edward Chen 2021-05-27 10:03:02 -07:00 committed by GitHub
parent 45a7352622
commit 13622bae91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 146 additions and 60 deletions

View file

@ -452,7 +452,9 @@ endif()
# Mark symbols to be invisible, for macOS/iOS target only
# Due to many dependencies have different symbol visibility settings, set global compile flags here.
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS")
string(APPEND CMAKE_CXX_FLAGS " -fvisibility=hidden -fvisibility-inlines-hidden")
foreach(flags CMAKE_CXX_FLAGS CMAKE_OBJC_FLAGS CMAKE_OBJCXX_FLAGS)
string(APPEND ${flags} " -fvisibility=hidden -fvisibility-inlines-hidden")
endforeach()
endif()
#must after OpenMP settings
@ -1025,7 +1027,7 @@ function(onnxruntime_set_compile_flags target_name)
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:SHELL:--compiler-options -Wno-deprecated-copy>" "$<$<COMPILE_LANGUAGE:CXX>:-Wno-deprecated-copy>")
endif()
if(onnxruntime_USE_CUDA)
if((NVCC_HAS_STRICT_ALIASING AND "${target_name}" MATCHES "cuda") OR (HAS_STRIC_ALIASING AND NOT "${target_name}" MATCHES "cuda"))
if((NVCC_HAS_STRICT_ALIASING AND "${target_name}" MATCHES "cuda") OR (HAS_STRICT_ALIASING AND NOT "${target_name}" MATCHES "cuda"))
target_compile_options(${target_name} PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:-Wno-strict-aliasing>")
endif()
endif()
@ -1034,10 +1036,19 @@ function(onnxruntime_set_compile_flags target_name)
endforeach()
endfunction()
function(onnxruntime_add_shared_library target_name)
add_library(${target_name} SHARED ${ARGN})
function(onnxruntime_set_source_file_properties target_name)
get_target_property(srcs ${target_name} SOURCES)
# enable ARC for Objective-C/C++
set(objective_c_cc_srcs ${srcs})
list(FILTER objective_c_cc_srcs INCLUDE REGEX "\.mm?$")
set_property(SOURCE ${objective_c_cc_srcs} APPEND PROPERTY COMPILE_OPTIONS "-fobjc-arc")
endfunction()
function(onnxruntime_configure_target target_name)
target_link_directories(${target_name} PRIVATE ${onnxruntime_LINK_DIRS})
onnxruntime_set_compile_flags(${target_name})
onnxruntime_set_source_file_properties(${target_name})
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT})
if(onnxruntime_ENABLE_LTO)
set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
@ -1046,16 +1057,14 @@ function(onnxruntime_add_shared_library target_name)
endif()
endfunction()
function(onnxruntime_add_shared_library target_name)
add_library(${target_name} SHARED ${ARGN})
onnxruntime_configure_target(${target_name})
endfunction()
function(onnxruntime_add_static_library target_name)
add_library(${target_name} ${ARGN})
target_link_directories(${target_name} PRIVATE ${onnxruntime_LINK_DIRS})
onnxruntime_set_compile_flags(${target_name})
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT})
if(onnxruntime_ENABLE_LTO)
set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL TRUE)
endif()
onnxruntime_configure_target(${target_name})
endfunction()
#For plugins that are not linked into other targets but may be loaded dynamically at runtime using dlopen-like functionality.
@ -1067,30 +1076,15 @@ function(onnxruntime_add_shared_library_module target_name)
add_library(${target_name} MODULE ${ARGN})
endif()
target_link_directories(${target_name} PRIVATE ${onnxruntime_LINK_DIRS})
onnxruntime_set_compile_flags(${target_name})
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT})
if(onnxruntime_ENABLE_LTO)
set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL TRUE)
endif()
onnxruntime_configure_target(${target_name})
endfunction()
#almost the same as the above function, except the first line of the body
function(onnxruntime_add_executable target_name)
if(${CMAKE_SYSTEM_NAME} MATCHES "iOSCross")
message(FATAL_ERROR "iOS doesn't support commmand line tool")
endif()
add_executable(${target_name} ${ARGN})
target_link_directories(${target_name} PRIVATE ${onnxruntime_LINK_DIRS})
onnxruntime_set_compile_flags(${target_name})
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${ONNXRUNTIME_ROOT})
if(onnxruntime_ENABLE_LTO)
set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
set_target_properties(${target_name} PROPERTIES INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL TRUE)
endif()
onnxruntime_configure_target(${target_name})
endfunction()
function(onnxruntime_add_include_to_target dst_target)

View file

@ -11,7 +11,6 @@ set(onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/common/logging/*.cc"
"${ONNXRUNTIME_ROOT}/core/common/logging/sinks/*.h"
"${ONNXRUNTIME_ROOT}/core/common/logging/sinks/*.cc"
"${ONNXRUNTIME_ROOT}/core/inc/*.h"
"${ONNXRUNTIME_ROOT}/core/platform/env.h"
"${ONNXRUNTIME_ROOT}/core/platform/env.cc"
"${ONNXRUNTIME_ROOT}/core/platform/env_time.h"
@ -21,6 +20,8 @@ set(onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/scoped_resource.h"
"${ONNXRUNTIME_ROOT}/core/platform/telemetry.h"
"${ONNXRUNTIME_ROOT}/core/platform/telemetry.cc"
"${ONNXRUNTIME_ROOT}/core/platform/logging/make_platform_default_log_sink.h"
"${ONNXRUNTIME_ROOT}/core/platform/logging/make_platform_default_log_sink.cc"
)
if(WIN32)
@ -53,6 +54,13 @@ else()
"${ONNXRUNTIME_ROOT}/core/platform/android/logging/*.cc"
)
endif()
if (APPLE)
list(APPEND onnxruntime_common_src_patterns
"${ONNXRUNTIME_ROOT}/core/platform/apple/logging/*.h"
"${ONNXRUNTIME_ROOT}/core/platform/apple/logging/*.mm"
)
endif()
endif()
if(CMAKE_GENERATOR_PLATFORM)
@ -164,3 +172,7 @@ endif()
if (onnxruntime_LINK_LIBATOMIC)
list(APPEND onnxruntime_EXTERNAL_LIBRARIES atomic)
endif()
if(APPLE)
target_link_libraries(onnxruntime_common "-framework Foundation")
endif()

View file

@ -5,18 +5,22 @@ if(${CMAKE_VERSION} VERSION_LESS "3.18")
message(FATAL_ERROR "CMake 3.18+ is required when building the Objective-C API.")
endif()
if(NOT APPLE)
message(FATAL_ERROR "Objective-C API must be built on an Apple platform.")
endif()
check_language(OBJC)
if(CMAKE_OBJC_COMPILER)
enable_language(OBJC)
enable_language(OBJC)
else()
message(FATAL_ERROR "Objective-C is not supported.")
message(FATAL_ERROR "Objective-C is not supported.")
endif()
check_language(OBJCXX)
if(CMAKE_OBJCXX_COMPILER)
enable_language(OBJCXX)
enable_language(OBJCXX)
else()
message(FATAL_ERROR "Objective-C++ is not supported.")
message(FATAL_ERROR "Objective-C++ is not supported.")
endif()
add_compile_options(
@ -29,8 +33,6 @@ endif()
set(OBJC_ROOT "${REPO_ROOT}/objectivec")
set(OBJC_ARC_COMPILE_OPTIONS "-fobjc-arc" "-fobjc-arc-exceptions")
# onnxruntime_objc target
# these headers are the public interface
@ -57,7 +59,7 @@ source_group(TREE "${OBJC_ROOT}" FILES
${onnxruntime_objc_srcs}
${onnxruntime_objc_common_srcs})
add_library(onnxruntime_objc SHARED
onnxruntime_add_shared_library(onnxruntime_objc
${onnxruntime_objc_headers}
${onnxruntime_objc_srcs}
${onnxruntime_objc_common_srcs})
@ -77,8 +79,6 @@ target_link_libraries(onnxruntime_objc
safeint_interface
${FOUNDATION_LIB})
target_compile_options(onnxruntime_objc PRIVATE ${OBJC_ARC_COMPILE_OPTIONS})
set_target_properties(onnxruntime_objc PROPERTIES
FRAMEWORK TRUE
VERSION "1.0.0"
@ -89,6 +89,8 @@ set_target_properties(onnxruntime_objc PROPERTIES
CXX_STANDARD 17 # TODO remove when everything else moves to 17
)
set_property(TARGET onnxruntime_objc APPEND PROPERTY COMPILE_OPTIONS "-fvisibility=default")
target_link_options(onnxruntime_objc PRIVATE "-Wl,-headerpad_max_install_names")
add_custom_command(TARGET onnxruntime_objc POST_BUILD
@ -122,12 +124,12 @@ if(onnxruntime_BUILD_UNIT_TESTS)
${onnxruntime_objc_test_srcs}
${onnxruntime_objc_common_srcs})
onnxruntime_configure_target(onnxruntime_objc_test)
target_include_directories(onnxruntime_objc_test
PRIVATE
"${OBJC_ROOT}")
target_compile_options(onnxruntime_objc_test PRIVATE ${OBJC_ARC_COMPILE_OPTIONS})
set_target_properties(onnxruntime_objc_test PROPERTIES
FOLDER "ONNXRuntimeTest")

View file

@ -721,11 +721,6 @@ if (onnxruntime_USE_COREML)
"${ONNXRUNTIME_ROOT}/core/providers/coreml/model/host_utils.mm"
)
set_source_files_properties(
${onnxruntime_providers_coreml_objcc_srcs}
COMPILE_FLAGS "${CMAKE_OBJC_FLAGS} -Xclang -x -Xclang objective-c++ -fobjc-arc"
)
set(onnxruntime_providers_coreml_cc_srcs
${onnxruntime_providers_coreml_cc_srcs_top}
${onnxruntime_providers_coreml_cc_srcs_nested}

View file

@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "core/common/logging/isink.h"
namespace onnxruntime {
namespace logging {
/**
* Log sink for Apple platforms.
*/
class AppleLogSink : public ISink {
private:
void SendImpl(const Timestamp& timestamp, const std::string& logger_id, const Capture& message) override;
};
} // namespace logging
} // namespace onnxruntime

View file

@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "core/platform/apple/logging/apple_log_sink.h"
#import <Foundation/Foundation.h>
#include <sstream>
#include "date/date.h"
namespace onnxruntime {
namespace logging {
void AppleLogSink::SendImpl(const Timestamp& timestamp, const std::string& logger_id, const Capture& message) {
using date::operator<<;
std::ostringstream msg;
msg << timestamp << " [" << message.SeverityPrefix() << ":" << message.Category() << ":" << logger_id << ", "
<< message.Location().ToString() << "] " << message.Message();
NSLog(@"%s", msg.str().c_str());
}
} // namespace logging
} // namespace onnxruntime

View file

@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "core/platform/logging/make_platform_default_log_sink.h"
#if defined(__ANDROID__)
#include "core/platform/android/logging/android_log_sink.h"
#elif defined(__APPLE__)
#include "core/platform/apple/logging/apple_log_sink.h"
#else
#include "core/common/logging/sinks/clog_sink.h"
#endif
namespace onnxruntime {
namespace logging {
std::unique_ptr<ISink> MakePlatformDefaultLogSink() {
#if defined(__ANDROID__)
return std::make_unique<AndroidLogSink>();
#elif defined(__APPLE__)
return std::make_unique<AppleLogSink>();
#else
return std::make_unique<CLogSink>();
#endif
}
} // namespace logging
} // namespace onnxruntime

View file

@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include <memory>
#include "core/common/logging/isink.h"
namespace onnxruntime {
namespace logging {
/**
* Creates a log sink that is appropriate for the current platform.
*/
std::unique_ptr<ISink> MakePlatformDefaultLogSink();
} // namespace logging
} // namespace onnxruntime

View file

@ -11,11 +11,7 @@
#include "core/session/allocator_impl.h"
#include "core/common/logging/logging.h"
#include "core/framework/provider_shutdown.h"
#ifdef __ANDROID__
#include "core/platform/android/logging/android_log_sink.h"
#else
#include "core/common/logging/sinks/clog_sink.h"
#endif
#include "core/platform/logging/make_platform_default_log_sink.h"
using namespace onnxruntime;
using namespace onnxruntime::logging;
@ -28,7 +24,7 @@ LoggingWrapper::LoggingWrapper(OrtLoggingFunction logging_function, void* logger
: logging_function_(logging_function), logger_param_(logger_param) {
}
void LoggingWrapper::SendImpl(const onnxruntime::logging::Timestamp& /*timestamp*/ /*timestamp*/, const std::string& logger_id,
void LoggingWrapper::SendImpl(const onnxruntime::logging::Timestamp& /*timestamp*/, const std::string& logger_id,
const onnxruntime::logging::Capture& message) {
std::string s = message.Location().ToString();
logging_function_(logger_param_, static_cast<OrtLoggingLevel>(message.Severity()), message.Category(),
@ -55,20 +51,16 @@ OrtEnv* OrtEnv::GetInstance(const OrtEnv::LoggingManagerConstructionInfo& lm_inf
std::string name = lm_info.logid;
if (lm_info.logging_function) {
std::unique_ptr<ISink> logger = std::make_unique<LoggingWrapper>(lm_info.logging_function,
lm_info.logger_param);
lm_info.logger_param);
lmgr.reset(new LoggingManager(std::move(logger),
static_cast<Severity>(lm_info.default_warning_level),
false,
LoggingManager::InstanceType::Default,
&name));
} else {
#ifdef __ANDROID__
ISink* sink = new AndroidLogSink();
#else
ISink* sink = new CLogSink();
#endif
auto sink = MakePlatformDefaultLogSink();
lmgr.reset(new LoggingManager(std::unique_ptr<ISink>{sink},
lmgr.reset(new LoggingManager(std::move(sink),
static_cast<Severity>(lm_info.default_warning_level),
false,
LoggingManager::InstanceType::Default,

View file

@ -18,7 +18,7 @@ class LoggingWrapper : public onnxruntime::logging::ISink {
public:
LoggingWrapper(OrtLoggingFunction logging_function, void* logger_param);
void SendImpl(const onnxruntime::logging::Timestamp& /*timestamp*/ /*timestamp*/, const std::string& logger_id,
void SendImpl(const onnxruntime::logging::Timestamp& /*timestamp*/, const std::string& logger_id,
const onnxruntime::logging::Capture& message) override;
private: