Spdlog is now optional for logging

Signed-off-by: Fabian Sauter <sauter.fabian@mailbox.org>
This commit is contained in:
Fabian Sauter 2022-07-26 10:48:57 +02:00
parent 652dad3d38
commit 444c8fc1db
5 changed files with 144 additions and 99 deletions

View file

@ -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_<DEBUG, INFO, etc...> 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()

View file

@ -320,7 +320,7 @@ Manager::createDevice(const std::vector<uint32_t>& familyQueueIndices,
this->mPhysicalDevice =
std::make_shared<vk::PhysicalDevice>(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

View file

@ -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 <set>
#if !KOMPUTE_OPT_USE_SPDLOG
#include <fmt/core.h>
#else
#include <spdlog/spdlog.h>
#endif // !KOMPUTE_OPT_USE_SPDLOG
#include <set>
#include <string>
#include <vector>
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<std::string>& set);
@ -43,4 +130,4 @@ std::string
vecToString(const std::vector<std::string>& vec);
} // namespace logger
#endif
#endif // KOMPUTE_OPT_LOG_LEVEL_DISABLED

View file

@ -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 $<IF:$<CONFIG:Debug>,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()

View file

@ -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 <cassert>
#include <iostream>
@ -9,65 +10,22 @@
#include <spdlog/async.h>
#include <spdlog/common.h>
#include <spdlog/logger.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef _WIN32
#include <direct.h>
#include <iso646.h>
#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<spdlog::sinks::stdout_color_sink_mt>();
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<spdlog::sinks::rotating_file_sink_mt>(
s, FILE_ROTATION_TIME, 3);
#else // _WIN32
spdlog::sink_ptr file_sink =
std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
logger::logFolder + "/kompute.log", FILE_ROTATION_TIME, 3);
#endif
file_sink->set_pattern("[%H:%M:%S %z] [%=8l] [thread %t] [%@]\t%v");
std::vector<spdlog::sink_ptr> sinks{ file_sink, console_sink };
std::vector<spdlog::sink_ptr> sinks{ console_sink };
std::shared_ptr<spdlog::logger> logger =
std::make_shared<spdlog::async_logger>(
"",
@ -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<std::string>& set)
@ -194,4 +133,5 @@ vecToString(const std::vector<std::string>& vec)
return result.substr(0, result.size() - 2); // Remove the tailing ", "
}
} // namespace logger
#endif