From 2abfa1628bf02ce5df3a512cf858f722c2f81859 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sat, 18 Jan 2025 13:36:01 +0200 Subject: [PATCH] Gabime/visibilty-hidden 2.x (#3324) Set CMAKE_CXX_VISIBILITY_PRESET and VISIBILITY_INLINES_HIDDEN when build shared lib --- CMakeLists.txt | 4 ++++ include/spdlog/details/null_mutex.h | 1 - include/spdlog/sinks/ansicolor_sink.h | 12 +++++++----- include/spdlog/sinks/base_sink.h | 2 +- src/sinks/ansicolor_sink.cpp | 21 ++++++++++++++------- src/sinks/rotating_file_sink.cpp | 2 +- 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 81df3bf88..b8517ea69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -242,6 +242,10 @@ if (BUILD_SHARED_LIBS) endif () add_library(spdlog SHARED ${VERSION_RC}) target_compile_definitions(spdlog PUBLIC SPDLOG_SHARED_LIB) + set_target_properties(spdlog PROPERTIES + CXX_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN ON) + if (MSVC) # disable dlls related warnings on msvc target_compile_options(spdlog PUBLIC $<$,$>>:/wd4251 diff --git a/include/spdlog/details/null_mutex.h b/include/spdlog/details/null_mutex.h index 47b9845b8..a0f504588 100644 --- a/include/spdlog/details/null_mutex.h +++ b/include/spdlog/details/null_mutex.h @@ -7,7 +7,6 @@ #include // null, no cost dummy "mutex" and dummy "atomic" log level - namespace spdlog { namespace details { struct null_mutex { diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 30b19628b..418c068b4 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -70,14 +70,16 @@ class ansicolor_sink : public base_sink { static constexpr std::string_view bold_on_red = "\033[1m\033[41m"; private: - void sink_it_(const details::log_msg &msg) override; - void flush_() override; FILE *target_file_; bool should_do_colors_; std::array colors_; - void print_ccode_(const string_view_t color_code); - void print_range_(const memory_buf_t &formatted, size_t start, size_t end); - static std::string to_string_(const string_view_t sv); + + void sink_it_(const details::log_msg &msg) override; + void flush_() override; + void set_color_mode_(color_mode mode); + void print_ccode_(string_view_t color_code) const; + void print_range_(const memory_buf_t &formatted, size_t start, size_t end) const; + static std::string to_string_(string_view_t sv); }; template diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h index 04cc6883c..5b60dcc99 100644 --- a/include/spdlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -36,7 +36,7 @@ class SPDLOG_API base_sink : public sink { protected: // sink formatter std::unique_ptr formatter_; - Mutex mutex_; + mutable Mutex mutex_; virtual void sink_it_(const details::log_msg &msg) = 0; virtual void flush_() = 0; diff --git a/src/sinks/ansicolor_sink.cpp b/src/sinks/ansicolor_sink.cpp index 6de54e505..b684ea0fd 100644 --- a/src/sinks/ansicolor_sink.cpp +++ b/src/sinks/ansicolor_sink.cpp @@ -13,7 +13,7 @@ namespace sinks { template ansicolor_sink::ansicolor_sink(FILE *target_file, color_mode mode) : target_file_(target_file) { - set_color_mode(mode); + set_color_mode_(mode); colors_.at(level_to_number(level::trace)) = to_string_(white); colors_.at(level_to_number(level::debug)) = to_string_(cyan); colors_.at(level_to_number(level::info)) = to_string_(green); @@ -37,22 +37,27 @@ bool ansicolor_sink::should_color() const { template void ansicolor_sink::set_color_mode(color_mode mode) { + std::lock_guard lock(base_sink::mutex_); + set_color_mode_(mode); +} + +template +void ansicolor_sink::set_color_mode_(color_mode mode) { std::lock_guard lock(base_sink::mutex_); switch (mode) { case color_mode::always: should_do_colors_ = true; - return; + return; case color_mode::automatic: should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal(); - return; + return; case color_mode::never: should_do_colors_ = false; - return; + return; default: should_do_colors_ = false; } } - template void ansicolor_sink::sink_it_(const details::log_msg &msg) { // Wrap the originally formatted message in color codes. @@ -83,12 +88,12 @@ void ansicolor_sink::flush_() { } template -void ansicolor_sink::print_ccode_(const string_view_t color_code) { +void ansicolor_sink::print_ccode_(const string_view_t color_code) const { details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_); } template -void ansicolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) { +void ansicolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) const { details::os::fwrite_bytes(formatted.data() + start, end - start, target_file_); } @@ -112,6 +117,8 @@ ansicolor_stderr_sink::ansicolor_stderr_sink(color_mode mode) // template instantiations #include "spdlog/details/null_mutex.h" +template class SPDLOG_API spdlog::sinks::ansicolor_sink; +template class SPDLOG_API spdlog::sinks::ansicolor_sink; template class SPDLOG_API spdlog::sinks::ansicolor_stdout_sink; template class SPDLOG_API spdlog::sinks::ansicolor_stdout_sink; template class SPDLOG_API spdlog::sinks::ansicolor_stderr_sink; diff --git a/src/sinks/rotating_file_sink.cpp b/src/sinks/rotating_file_sink.cpp index b58115bda..b43c46286 100644 --- a/src/sinks/rotating_file_sink.cpp +++ b/src/sinks/rotating_file_sink.cpp @@ -142,4 +142,4 @@ bool rotating_file_sink::rename_file_(const filename_t &src_filename, con // template instantiations #include "spdlog/details/null_mutex.h" template class SPDLOG_API spdlog::sinks::rotating_file_sink; -template class SPDLOG_API spdlog::sinks::rotating_file_sink; \ No newline at end of file +template class SPDLOG_API spdlog::sinks::rotating_file_sink;