From 059221a7ded4c50447cf6d8efb89e62928ce3b34 Mon Sep 17 00:00:00 2001 From: alexander-g <3867427+alexander-g@users.noreply.github.com> Date: Sun, 24 Jan 2021 12:12:51 +0100 Subject: [PATCH] native logging for python --- CMakeLists.txt | 5 +- python/src/main.cpp | 14 ++++- setup.py | 2 +- src/CMakeLists.txt | 9 +++ src/include/kompute/Core.hpp | 119 +++++++++++++++++++++-------------- 5 files changed, 98 insertions(+), 51 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7be7a3f..18a2b7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,10 @@ if(KOMPUTE_OPT_ANDOID_BUILD) set(KOMPUTE_EXTRA_CXX_FLAGS "${KOMPUTE_EXTRA_CXX_FLAGS} -DVK_USE_PLATFORM_ANDROID_KHR") endif() +if(KOMPUTE_OPT_BUILD_PYTHON) + set(KOMPUTE_EXTRA_CXX_FLAGS "${KOMPUTE_EXTRA_CXX_FLAGS} -DKOMPUTE_BUILD_PYTHON") +endif() + if(KOMPUTE_OPT_DISABLE_VK_DEBUG_LAYERS) set(KOMPUTE_EXTRA_CXX_FLAGS "${KOMPUTE_EXTRA_CXX_FLAGS} -DKOMPUTE_DISABLE_VK_DEBUG_LAYERS=1") endif() @@ -57,4 +61,3 @@ endif() if(KOMPUTE_OPT_BUILD_PYTHON) add_subdirectory(python) endif() - diff --git a/python/src/main.cpp b/python/src/main.cpp index 8f653b6..bbb24a7 100644 --- a/python/src/main.cpp +++ b/python/src/main.cpp @@ -2,13 +2,25 @@ #include #include -#include +#include +#include +#include +#include +#include +#include +#include +#include #include "docstrings.hpp" namespace py = pybind11; +//used in Core.hpp +py::object kp_logger; PYBIND11_MODULE(kp, m) { + py::module_ logging = py::module_::import("logging"); + kp_logger = logging.attr("getLogger")("kp"); + logging.attr("basicConfig")(); py::module_ np = py::module_::import("numpy"); diff --git a/setup.py b/setup.py index 1896b8f..3b37507 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,7 @@ class CMakeBuild(build_ext): cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir, '-DKOMPUTE_OPT_BUILD_PYTHON=1', - '-DKOMPUTE_OPT_ENABLE_SPDLOG=1', + '-DKOMPUTE_OPT_ENABLE_SPDLOG=0', '-DKOMPUTE_OPT_REPO_SUBMODULE_BUILD=1', '-DPYTHON_EXECUTABLE=' + sys.executable] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 348c053..169cde9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -124,3 +124,12 @@ if(KOMPUTE_OPT_INSTALL) DESTINATION lib/cmake/kompute) endif() +if(KOMPUTE_OPT_BUILD_PYTHON) + include_directories(${PROJECT_SOURCE_DIR}/python/pybind11/include) + if (WIN32) + execute_process(COMMAND CMD /c python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())" OUTPUT_VARIABLE PYTHON_INCLUDE_DIR) + else() + execute_process(COMMAND python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())" OUTPUT_VARIABLE PYTHON_INCLUDE_DIR) + endif() + include_directories(${PYTHON_INCLUDE_DIR}) +endif() diff --git a/src/include/kompute/Core.hpp b/src/include/kompute/Core.hpp index 73d6bc8..e048437 100644 --- a/src/include/kompute/Core.hpp +++ b/src/include/kompute/Core.hpp @@ -32,53 +32,76 @@ static const char* KOMPUTE_LOG_TAG = "KomputeLog"; #endif #endif +#if defined(KOMPUTE_BUILD_PYTHON) + #include + namespace py = pybind11; + //from python/src/main.cpp + extern py::object kp_logger; +#endif + + #ifndef KOMPUTE_LOG_OVERRIDE -#if KOMPUTE_ENABLE_SPDLOG -#include -#else -#include -#if SPDLOG_ACTIVE_LEVEL > 1 -#define SPDLOG_DEBUG(message, ...) -#else -#if defined(VK_USE_PLATFORM_ANDROID_KHR) -#define SPDLOG_DEBUG(message, ...) \ - ((void)__android_log_print(ANDROID_LOG_DEBUG, KOMPUTE_LOG_TAG, message)) -#else -#define SPDLOG_DEBUG(message, ...) \ - std::cout << "DEBUG: " << message << std::endl -#endif // VK_USE_PLATFORM_ANDROID_KHR -#endif // SPDLOG_ACTIVE_LEVEL > 1 -#if SPDLOG_ACTIVE_LEVEL > 2 -#define SPDLOG_INFO(message, ...) -#else -#if defined(VK_USE_PLATFORM_ANDROID_KHR) -#define SPDLOG_INFO(message, ...) \ - ((void)__android_log_print(ANDROID_LOG_INFO, KOMPUTE_LOG_TAG, message)) -#else -#define SPDLOG_INFO(message, ...) std::cout << "INFO: " << message << std::endl -#endif // VK_USE_PLATFORM_ANDROID_KHR -#endif // SPDLOG_ACTIVE_LEVEL > 2 -#if SPDLOG_ACTIVE_LEVEL > 3 -#define SPDLOG_WARN(message, ...) -#else -#if defined(VK_USE_PLATFORM_ANDROID_KHR) -#define SPDLOG_WARN(message, ...) \ - ((void)__android_log_print(ANDROID_LOG_INFO, KOMPUTE_LOG_TAG, message)) -#else -#define SPDLOG_WARN(message, ...) \ - std::cout << "WARNING: " << message << std::endl -#endif // VK_USE_PLATFORM_ANDROID_KHR -#endif // SPDLOG_ACTIVE_LEVEL > 3 -#if SPDLOG_ACTIVE_LEVEL > 4 -#define SPDLOG_ERROR(message, ...) -#else -#if defined(VK_USE_PLATFORM_ANDROID_KHR) -#define SPDLOG_ERROR(message, ...) \ - ((void)__android_log_print(ANDROID_LOG_INFO, KOMPUTE_LOG_TAG, message)) -#else -#define SPDLOG_ERROR(message, ...) \ - std::cout << "ERROR: " << message << std::endl -#endif // VK_USE_PLATFORM_ANDROID_KHR -#endif // SPDLOG_ACTIVE_LEVEL > 4 -#endif // KOMPUTE_SPDLOG_ENABLED + #if KOMPUTE_ENABLE_SPDLOG + #include + #else + #include + #if SPDLOG_ACTIVE_LEVEL > 1 + #define SPDLOG_DEBUG(message, ...) + #else + #if defined(VK_USE_PLATFORM_ANDROID_KHR) + #define SPDLOG_DEBUG(message, ...) \ + ((void)__android_log_print(ANDROID_LOG_DEBUG, KOMPUTE_LOG_TAG, message)) + #elif defined(KOMPUTE_BUILD_PYTHON) + #define SPDLOG_DEBUG(message, ...) \ + kp_logger.attr("debug")(message); + #else + #define SPDLOG_DEBUG(message, ...) \ + std::cout << "DEBUG: " << message << std::endl + #endif // VK_USE_PLATFORM_ANDROID_KHR + #endif // SPDLOG_ACTIVE_LEVEL > 1 + + #if SPDLOG_ACTIVE_LEVEL > 2 + #define SPDLOG_INFO(message, ...) + #else + #if defined(VK_USE_PLATFORM_ANDROID_KHR) + #define SPDLOG_INFO(message, ...) \ + ((void)__android_log_print(ANDROID_LOG_INFO, KOMPUTE_LOG_TAG, message)) + #elif defined(KOMPUTE_BUILD_PYTHON) + #define SPDLOG_INFO(message, ...) \ + kp_logger.attr("info")(message); + #else + #define SPDLOG_INFO(message, ...) std::cout << "INFO: " << message << std::endl + #endif // VK_USE_PLATFORM_ANDROID_KHR + #endif // SPDLOG_ACTIVE_LEVEL > 2 + + #if SPDLOG_ACTIVE_LEVEL > 3 + #define SPDLOG_WARN(message, ...) + #else + #if defined(VK_USE_PLATFORM_ANDROID_KHR) + #define SPDLOG_WARN(message, ...) \ + ((void)__android_log_print(ANDROID_LOG_INFO, KOMPUTE_LOG_TAG, message)) + #elif defined(KOMPUTE_BUILD_PYTHON) + #define SPDLOG_WARN(message, ...) \ + kp_logger.attr("warning")(message); + #else + #define SPDLOG_WARN(message, ...) \ + std::cout << "WARNING: " << message << std::endl + #endif // VK_USE_PLATFORM_ANDROID_KHR + #endif // SPDLOG_ACTIVE_LEVEL > 3 + + #if SPDLOG_ACTIVE_LEVEL > 4 + #define SPDLOG_ERROR(message, ...) + #else + #if defined(VK_USE_PLATFORM_ANDROID_KHR) + #define SPDLOG_ERROR(message, ...) \ + ((void)__android_log_print(ANDROID_LOG_INFO, KOMPUTE_LOG_TAG, message)) + #elif defined(KOMPUTE_BUILD_PYTHON) + #define SPDLOG_ERROR(message, ...) \ + kp_logger.attr("error")(message); + #else + #define SPDLOG_ERROR(message, ...) \ + std::cout << "ERROR: " << message << std::endl + #endif // VK_USE_PLATFORM_ANDROID_KHR + #endif // SPDLOG_ACTIVE_LEVEL > 4 + #endif // KOMPUTE_SPDLOG_ENABLED #endif // KOMPUTE_LOG_OVERRIDE