From d5c9bac3c79f0a9ec656a2f7ec27f5e29e75181a Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 12 May 2019 23:08:14 +0300 Subject: [PATCH] wip static-lib --- example/example.cpp | 1 - include/spdlog/sinks/daily_file_sink.h | 1 + include/spdlog/sinks/stdout_sinks.h | 1 + include/spdlog/sinks/wincolor_sink-inl.h | 10 +-- include/spdlog/sinks/wincolor_sink.h | 15 ++-- include/spdlog/spdlog-inl.h | 96 ++++++++++++++++++++++++ include/spdlog/spdlog.h | 90 ++++++---------------- src/spdlog.cpp | 2 + 8 files changed, 133 insertions(+), 83 deletions(-) create mode 100644 include/spdlog/spdlog-inl.h diff --git a/example/example.cpp b/example/example.cpp index e6508156..9647fddd 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -2,7 +2,6 @@ // Copyright(c) 2015 Gabi Melman. // Distributed under the MIT License (http://opensource.org/licenses/MIT) - // spdlog usage example #include diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index a240294c..23ca6f0b 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -11,6 +11,7 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/fmt/fmt.h" #include "spdlog/sinks/base_sink.h" +#include "spdlog/details/os.h" #include #include diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index 91274c11..91bed987 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -9,6 +9,7 @@ #include "spdlog/details/console_globals.h" #include "spdlog/details/null_mutex.h" +#include "spdlog/details/pattern_formatter.h" #include #include diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 8600e0ad..da5262b3 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -7,8 +7,8 @@ namespace spdlog { namespace sinks { template SPDLOG_INLINE wincolor_sink::wincolor_sink() - : out_handle_(TargetStream::handle()) - , mutex_(ConsoleMutex::mutex()) + : out_handle_(TargetStream::handle()) + , mutex_(ConsoleMutex::mutex()) { colors_[level::trace] = WHITE; colors_[level::debug] = CYAN; @@ -49,7 +49,7 @@ void SPDLOG_INLINE wincolor_sink::log(const details: print_range_(formatted, msg.color_range_start, msg.color_range_end); ::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors - // after color range + // after color range print_range_(formatted, msg.color_range_end, formatted.size()); } else // print without colors if color range is invalid @@ -77,7 +77,7 @@ void SPDLOG_INLINE wincolor_sink::set_formatter(std: std::lock_guard lock(mutex_); formatter_ = std::move(sink_formatter); } - + // set color and return the orig console attributes (for resetting later) template WORD SPDLOG_INLINE wincolor_sink::set_console_attribs(WORD attribs) @@ -100,5 +100,5 @@ void SPDLOG_INLINE wincolor_sink::print_range_(const ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr); } -} +} // namespace sinks } // namespace spdlog \ No newline at end of file diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 7645819c..29319fd9 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -35,27 +35,26 @@ public: const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; - wincolor_sink(); + wincolor_sink(); ~wincolor_sink() override; wincolor_sink(const wincolor_sink &other) = delete; wincolor_sink &operator=(const wincolor_sink &other) = delete; // change the color for the given level - void set_color(level::level_enum level, WORD color); - void log(const details::log_msg &msg) final override; - void flush() final override; - void set_pattern(const std::string &pattern) override final; + void set_color(level::level_enum level, WORD color); + void log(const details::log_msg &msg) final override; + void flush() final override; + void set_pattern(const std::string &pattern) override final; void set_formatter(std::unique_ptr sink_formatter) override final; - private: - using mutex_t = typename ConsoleMutex::mutex_t; + using mutex_t = typename ConsoleMutex::mutex_t; HANDLE out_handle_; mutex_t &mutex_; std::unordered_map colors_; - // set color and return the orig console attributes (for resetting later) + // set color and return the orig console attributes (for resetting later) WORD set_console_attribs(WORD attribs); // print a range of formatted message to console void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end); diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h new file mode 100644 index 00000000..e2cd60ac --- /dev/null +++ b/include/spdlog/spdlog-inl.h @@ -0,0 +1,96 @@ +// Copyright(c) 2015-present Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include "spdlog/common.h" +#include "spdlog/details/pattern_formatter.h" + +namespace spdlog { + +SPDLOG_INLINE void initialize_logger(std::shared_ptr logger) +{ + details::registry::instance().initialize_logger(std::move(logger)); +} + +SPDLOG_INLINE std::shared_ptr get(const std::string &name) +{ + return details::registry::instance().get(name); +} + +SPDLOG_INLINE void set_formatter(std::unique_ptr formatter) +{ + details::registry::instance().set_formatter(std::move(formatter)); +} + +SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type) +{ + set_formatter(std::unique_ptr(new pattern_formatter(std::move(pattern), time_type))); +} + +SPDLOG_INLINE void set_level(level::level_enum log_level) +{ + details::registry::instance().set_level(log_level); +} + +SPDLOG_INLINE void flush_on(level::level_enum log_level) +{ + details::registry::instance().flush_on(log_level); +} + +SPDLOG_INLINE void flush_every(std::chrono::seconds interval) +{ + details::registry::instance().flush_every(interval); +} + +SPDLOG_INLINE void set_error_handler(void (*handler)(const std::string &msg)) +{ + details::registry::instance().set_error_handler(handler); +} + +SPDLOG_INLINE void register_logger(std::shared_ptr logger) +{ + details::registry::instance().register_logger(std::move(logger)); +} + +SPDLOG_INLINE void apply_all(const std::function)> &fun) +{ + details::registry::instance().apply_all(fun); +} + +SPDLOG_INLINE void drop(const std::string &name) +{ + details::registry::instance().drop(name); +} + +SPDLOG_INLINE void drop_all() +{ + details::registry::instance().drop_all(); +} + +SPDLOG_INLINE void shutdown() +{ + details::registry::instance().shutdown(); +} + +SPDLOG_INLINE void set_automatic_registration(bool automatic_registation) +{ + details::registry::instance().set_automatic_registration(automatic_registation); +} + +SPDLOG_INLINE std::shared_ptr default_logger() +{ + return details::registry::instance().default_logger(); +} + +SPDLOG_INLINE spdlog::logger *default_logger_raw() +{ + return details::registry::instance().get_default_raw(); +} + +SPDLOG_INLINE void set_default_logger(std::shared_ptr default_logger) +{ + details::registry::instance().set_default_logger(std::move(default_logger)); +} + +} // namespace spdlog \ No newline at end of file diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index db67dba6..56a69e4c 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -11,7 +11,6 @@ #include "spdlog/common.h" #include "spdlog/details/registry.h" -#include "spdlog/details/pattern_formatter.h" #include "spdlog/logger.h" #include "spdlog/version.h" @@ -58,94 +57,52 @@ inline std::shared_ptr create(std::string logger_name, SinkArgs // auto console_sink = std::make_shared(); // auto console_logger = std::make_shared("console_logger", console_sink); // spdlog::initialize_logger(console_logger); -inline void initialize_logger(std::shared_ptr logger) -{ - details::registry::instance().initialize_logger(std::move(logger)); -} +void initialize_logger(std::shared_ptr logger); // Return an existing logger or nullptr if a logger with such name doesn't // exist. // example: spdlog::get("my_logger")->info("hello {}", "world"); -inline std::shared_ptr get(const std::string &name) -{ - return details::registry::instance().get(name); -} +std::shared_ptr get(const std::string &name); // Set global formatter. Each sink in each logger will get a clone of this object -inline void set_formatter(std::unique_ptr formatter) -{ - details::registry::instance().set_formatter(std::move(formatter)); -} +void set_formatter(std::unique_ptr formatter); // Set global format string. // example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v"); -inline void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local) -{ - set_formatter(std::unique_ptr(new pattern_formatter(std::move(pattern), time_type))); -} +void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local); // Set global logging level -inline void set_level(level::level_enum log_level) -{ - details::registry::instance().set_level(log_level); -} +void set_level(level::level_enum log_level); // Set global flush level -inline void flush_on(level::level_enum log_level) -{ - details::registry::instance().flush_on(log_level); -} +void flush_on(level::level_enum log_level); // Start/Restart a periodic flusher thread // Warning: Use only if all your loggers are thread safe! -inline void flush_every(std::chrono::seconds interval) -{ - details::registry::instance().flush_every(interval); -} +void flush_every(std::chrono::seconds interval); // Set global error handler -inline void set_error_handler(void (*handler)(const std::string &msg)) -{ - details::registry::instance().set_error_handler(handler); -} +void set_error_handler(void (*handler)(const std::string &msg)); // Register the given logger with the given name -inline void register_logger(std::shared_ptr logger) -{ - details::registry::instance().register_logger(std::move(logger)); -} +void register_logger(std::shared_ptr logger); // Apply a user defined function on all registered loggers // Example: // spdlog::apply_all([&](std::shared_ptr l) {l->flush();}); -inline void apply_all(const std::function)> &fun) -{ - details::registry::instance().apply_all(fun); -} +void apply_all(const std::function)> &fun); // Drop the reference to the given logger -inline void drop(const std::string &name) -{ - details::registry::instance().drop(name); -} +void drop(const std::string &name); // Drop all references from the registry -inline void drop_all() -{ - details::registry::instance().drop_all(); -} +void drop_all(); // stop any running threads started by spdlog and clean registry loggers -inline void shutdown() -{ - details::registry::instance().shutdown(); -} +void shutdown(); // Automatic registration of loggers when using spdlog::create() or spdlog::create_async -inline void set_automatic_registration(bool automatic_registation) -{ - details::registry::instance().set_automatic_registration(automatic_registation); -} +void set_automatic_registration(bool automatic_registation); // API for using default logger (stdout_color_mt), // e.g: spdlog::info("Message {}", 1); @@ -162,20 +119,11 @@ inline void set_automatic_registration(bool automatic_registation) // set_default_logger() *should not* be used concurrently with the default API. // e.g do not call set_default_logger() from one thread while calling spdlog::info() from another. -inline std::shared_ptr default_logger() -{ - return details::registry::instance().default_logger(); -} +std::shared_ptr default_logger(); -inline spdlog::logger *default_logger_raw() -{ - return details::registry::instance().get_default_raw(); -} +spdlog::logger *default_logger_raw(); -inline void set_default_logger(std::shared_ptr default_logger) -{ - details::registry::instance().set_default_logger(std::move(default_logger)); -} +void set_default_logger(std::shared_ptr default_logger); template inline void log(source_loc source, level::level_enum lvl, const char *fmt, const Args &... args) @@ -382,4 +330,8 @@ inline void critical(const wchar_t *fmt, const Args &... args) #define SPDLOG_CRITICAL(...) (void)0 #endif +#ifdef SPDLOG_HEADER_ONLY +#include "spdlog-inl.h" +#endif + #endif // SPDLOG_H diff --git a/src/spdlog.cpp b/src/spdlog.cpp index d4e78ffc..fd321535 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -9,6 +9,8 @@ #include #include "spdlog/spdlog.h" +#include "spdlog/spdlog-inl.h" + #include "spdlog/async.h" #include "spdlog/common.h"