Repository: gabime/spdlog
File: include/spdlog/details/registry-inl.h (C/C++)

Code before:

    if (backtrace_n_messages_ > 0) {
        new_logger->enable_backtrace(backtrace_n_messages_);
    }

    if (automatic_registration_) {
        register_logger_(std::move(new_logger));
    }
}

SPDLOG_INLINE std::shared_ptr<logger> registry::get(const std::string &logger_name) {
    std::lock_guard<std::mutex> lock(logger_map_mutex_);
    auto found = loggers_.find(logger_name);
    return found == loggers_.end() ? nullptr : found->second;
}

#if __cplusplus >= 201703L  // C++17
SPDLOG_INLINE std::shared_ptr<logger> registry::get(std::string_view logger_name) {
    std::lock_guard<std::mutex> lock(logger_map_mutex_);
    using key_value_pair = std::pair<std::string, std::shared_ptr<spdlog::logger>>;
    const auto match = [logger_name](const key_value_pair &item) {
        return item.first == logger_name;
    };
    const auto found = std::find_if(loggers_.cbegin(), loggers_.cend(), match);
    return found == loggers_.end() ? nullptr : found->second;
}

SPDLOG_INLINE std::shared_ptr<logger> registry::get(const char *logger_name) {
    return get(std::string_view(logger_name));
}
#endif

SPDLOG_INLINE std::shared_ptr<logger> registry::default_logger() {
    std::lock_guard<std::mutex> lock(logger_map_mutex_);
    return default_logger_;
}

// Return raw ptr to the default logger.
// To be used directly by the spdlog default api (e.g. spdlog::info)
// This make the default API faster, but cannot be used concurrently with set_default_logger().
// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
SPDLOG_INLINE logger *registry::get_default_raw() { return default_logger_.get(); }

// set default logger.
// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map.
SPDLOG_INLINE void registry::set_default_logger(std::shared_ptr<logger> new_default_logger) {
    std::lock_guard<std::mutex> lock(logger_map_mutex_);
    // remove previous default logger from the map
    if (default_logger_ != nullptr) {
        loggers_.erase(default_logger_->name());
    }

Review comment (bug):
since this is cpp17, a much shorter version would be

```

std::lock_guard<std::mutex> lock(logger_map_mutex_);
for (auto const& [key, val] : loggers_) {
  if(logger_name == key) return val;
}
return nullptr;
```

And then also no need to `#include <algorithm>`

Code after:
    if (backtrace_n_messages_ > 0) {
        new_logger->enable_backtrace(backtrace_n_messages_);
    }

    if (automatic_registration_) {
        register_logger_(std::move(new_logger));
    }
}

SPDLOG_INLINE std::shared_ptr<logger> registry::get(const std::string &logger_name) {
    std::lock_guard<std::mutex> lock(logger_map_mutex_);
    auto found = loggers_.find(logger_name);
    return found == loggers_.end() ? nullptr : found->second;
}

#if __cplusplus >= 201703L  // C++17
SPDLOG_INLINE std::shared_ptr<logger> registry::get(std::string_view logger_name) {
    std::lock_guard<std::mutex> lock(logger_map_mutex_);
    for (const auto &[key, val] : loggers_) {
        if (key == logger_name) {
            return val;
        }
    }
    return nullptr;
}

SPDLOG_INLINE std::shared_ptr<logger> registry::get(const char *logger_name) {
    return get(std::string_view(logger_name));
}
#endif

SPDLOG_INLINE std::shared_ptr<logger> registry::default_logger() {
    std::lock_guard<std::mutex> lock(logger_map_mutex_);
    return default_logger_;
}

// Return raw ptr to the default logger.
// To be used directly by the spdlog default api (e.g. spdlog::info)
// This make the default API faster, but cannot be used concurrently with set_default_logger().
// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
SPDLOG_INLINE logger *registry::get_default_raw() { return default_logger_.get(); }

// set default logger.
// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map.
SPDLOG_INLINE void registry::set_default_logger(std::shared_ptr<logger> new_default_logger) {
    std::lock_guard<std::mutex> lock(logger_map_mutex_);
    // remove previous default logger from the map
    if (default_logger_ != nullptr) {
        loggers_.erase(default_logger_->name());
    }
    if (new_default_logger != nullptr) {