Implements shared library compatible global set_default_logger.

The key difference is that registry::instance is moved to a cpp when
compiling the library.
pull/2200/head
Sean Farrell 4 years ago
parent 0c611af552
commit 9f87c566bd

@ -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)

@ -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
# ---------------------------------------------------------------------------------------

@ -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();
}

@ -0,0 +1,22 @@
//
// Copyright(c) 2021
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#include "mylibrary.h"
#include <spdlog/spdlog.h>
namespace lib
{
const std::string logger_name = "example";
void set_logger(const std::shared_ptr<spdlog::logger>& logger)
{
spdlog::set_default_logger(logger);
}
void test()
{
spdlog::info("This message is from the shared library.");
}
}

@ -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 <memory>
#include <spdlog/logger.h>
namespace lib
{
LIB_EXPORT void set_logger(const std::shared_ptr<spdlog::logger>& logger);
LIB_EXPORT void test();
}

@ -288,11 +288,13 @@ SPDLOG_INLINE void registry::set_levels(log_levels levels, level::level_enum *gl
}
}
#ifndef SPDLOG_COMPILED_LIB
SPDLOG_INLINE registry &registry::instance()
{
static registry s_instance;
return s_instance;
}
#endif
SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name)
{

@ -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 <spdlog/details/registry.h>
namespace spdlog {
namespace details {
registry &registry::instance()
{
static registry s_instance;
return s_instance;
}
}
}
Loading…
Cancel
Save