diff --git a/CMakeLists.txt b/CMakeLists.txt index 21577e50..95af3e53 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -137,7 +137,7 @@ message(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) # --------------------------------------------------------------------------------------- # Static/Shared library (shared not supported in windows yet) # --------------------------------------------------------------------------------------- -set(SPDLOG_SRCS src/spdlog.cpp src/stdout_sinks.cpp src/color_sinks.cpp src/file_sinks.cpp src/async.cpp src/cfg.cpp) +set(SPDLOG_SRCS src/spdlog.cpp src/stdout_sinks.cpp src/color_sinks.cpp src/file_sinks.cpp src/registry.cpp src/async.cpp src/cfg.cpp) if(NOT SPDLOG_USE_STD_FORMAT AND NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO) list(APPEND SPDLOG_SRCS src/fmt.cpp) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 83336e98..f982f45b 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -8,6 +8,22 @@ if(NOT TARGET spdlog) find_package(spdlog REQUIRED) endif() +# --------------------------------------------------------------------------------------- +# Example of using shared libraries +# --------------------------------------------------------------------------------------- +add_library(mylibrary SHARED mylibrary.cpp mylibrary.h) +target_link_libraries(mylibrary PRIVATE spdlog::spdlog) +target_compile_definitions(mylibrary PUBLIC SPDLOG_COMPILED_LIB) + +add_executable(library_example library_example.cpp) +target_link_libraries(library_example PRIVATE spdlog::spdlog mylibrary) +target_compile_definitions(library_example PUBLIC SPDLOG_COMPILED_LIB) + +if(SPDLOG_BUILD_SHARED OR BUILD_SHARED_LIBS) + target_compile_definitions(mylibrary PUBLIC SPDLOG_SHARED_LIB) + target_compile_definitions(library_example PUBLIC SPDLOG_SHARED_LIB) +endif() + # --------------------------------------------------------------------------------------- # Example of using pre-compiled library # --------------------------------------------------------------------------------------- diff --git a/example/library_example.cpp b/example/library_example.cpp new file mode 100644 index 00000000..4a35fff8 --- /dev/null +++ b/example/library_example.cpp @@ -0,0 +1,21 @@ +// +// Copyright(c) 2021 +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#include "spdlog/spdlog.h" +#include "spdlog/sinks/basic_file_sink.h" +#include "mylibrary.h" + +int main(int, char *[]) +{ + auto new_logger = spdlog::basic_logger_mt("library_example", "logs/library_example.txt", true); + spdlog::set_level(spdlog::level::info); + + spdlog::set_default_logger(new_logger); + #ifndef SPDLOG_SHARED_LIB + lib::set_logger(new_logger); + #endif + + spdlog::info("This message is from the base application"); + lib::test(); +} diff --git a/example/mylibrary.cpp b/example/mylibrary.cpp new file mode 100644 index 00000000..aeb7032e --- /dev/null +++ b/example/mylibrary.cpp @@ -0,0 +1,22 @@ +// +// Copyright(c) 2021 +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#include "mylibrary.h" + +#include + +namespace lib +{ + const std::string logger_name = "example"; + + void set_logger(const std::shared_ptr& logger) + { + spdlog::set_default_logger(logger); + } + + void test() + { + spdlog::info("This message is from the shared library."); + } +} \ No newline at end of file diff --git a/example/mylibrary.h b/example/mylibrary.h new file mode 100644 index 00000000..3043b633 --- /dev/null +++ b/example/mylibrary.h @@ -0,0 +1,19 @@ +// +// Copyright(c) 2021 +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#ifdef _WIN32 +#define LIB_EXPORT __declspec(dllexport) +#else // !defined(_WIN32) +#define LIB_EXPORT +#endif + +#include +#include + +namespace lib +{ + LIB_EXPORT void set_logger(const std::shared_ptr& logger); + + LIB_EXPORT void test(); +} diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index c55b5eea..7f5e1925 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -288,11 +288,13 @@ SPDLOG_INLINE void registry::set_levels(log_levels levels, level::level_enum *gl } } +#ifndef SPDLOG_COMPILED_LIB SPDLOG_INLINE registry ®istry::instance() { static registry s_instance; return s_instance; } +#endif SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name) { diff --git a/src/registry.cpp b/src/registry.cpp new file mode 100644 index 00000000..29dd2c83 --- /dev/null +++ b/src/registry.cpp @@ -0,0 +1,20 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#ifndef SPDLOG_COMPILED_LIB +# error Please define SPDLOG_COMPILED_LIB to compile this file. +#endif + +#include + +namespace spdlog { +namespace details { + +registry ®istry::instance() +{ + static registry s_instance; + return s_instance; +} + +} +}