Option to disable logging completely.

Signed-off-by: Fabian Sauter <sauter.fabian@mailbox.org>
This commit is contained in:
Fabian Sauter 2022-07-12 10:32:11 +02:00
parent 7bdde2e6f1
commit cf721cc21a
20 changed files with 86 additions and 57 deletions

View file

@ -42,7 +42,7 @@ endmacro()
macro(kompute_log_level OPTION_NAME OPTION_TEXT OPTION_DEFAULT)
set(${OPTION_NAME} ${OPTION_DEFAULT} CACHE STRING ${OPTION_TEXT})
set_property(CACHE ${OPTION_NAME} PROPERTY STRINGS "Trace" "Debug" "Info" "Warn" "Error" "Critical")
set_property(CACHE ${OPTION_NAME} PROPERTY STRINGS "Trace" "Debug" "Info" "Warn" "Error" "Critical" "Default" "Off")
if(DEFINED ENV{${OPTION_NAME}})
# Allow setting the option through an environment variable
@ -53,6 +53,12 @@ macro(kompute_log_level OPTION_NAME OPTION_TEXT OPTION_DEFAULT)
add_definitions(-D${OPTION_NAME})
endif()
# Allow disabling logging completely and prevent linking against it:
if(${KOMPUTE_OPT_LOG_LEVEL} STREQUAL "Off")
set(${OPTION_NAME}_DISABLED ON)
add_compile_definitions(${OPTION_NAME}_DISABLED=1)
endif()
message(STATUS " ${OPTION_NAME}: ${${OPTION_NAME}}")
endmacro()
@ -132,15 +138,17 @@ else()
endif()
# Spdlog
if(KOMPUTE_OPT_USE_BUILD_IN_SPDLOG)
set(SPDLOG_INSTALL ${KOMPUTE_OPT_INSTALL})
set(SPDLOG_BUILD_SHARED ${KOMPUTE_OPT_DEPENDENCIES_SHARED_LIBS})
if(NOT KOMPUTE_OPT_LOG_LEVEL_DISABLED)
if(KOMPUTE_OPT_USE_BUILD_IN_SPDLOG)
set(SPDLOG_INSTALL ${KOMPUTE_OPT_INSTALL})
set(SPDLOG_BUILD_SHARED ${KOMPUTE_OPT_DEPENDENCIES_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)
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()
# fmt

View file

@ -40,8 +40,10 @@ Manager::Manager(uint32_t physicalDeviceIndex,
{
this->mManageResources = true;
// Make sure the logger is setup
// Make sure the logger is setup
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
this->createInstance();
this->createDevice(
@ -58,8 +60,10 @@ Manager::Manager(std::shared_ptr<vk::Instance> instance,
this->mPhysicalDevice = physicalDevice;
this->mDevice = device;
// Make sure the logger is setup
// Make sure the logger is setup
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
}
Manager::~Manager()

View file

@ -3,6 +3,7 @@
#include "kompute/Core.hpp"
#include "fmt/format.h"
#include "kompute/Tensor.hpp"
#include "logger/Logger.hpp"

View file

@ -1,5 +1,13 @@
#pragma once
#if KOMPUTE_OPT_LOG_LEVEL_DISABLED
#define KP_LOG_TRACE(...)
#define KP_LOG_DEBUG(...)
#define KP_LOG_INFO(...)
#define KP_LOG_WARN(...)
#define KP_LOG_ERROR(...)
#else
#include <set>
#include <spdlog/spdlog.h>
#include <string>
@ -34,3 +42,5 @@ vecToString(const std::vector<const char*>& vec);
std::string
vecToString(const std::vector<std::string>& vec);
} // namespace logger
#endif

View file

@ -3,7 +3,10 @@ cmake_minimum_required(VERSION 3.14)
set(LOGGER_SOURCES Logger.cpp)
add_library(kp_logger ${LOGGER_SOURCES})
target_link_libraries(kp_logger PUBLIC spdlog::spdlog)
if(NOT KOMPUTE_OPT_LOG_LEVEL_DISABLED)
target_link_libraries(kp_logger PUBLIC spdlog::spdlog)
endif()
if(${KOMPUTE_OPT_LOG_LEVEL} STREQUAL "Trace")
set(KOMPUTE_OPT_LOG_LEVEL TRACE)

View file

@ -1,9 +1,10 @@
#include "kompute/logger/Logger.hpp"
#if KOMPUTE_OPT_LOG_LEVEL_DISABLED
#else
#include <cassert>
#include <iostream>
#include <memory>
#include <string>
#include <mutex>
#include <spdlog/async.h>
#include <spdlog/common.h>
@ -11,13 +12,14 @@
#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 <iso646.h>
#include <direct.h>
#include <iso646.h>
#endif // _WIN32
namespace logger {
@ -56,7 +58,7 @@ createDir(const std::string& path)
#if defined(_WIN32)
nError = _mkdir(path.c_str()); // can be used on Windows
#else
mode_t nMode = 0733; // UNIX style permissions
mode_t nMode = 0733; // UNIX style permissions
nError = mkdir(path.c_str(), nMode); // can be used on non-Windows
#endif
if (nError != 0) {
@ -192,3 +194,4 @@ vecToString(const std::vector<std::string>& vec)
return result.substr(0, result.size() - 2); // Remove the tailing ", "
}
} // namespace logger
#endif

View file

@ -5,6 +5,7 @@
#include <chrono>
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
#include "shaders/Utils.hpp"
TEST(TestAsyncOperations, TestManagerParallelExecution)
@ -264,9 +265,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
#include "shaders/Utils.hpp"
@ -145,9 +146,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
#include "test_logistic_regression_shader.hpp"
@ -162,9 +163,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
TEST(TestManager, EndToEndOpMultEvalFlow)
{
@ -111,9 +112,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
#include "shaders/Utils.hpp"
@ -276,9 +277,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
#include "shaders/Utils.hpp"
#include "test_op_custom_shader.hpp"
@ -113,9 +114,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
#include "shaders/Utils.hpp"
@ -162,9 +163,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
TEST(TestOpTensorCreate, CreateSingleTensorSingleOp)
{
@ -63,9 +64,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
TEST(TestOpTensorSync, SyncToDeviceMemorySingleTensor)
{
@ -58,9 +59,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
#include "shaders/Utils.hpp"
@ -395,9 +396,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
#include "shaders/Utils.hpp"
@ -248,9 +249,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
#include "shaders/Utils.hpp"
@ -107,9 +108,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
TEST(TestTensor, ConstructorData)
{
@ -49,9 +50,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();

View file

@ -3,6 +3,7 @@
#include "gtest/gtest.h"
#include "kompute/Kompute.hpp"
#include "kompute/logger/Logger.hpp"
#include "test_workgroup_shader.hpp"
@ -68,9 +69,8 @@ main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
#if KOMPUTE_ENABLE_SPDLOG
spdlog::set_level(
static_cast<spdlog::level::level_enum>(KOMPUTE_LOG_LEVEL));
#if !KOMPUTE_OPT_LOG_LEVEL_DISABLED
logger::setupLogger();
#endif
return RUN_ALL_TESTS();