From 444c8fc1db8dacdf39b187c9114a369af9135479 Mon Sep 17 00:00:00 2001 From: Fabian Sauter Date: Tue, 26 Jul 2022 10:48:57 +0200 Subject: [PATCH] Spdlog is now optional for logging Signed-off-by: Fabian Sauter --- CMakeLists.txt | 29 ++++--- src/Manager.cpp | 2 +- src/include/kompute/logger/Logger.hpp | 107 +++++++++++++++++++++++--- src/logger/CMakeLists.txt | 23 ++++-- src/logger/Logger.cpp | 82 +++----------------- 5 files changed, 144 insertions(+), 99 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 676360ef0..a25f44162 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(kompute VERSION 0.8.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) -# Only change the folder behaviour if kompute is not a subproject +# Only change the folder behavior if kompute is not a subproject if(${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}) set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMake") @@ -88,14 +88,15 @@ kompute_option(KOMPUTE_OPT_INSTALL "Enable if you want to enable installation." # Build options kompute_option(KOMPUTE_OPT_BUILD_PYTHON "Enable if you want to build python bindings." OFF) -kompute_log_level(KOMPUTE_OPT_LOG_LEVEL "Internally we use spdlog for logging. The log level used can be changed here. Possible values: 'Trace', 'Debug', 'Info', 'Warn', 'Error', 'Critical', 'Off', 'Default'. If set to 'Off' spdlog will be deactivated completely. If set to 'Default', the log level will be set to 'Info' for release builds and 'Debug' else." "Default") +kompute_log_level(KOMPUTE_OPT_LOG_LEVEL "Internally we use Spdlog or fmt for logging, depending on the value of 'KOMPUTE_OPT_USE_SPDLOG'. The log level used can be changed here. Possible values: 'Trace', 'Debug', 'Info', 'Warn', 'Error', 'Critical', 'Off', 'Default'. If set to 'Off' logging will be deactivated completely. If set to 'Default', the log level will be set to 'Info' for release builds and 'Debug' else." "Default") +kompute_option(KOMPUTE_OPT_USE_SPDLOG "If enabled, logging via KP_LOG_ will happen through Spdlog instead of plan fmt." OFF) kompute_option(KOMPUTE_OPT_ANDROID_BUILD "Enable android compilation flags required." OFF) kompute_option(KOMPUTE_OPT_DISABLE_VK_DEBUG_LAYERS "Explicitly disable debug layers even on debug." OFF) kompute_option(KOMPUTE_OPT_DISABLE_VULKAN_VERSION_CHECK "Whether to check if your driver supports the Vulkan Header version you are linking against. This might be useful in case you build shared on a different system than you run later." OFF) kompute_option(KOMPUTE_OPT_BUILD_SHADERS "Rebuilds all compute shaders during compilation and does not use the already precompiled versions. Requires glslangValidator to be installed on your system." OFF) # External components -kompute_option(KOMPUTE_OPT_USE_BUILT_IN_SPDLOG "Use the built-in version of Spdlog." ON) +kompute_option(KOMPUTE_OPT_USE_BUILT_IN_SPDLOG "Use the built-in version of Spdlog. Requires 'KOMPUTE_OPT_USE_SPDLOG' to be set to ON in order to have any effect." ON) kompute_option(KOMPUTE_OPT_USE_BUILT_IN_FMT "Use the built-in version of fmt." ON) kompute_option(KOMPUTE_OPT_USE_BUILT_IN_GOOGLE_TEST "Use the built-in version of GoogleTest." ON) kompute_option(KOMPUTE_OPT_USE_BUILT_IN_PYBIND11 "Use the built-in version of pybind11." ON) @@ -139,16 +140,20 @@ else() endif() # Spdlog -if(NOT KOMPUTE_OPT_LOG_LEVEL_DISABLED) - if(KOMPUTE_OPT_USE_BUILT_IN_SPDLOG) - set(SPDLOG_INSTALL ${KOMPUTE_OPT_INSTALL}) - set(SPDLOG_BUILD_SHARED ${BUILD_SHARED_LIBS}) +if(KOMPUTE_OPT_USE_SPDLOG) + add_compile_definitions(KOMPUTE_OPT_USE_SPDLOG=1) - FetchContent_Declare(spdlog GIT_REPOSITORY https://github.com/gabime/spdlog.git - GIT_TAG v1.10.0) # Source: https://github.com/gabime/spdlog/releases - FetchContent_MakeAvailable(spdlog) - else() - find_package(spdlog REQUIRED) + if(NOT KOMPUTE_OPT_LOG_LEVEL_DISABLED) + if(KOMPUTE_OPT_USE_BUILT_IN_SPDLOG) + set(SPDLOG_INSTALL ${KOMPUTE_OPT_INSTALL}) + set(SPDLOG_BUILD_SHARED ${BUILD_SHARED_LIBS}) + + FetchContent_Declare(spdlog GIT_REPOSITORY https://github.com/gabime/spdlog.git + GIT_TAG v1.10.0) # Source: https://github.com/gabime/spdlog/releases + FetchContent_MakeAvailable(spdlog) + else() + find_package(spdlog REQUIRED) + endif() endif() endif() diff --git a/src/Manager.cpp b/src/Manager.cpp index 53bcaa1ac..5e9ee9d31 100644 --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -320,7 +320,7 @@ Manager::createDevice(const std::vector& familyQueueIndices, this->mPhysicalDevice = std::make_shared(physicalDevice); -#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO && SPDLOG_ACTIVE_LEVEL != SPDLOG_LEVEL_OFF +#if KOMPUTE_OPT_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_INFO vk::PhysicalDeviceProperties physicalDeviceProperties = physicalDevice.getProperties(); #endif diff --git a/src/include/kompute/logger/Logger.hpp b/src/include/kompute/logger/Logger.hpp index f24ad3a80..43f119471 100644 --- a/src/include/kompute/logger/Logger.hpp +++ b/src/include/kompute/logger/Logger.hpp @@ -1,5 +1,14 @@ #pragma once +#define KOMPUTE_LOG_LEVEL_TRACE 0 +#define KOMPUTE_LOG_LEVEL_DEBUG 1 +#define KOMPUTE_LOG_LEVEL_INFO 2 +#define KOMPUTE_LOG_LEVEL_WARN 3 +#define KOMPUTE_LOG_LEVEL_ERROR 4 +#define KOMPUTE_LOG_LEVEL_CRITICAL 5 +#define KOMPUTE_LOG_LEVEL_OFF 6 + +// Logging is disabled entirely. #if KOMPUTE_OPT_LOG_LEVEL_DISABLED #define KP_LOG_TRACE(...) #define KP_LOG_DEBUG(...) @@ -8,10 +17,94 @@ #define KP_LOG_ERROR(...) #else -#include +#if !KOMPUTE_OPT_USE_SPDLOG +#include +#else #include +#endif // !KOMPUTE_OPT_USE_SPDLOG +#include #include #include +namespace logger { +// Setup the logger, note the loglevel can not be set below the CMake log level +// (To change this use -DKOMPUTE_OPT_LOG_LEVEL=...) +void +setupLogger(); + +// Logging is enabled, but we do not use Spdlog. So we use fmt in case nothing +// else is defined, overriding logging. +#if !KOMPUTE_OPT_USE_SPDLOG + +#ifndef KP_LOG_TRACE +#if KOMPUTE_OPT_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_TRACE +#define KP_LOG_TRACE(...) \ + fmt::print("[{} {}] [trace] [{}:{}] {}\n", \ + __DATE__, \ + __TIME__, \ + __FILE__, \ + __LINE__, \ + fmt::format(__VA_ARGS__)) +#else +#define KP_LOG_TRACE(...) +#endif +#endif // !KP_LOG_TRACE + +#ifndef KP_LOG_DEBUG +#if KOMPUTE_OPT_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_DEBUG +#define KP_LOG_DEBUG(...) \ + fmt::print("[{} {}] [debug] [{}:{}] {}\n", \ + __DATE__, \ + __TIME__, \ + __FILE__, \ + __LINE__, \ + fmt::format(__VA_ARGS__)) +#else +#define KP_LOG_DEBUG(...) +#endif +#endif // !KP_LOG_DEBUG + +#ifndef KP_LOG_INFO +#if KOMPUTE_OPT_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_INFO +#define KP_LOG_INFO(...) \ + fmt::print("[{} {}] [info] [{}:{}] {}\n", \ + __DATE__, \ + __TIME__, \ + __FILE__, \ + __LINE__, \ + fmt::format(__VA_ARGS__)) +#else +#define KP_LOG_INFO(...) +#endif +#endif // !KP_LOG_INFO + +#ifndef KP_LOG_WARN +#if KOMPUTE_OPT_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_WARN +#define KP_LOG_WARN(...) \ + fmt::print("[{} {}] [warn] [{}:{}] {}\n", \ + __DATE__, \ + __TIME__, \ + __FILE__, \ + __LINE__, \ + fmt::format(__VA_ARGS__)) +#else +#define KP_LOG_WARN(...) +#endif +#endif // !KP_LOG_WARN + +#ifndef KP_LOG_ERROR +#if KOMPUTE_OPT_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_ERROR +#define KP_LOG_ERROR(...) \ + fmt::print("[{} {}] [error] [{}:{}] {}\n", \ + __DATE__, \ + __TIME__, \ + __FILE__, \ + __LINE__, \ + fmt::format(__VA_ARGS__)) +#else +#define KP_LOG_ERROR(...) +#endif +#endif // !KP_LOG_ERROR +#else #define KP_LOG_TRACE(...) SPDLOG_TRACE(__VA_ARGS__) #define KP_LOG_DEBUG(...) SPDLOG_DEBUG(__VA_ARGS__) @@ -19,20 +112,14 @@ #define KP_LOG_WARN(...) SPDLOG_WARN(__VA_ARGS__) #define KP_LOG_ERROR(...) SPDLOG_ERROR(__VA_ARGS__) -namespace logger { -const std::string logFolder("kompute_logs"); -// Setup the logger, note the loglevel can not be set below the CMake log level -// (To change this use -DKOMPUTE_OPT_LOG_LEVEL=...) -void -setupLogger(); void setLogLevel(spdlog::level::level_enum level); -void -deactivateLogger(); spdlog::level::level_enum getLogLevel(); +#endif // !KOMPUTE_OPT_USE_SPDLOG + std::string setToString(const std::set& set); @@ -43,4 +130,4 @@ std::string vecToString(const std::vector& vec); } // namespace logger -#endif \ No newline at end of file +#endif // KOMPUTE_OPT_LOG_LEVEL_DISABLED \ No newline at end of file diff --git a/src/logger/CMakeLists.txt b/src/logger/CMakeLists.txt index 6132c1d21..b73615a61 100644 --- a/src/logger/CMakeLists.txt +++ b/src/logger/CMakeLists.txt @@ -4,9 +4,14 @@ set(LOGGER_SOURCES Logger.cpp) add_library(kp_logger ${LOGGER_SOURCES}) -if(NOT KOMPUTE_OPT_LOG_LEVEL_DISABLED) - target_link_libraries(kp_logger PUBLIC spdlog::spdlog) -endif() +# Define log levels in code +add_compile_definitions(KOMPUTE_LOG_LEVEL_TRACE=0) +add_compile_definitions(KOMPUTE_LOG_LEVEL_DEBUG=1) +add_compile_definitions(KOMPUTE_LOG_LEVEL_INFO=2) +add_compile_definitions(KOMPUTE_LOG_LEVEL_WARN=3) +add_compile_definitions(KOMPUTE_LOG_LEVEL_ERROR=4) +add_compile_definitions(KOMPUTE_LOG_LEVEL_CRITICAL=5) +add_compile_definitions(KOMPUTE_LOG_LEVEL_OFF=6) if(${KOMPUTE_OPT_LOG_LEVEL} STREQUAL "Trace") set(KOMPUTE_OPT_LOG_LEVEL TRACE) @@ -31,9 +36,17 @@ elseif(${KOMPUTE_OPT_LOG_LEVEL} STREQUAL "Off") message(STATUS "Using log level Off") elseif(${KOMPUTE_OPT_LOG_LEVEL} STREQUAL "Default") set(KOMPUTE_OPT_LOG_LEVEL $,DEBUG,INFO>) - message(STATUS "Setting KOMPUTE_OPT_LOG_LEVEL to according to build type") + message(STATUS "Setting KOMPUTE_OPT_LOG_LEVEL to according to the build type") else() message(FATAL_ERROR "Log level '${KOMPUTE_OPT_LOG_LEVEL}' unknown, use -DKOMPUTE_OPT_LOG_LEVEL={Trace, Debug, Info, Warn, Error, Critical, Off, Default} to set it to a correct value.") endif() -target_compile_definitions(kp_logger INTERFACE SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_${KOMPUTE_OPT_LOG_LEVEL}) +# Link depending on how the logger should be setup +if(NOT KOMPUTE_OPT_LOG_LEVEL_DISABLED) + if(KOMPUTE_OPT_USE_SPDLOG) + target_link_libraries(kp_logger PUBLIC spdlog::spdlog) + target_compile_definitions(kp_logger INTERFACE SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_${KOMPUTE_OPT_LOG_LEVEL}) + else() + target_link_libraries(kp_logger PUBLIC fmt::fmt) + endif() +endif() diff --git a/src/logger/Logger.cpp b/src/logger/Logger.cpp index bb02023a2..79cb46686 100644 --- a/src/logger/Logger.cpp +++ b/src/logger/Logger.cpp @@ -1,6 +1,7 @@ #include "kompute/logger/Logger.hpp" -#if KOMPUTE_OPT_LOG_LEVEL_DISABLED +#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED +#if !KOMPUTE_OPT_USE_SPDLOG #else #include #include @@ -9,65 +10,22 @@ #include #include #include -#include #include #include #include - -#include -#include - -#ifdef _WIN32 -#include -#include -#endif // _WIN32 +#endif // !KOMPUTE_OPT_USE_SPDLOG namespace logger { -constexpr int THREAD_QUEUE_LENGTH = 8192; -constexpr int FILE_ROTATION_TIME = 1048576 * 5; +#if !KOMPUTE_OPT_USE_SPDLOG -/** - * Should be replaced with: - * std::filesystem::exists(path) - * when switching to cpp17 - **/ -bool -exists(const std::string& path) -{ - struct stat info - {}; - if (stat(path.c_str(), &info) != 0) { - // std::cerr << "Failed to check if '" << path - // << "' exists. Cannot access!\n"; - // assert(false); - return false; - } - return info.st_mode & S_IFDIR; -} - -/** - * Based on: https://stackoverflow.com/a/35109823 - * Should be replaced with: - * std::filesystem::create_directory(path); - * when switching to cpp17 - **/ void -createDir(const std::string& path) +setupLogger() { - int nError = 0; -#if defined(_WIN32) - nError = _mkdir(path.c_str()); // can be used on Windows -#else - mode_t nMode = 0733; // UNIX style permissions - nError = mkdir(path.c_str(), nMode); // can be used on non-Windows -#endif - if (nError != 0) { - std::cerr << "Failed to create '" << path << "' with: " << nError - << '\n'; - assert(false); - } } +#else +constexpr int THREAD_QUEUE_LENGTH = 8192; + void setupLogger() { @@ -82,25 +40,11 @@ setupLogger() setup = true; setupMutex.unlock(); - if (!exists(logger::logFolder)) { - createDir(logger::logFolder); - } spdlog::init_thread_pool(THREAD_QUEUE_LENGTH, 1); spdlog::sink_ptr console_sink = std::make_shared(); console_sink->set_pattern("[%H:%M:%S %z] [%=8l] [thread %t] [%@]\t%v"); -#ifdef _WIN32 - std::string s = logger::logFolder + "\\kompute.log"; - spdlog::sink_ptr file_sink = - std::make_shared( - s, FILE_ROTATION_TIME, 3); -#else // _WIN32 - spdlog::sink_ptr file_sink = - std::make_shared( - logger::logFolder + "/kompute.log", FILE_ROTATION_TIME, 3); -#endif - file_sink->set_pattern("[%H:%M:%S %z] [%=8l] [thread %t] [%@]\t%v"); - std::vector sinks{ file_sink, console_sink }; + std::vector sinks{ console_sink }; std::shared_ptr logger = std::make_shared( "", @@ -148,12 +92,7 @@ setLogLevel(const spdlog::level::level_enum level) { spdlog::default_logger()->set_level(level); } - -void -deactivateLogger() -{ - logger::setLogLevel(spdlog::level::off); -} +#endif // !KOMPUTE_OPT_USE_SPDLOG std::string setToString(const std::set& set) @@ -194,4 +133,5 @@ vecToString(const std::vector& vec) return result.substr(0, result.size() - 2); // Remove the tailing ", " } } // namespace logger + #endif