Commit 623f59ce authored by gabime's avatar gabime

Added register_logger to spdlog.h to register manually created loggers

parent 8b4b5d27
...@@ -44,6 +44,13 @@ namespace details ...@@ -44,6 +44,13 @@ namespace details
class registry class registry
{ {
public: public:
void register_logger(std::shared_ptr<logger> logger, const std::string& logger_name)
{
_loggers[logger_name] = logger;
}
std::shared_ptr<logger> get(const std::string& logger_name) std::shared_ptr<logger> get(const std::string& logger_name)
{ {
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
...@@ -68,7 +75,8 @@ public: ...@@ -68,7 +75,8 @@ public:
if (_formatter) if (_formatter)
new_logger->set_formatter(_formatter); new_logger->set_formatter(_formatter);
new_logger->set_level(_level); new_logger->set_level(_level);
_loggers[logger_name] = new_logger; register_logger(new_logger, logger_name);
return new_logger; return new_logger;
} }
...@@ -93,6 +101,7 @@ public: ...@@ -93,6 +101,7 @@ public:
return create(logger_name, { sink }); return create(logger_name, { sink });
} }
void formatter(formatter_ptr f) void formatter(formatter_ptr f)
{ {
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
......
...@@ -32,6 +32,11 @@ ...@@ -32,6 +32,11 @@
#include "../sinks/stdout_sinks.h" #include "../sinks/stdout_sinks.h"
#include "../sinks/syslog_sink.h" #include "../sinks/syslog_sink.h"
inline void spdlog::register_logger(std::shared_ptr<logger> logger, const std::string& logger_name)
{
return details::registry::instance().register_logger(logger, logger_name);
}
inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name) inline std::shared_ptr<spdlog::logger> spdlog::get(const std::string& name)
{ {
return details::registry::instance().get(name); return details::registry::instance().get(name);
......
...@@ -74,7 +74,7 @@ void set_async_mode(size_t queue_size, const async_overflow_policy overflow_poli ...@@ -74,7 +74,7 @@ void set_async_mode(size_t queue_size, const async_overflow_policy overflow_poli
void set_sync_mode(); void set_sync_mode();
// //
// Create multi/single threaded rotating file logger // Create and register multi/single threaded rotating file logger
// //
std::shared_ptr<logger> 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<logger> 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<logger> 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<logger> 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);
...@@ -87,7 +87,7 @@ std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const st ...@@ -87,7 +87,7 @@ std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const st
// //
// Create stdout/stderr loggers // Create and register stdout/stderr loggers
// //
std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name); std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name);
std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name); std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name);
...@@ -96,25 +96,28 @@ std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name); ...@@ -96,25 +96,28 @@ std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name);
// //
// Create a syslog logger // Create and register a syslog logger
// //
#ifdef __linux__ #ifdef __linux__
std::shared_ptr<logger> syslog_logger(const std::string& logger_name, const std::string& ident = "", int syslog_option = 0); std::shared_ptr<logger> syslog_logger(const std::string& logger_name, const std::string& ident = "", int syslog_option = 0);
#endif #endif
// Create a logger with multiple sinks // Create and register a logger with multiple sinks
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks); std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
template<class It> template<class It>
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end); std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
// Create a logger with templated sink type // Create and register a logger with templated sink type
// Example: spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename", "txt"); // Example: spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename", "txt");
template <typename Sink, typename... Args> template <typename Sink, typename... Args>
std::shared_ptr<spdlog::logger> create(const std::string& logger_name, const Args&...); std::shared_ptr<spdlog::logger> create(const std::string& logger_name, const Args&...);
// Register the given logger with the given name
void register_logger(std::shared_ptr<logger> logger, const std::string& logger_name);
// Drop the reference to the given logger // Drop the reference to the given logger
void drop(const std::string &name); void drop(const std::string &name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment