diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index 7bcde900..00d232aa 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -53,20 +53,20 @@ class async_logger :public logger { public: template - async_logger(const std::string& name, + async_logger(const SPDLOG_NAME_TYPE_REF name, const It& begin, const It& end, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function& worker_warmup_cb = nullptr); - async_logger(const std::string& logger_name, + async_logger(const SPDLOG_NAME_TYPE_REF logger_name, sinks_init_list sinks, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function& worker_warmup_cb = nullptr); - async_logger(const std::string& logger_name, + async_logger(const SPDLOG_NAME_TYPE_REF logger_name, sink_ptr single_sink, size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index e113e1d2..65341ba1 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -34,7 +34,7 @@ template -inline spdlog::async_logger::async_logger(const std::string& logger_name, +inline spdlog::async_logger::async_logger(const SPDLOG_NAME_TYPE_REF logger_name, const It& begin, const It& end, size_t queue_size, @@ -45,14 +45,14 @@ inline spdlog::async_logger::async_logger(const std::string& logger_name, { } -inline spdlog::async_logger::async_logger(const std::string& logger_name, +inline spdlog::async_logger::async_logger(const SPDLOG_NAME_TYPE_REF logger_name, sinks_init_list sinks, size_t queue_size, const async_overflow_policy overflow_policy, const std::function& worker_warmup_cb) : async_logger(logger_name, sinks.begin(), sinks.end(), queue_size, overflow_policy, worker_warmup_cb) {} -inline spdlog::async_logger::async_logger(const std::string& logger_name, +inline spdlog::async_logger::async_logger(const SPDLOG_NAME_TYPE_REF logger_name, sink_ptr single_sink, size_t queue_size, const async_overflow_policy overflow_policy, diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 1a920c03..34152164 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -33,7 +33,7 @@ // create logger with given name, sinks and the default pattern formatter // all other ctors will call this one template -inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) : +inline spdlog::logger::logger(const SPDLOG_NAME_TYPE_REF logger_name, const It& begin, const It& end) : _name(logger_name), _sinks(begin, end), _formatter(std::make_shared("%+")) @@ -44,12 +44,12 @@ inline spdlog::logger::logger(const std::string& logger_name, const It& begin, c } // ctor with sinks as init list -inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list) : +inline spdlog::logger::logger(const SPDLOG_NAME_TYPE_REF logger_name, sinks_init_list sinks_list) : logger(logger_name, sinks_list.begin(), sinks_list.end()) {} // ctor with single sink -inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink) : +inline spdlog::logger::logger(const SPDLOG_NAME_TYPE_REF logger_name, spdlog::sink_ptr single_sink) : logger(logger_name, { single_sink }) {} @@ -273,7 +273,7 @@ inline spdlog::details::line_logger spdlog::logger::force_log(level::level_enum // // name and level // -inline const std::string& spdlog::logger::name() const +inline const SPDLOG_NAME_TYPE_REF spdlog::logger::name() const { return _name; } diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 5f708743..8d271345 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -52,7 +52,7 @@ public: } - std::shared_ptr get(const std::string& logger_name) + std::shared_ptr get(const SPDLOG_NAME_TYPE_REF logger_name) { std::lock_guard lock(_mutex); auto found = _loggers.find(logger_name); @@ -60,7 +60,7 @@ public: } template - std::shared_ptr create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) + std::shared_ptr create(const SPDLOG_NAME_TYPE_REF logger_name, const It& sinks_begin, const It& sinks_end) { std::shared_ptr new_logger; @@ -81,7 +81,7 @@ public: return new_logger; } - void drop(const std::string& logger_name) + void drop(const SPDLOG_NAME_TYPE_REF logger_name) { std::lock_guard lock(_mutex); _loggers.erase(logger_name); @@ -92,12 +92,12 @@ public: std::lock_guard lock(_mutex); _loggers.clear(); } - std::shared_ptr create(const std::string& logger_name, sinks_init_list sinks) + std::shared_ptr create(const SPDLOG_NAME_TYPE_REF logger_name, sinks_init_list sinks) { return create(logger_name, sinks.begin(), sinks.end()); } - std::shared_ptr create(const std::string& logger_name, sink_ptr sink) + std::shared_ptr create(const SPDLOG_NAME_TYPE_REF logger_name, sink_ptr sink) { return create(logger_name, { sink }); } @@ -153,14 +153,14 @@ private: { auto logger_name = logger->name(); if (_loggers.find(logger_name) != std::end(_loggers)) - throw spdlog_ex("logger with name " + logger_name + " already exists"); + throw spdlog_ex("logger with name already exists"); _loggers[logger->name()] = logger; } registry() = default; registry(const registry&) = delete; registry& operator=(const registry&) = delete; std::mutex _mutex; - std::unordered_map > _loggers; + std::unordered_map > _loggers; formatter_ptr _formatter; level::level_enum _level = level::info; bool _async_mode = false; diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 5e1bc73a..09916137 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -37,62 +37,62 @@ inline void spdlog::register_logger(std::shared_ptr logger) return details::registry::instance().register_logger(logger); } -inline std::shared_ptr spdlog::get(const std::string& name) +inline std::shared_ptr spdlog::get(const SPDLOG_NAME_TYPE_REF name) { return details::registry::instance().get(name); } -inline void spdlog::drop(const std::string &name) +inline void spdlog::drop(const SPDLOG_NAME_TYPE_REF name) { details::registry::instance().drop(name); } // Create multi/single threaded rotating file logger -inline std::shared_ptr spdlog::rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush) +inline std::shared_ptr spdlog::rotating_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush) { return create(logger_name, filename, "txt", max_file_size, max_files, force_flush); } -inline std::shared_ptr spdlog::rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush) +inline std::shared_ptr spdlog::rotating_logger_st(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush) { return create(logger_name, filename, "txt", max_file_size, max_files, force_flush); } // Create file logger which creates new file at midnight): -inline std::shared_ptr spdlog::daily_logger_mt(const std::string& logger_name, const std::string& filename, int hour, int minute, bool force_flush) +inline std::shared_ptr spdlog::daily_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, int hour, int minute, bool force_flush) { return create(logger_name, filename, "txt", hour, minute, force_flush); } -inline std::shared_ptr spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, int hour, int minute, bool force_flush) +inline std::shared_ptr spdlog::daily_logger_st(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, int hour, int minute, bool force_flush) { return create(logger_name, filename, "txt", hour, minute, force_flush); } // Create stdout/stderr loggers -inline std::shared_ptr spdlog::stdout_logger_mt(const std::string& logger_name) +inline std::shared_ptr spdlog::stdout_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name) { return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_mt::instance()); } -inline std::shared_ptr spdlog::stdout_logger_st(const std::string& logger_name) +inline std::shared_ptr spdlog::stdout_logger_st(const SPDLOG_NAME_TYPE_REF logger_name) { return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_st::instance()); } -inline std::shared_ptr spdlog::stderr_logger_mt(const std::string& logger_name) +inline std::shared_ptr spdlog::stderr_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name) { return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_mt::instance()); } -inline std::shared_ptr spdlog::stderr_logger_st(const std::string& logger_name) +inline std::shared_ptr spdlog::stderr_logger_st(const SPDLOG_NAME_TYPE_REF logger_name) { return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_st::instance()); } #ifdef __linux__ // Create syslog logger -inline std::shared_ptr spdlog::syslog_logger(const std::string& logger_name, const std::string& syslog_ident, int syslog_option) +inline std::shared_ptr spdlog::syslog_logger(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& syslog_ident, int syslog_option) { return create(logger_name, syslog_ident, syslog_option); } @@ -101,14 +101,14 @@ inline std::shared_ptr spdlog::syslog_logger(const std::string& //Create logger with multiple sinks -inline std::shared_ptr spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks) +inline std::shared_ptr spdlog::create(const SPDLOG_NAME_TYPE_REF logger_name, spdlog::sinks_init_list sinks) { return details::registry::instance().create(logger_name, sinks); } template -inline std::shared_ptr spdlog::create(const std::string& logger_name, const Args&... args) +inline std::shared_ptr spdlog::create(const SPDLOG_NAME_TYPE_REF logger_name, const Args&... args) { sink_ptr sink = std::make_shared(args...); return details::registry::instance().create(logger_name, { sink }); @@ -116,7 +116,7 @@ inline std::shared_ptr spdlog::create(const std::string& logger_ template -inline std::shared_ptr spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) +inline std::shared_ptr spdlog::create(const SPDLOG_NAME_TYPE_REF logger_name, const It& sinks_begin, const It& sinks_end) { return details::registry::instance().create(logger_name, sinks_begin, sinks_end); } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index cf97fc80..a9316d02 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -47,10 +47,10 @@ class line_logger; class logger { public: - logger(const std::string& logger_name, sink_ptr single_sink); - logger(const std::string& name, sinks_init_list); + logger(const SPDLOG_NAME_TYPE_REF logger_name, sink_ptr single_sink); + logger(const SPDLOG_NAME_TYPE_REF name, sinks_init_list); template - logger(const std::string& name, const It& begin, const It& end); + logger(const SPDLOG_NAME_TYPE_REF name, const It& begin, const It& end); virtual ~logger(); logger(const logger&) = delete; @@ -59,7 +59,7 @@ public: void set_level(level::level_enum); level::level_enum level() const; - const std::string& name() const; + const SPDLOG_NAME_TYPE_REF name() const; bool should_log(level::level_enum) const; // logger.info(cppformat_string, arg1, arg2, arg3, ...) call style @@ -120,7 +120,7 @@ protected: friend details::line_logger; - std::string _name; + SPDLOG_NAME_TYPE _name; std::vector _sinks; formatter_ptr _formatter; std::atomic_int _level; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 8601555f..27bec773 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -29,6 +29,10 @@ #pragma once #include "tweakme.h" +#ifndef SPDLOG_NAME_TYPE +#define SPDLOG_NAME_TYPE std::string +#define SPDLOG_NAME_TYPE_REF std::string& +#endif #include "common.h" #include "logger.h" @@ -41,7 +45,7 @@ namespace spdlog // auto logger = spdlog::get("mylog"); // logger.info("This is another message" , x, y, z); // logger.info() << "This is another message" << x << y << z; -std::shared_ptr get(const std::string& name); +std::shared_ptr get(const SPDLOG_NAME_TYPE_REF name); // // Set global formatting @@ -76,50 +80,50 @@ void set_sync_mode(); // // Create and register multi/single threaded rotating file logger // -std::shared_ptr rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false); -std::shared_ptr rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false); +std::shared_ptr rotating_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false); +std::shared_ptr rotating_logger_st(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false); // // Create file logger which creates new file on the given time (default in midnight): // -std::shared_ptr daily_logger_mt(const std::string& logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false); -std::shared_ptr daily_logger_st(const std::string& logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false); +std::shared_ptr daily_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false); +std::shared_ptr daily_logger_st(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false); // // Create and register stdout/stderr loggers // -std::shared_ptr stdout_logger_mt(const std::string& logger_name); -std::shared_ptr stdout_logger_st(const std::string& logger_name); -std::shared_ptr stderr_logger_mt(const std::string& logger_name); -std::shared_ptr stderr_logger_st(const std::string& logger_name); +std::shared_ptr stdout_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name); +std::shared_ptr stdout_logger_st(const SPDLOG_NAME_TYPE_REF logger_name); +std::shared_ptr stderr_logger_mt(const SPDLOG_NAME_TYPE_REF logger_name); +std::shared_ptr stderr_logger_st(const SPDLOG_NAME_TYPE_REF logger_name); // // Create and register a syslog logger // #ifdef __linux__ -std::shared_ptr syslog_logger(const std::string& logger_name, const std::string& ident = "", int syslog_option = 0); +std::shared_ptr syslog_logger(const SPDLOG_NAME_TYPE_REF logger_name, const std::string& ident = "", int syslog_option = 0); #endif // Create and register a logger with multiple sinks -std::shared_ptr create(const std::string& logger_name, sinks_init_list sinks); +std::shared_ptr create(const SPDLOG_NAME_TYPE_REF logger_name, sinks_init_list sinks); template -std::shared_ptr create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end); +std::shared_ptr create(const SPDLOG_NAME_TYPE_REF logger_name, const It& sinks_begin, const It& sinks_end); // Create and register a logger with templated sink type // Example: spdlog::create("mylog", "dailylog_filename", "txt"); template -std::shared_ptr create(const std::string& logger_name, const Args&...); +std::shared_ptr create(const SPDLOG_NAME_TYPE_REF logger_name, const Args&...); // Register the given logger with the given name void register_logger(std::shared_ptr logger); // Drop the reference to the given logger -void drop(const std::string &name); +void drop(const SPDLOG_NAME_TYPE_REF name); // Drop all references void drop_all(); diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index 20e2c5e4..64942a38 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -64,3 +64,10 @@ // #define SPDLOG_DEBUG_ON // #define SPDLOG_TRACE_ON /////////////////////////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to enable the SPDLOG_DEBUG/SPDLOG_TRACE macros +// #define SPDLOG_NAME_TYPE int +// #define SPDLOG_NAME_TYPE_REF int +///////////////////////////////////////////////////////////////////////////////