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

Code before:
};

// Class for formatting Mapped Diagnostic Context (MDC) in log messages.
// Example: [logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message
#ifndef SPDLOG_NO_TLS
template <typename ScopedPadder>
class mdc_formatter : public flag_formatter {
public:
    explicit mdc_formatter(padding_info padinfo)
        : flag_formatter(padinfo) {}

    void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override {
        auto &mdc_map = mdc::get_context();
        if (mdc_map.empty()) {
            ScopedPadder p(0, padinfo_, dest);
            return;
        } else {
            format_mdc(mdc_map, dest);
        }
    }

    void format_mdc(const mdc::mdc_map_t &mdc_map, memory_buf_t &dest) {
        auto last_element = --mdc_map.end();
        for (auto it = mdc_map.begin(); it != mdc_map.end(); ++it) {
            auto &pair = *it;
            const auto &key = pair.first;
            const auto &value = pair.second;
            size_t content_size = key.size() + value.size() + 1;  // 1 for ':'

            if (it != last_element) {
                content_size++;  // 1 for ' '
            }

            ScopedPadder p(content_size, padinfo_, dest);
            fmt_helper::append_string_view(key, dest);
            fmt_helper::append_string_view(":", dest);
            fmt_helper::append_string_view(value, dest);
            if (it != last_element) {
                fmt_helper::append_string_view(" ", dest);
            }
        }
    }
};
#endif

// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
class full_formatter final : public flag_formatter {
public:
    explicit full_formatter(padding_info padinfo)
        : flag_formatter(padinfo) {}

Review comment (refactor):
While removing the intermediate `pair` variable is valid, the refactored code reduces readability. The previous approach with an explicit `pair` reference made the code's intent clearer at first glance. Consider whether the minimal gain in removing one line justifies the decreased clarity, especially since the original `pair` variable would benefit from being marked `const` as mentioned in the PR description.

Code after:
};

// Class for formatting Mapped Diagnostic Context (MDC) in log messages.
// Example: [logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message
#ifndef SPDLOG_NO_TLS
template <typename ScopedPadder>
class mdc_formatter : public flag_formatter {
public:
    explicit mdc_formatter(padding_info padinfo)
        : flag_formatter(padinfo) {}

    void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override {
        const auto &mdc_map = mdc::get_context();
        if (mdc_map.empty()) {
            ScopedPadder p(0, padinfo_, dest);
            return;
        } else {
            format_mdc(mdc_map, dest);
        }
    }

    void format_mdc(const mdc::mdc_map_t &mdc_map, memory_buf_t &dest) {
        const auto last_element = std::prev(mdc_map.end());
        for (auto it = mdc_map.begin(); it != mdc_map.end(); ++it) {
            const auto &key = it->first;
            const auto &value = it->second;
            size_t content_size = key.size() + value.size() + 1;  // 1 for ':'

            if (it != last_element) {
                content_size++;  // 1 for ' '
            }

            ScopedPadder p(content_size, padinfo_, dest);
            fmt_helper::append_string_view(key, dest);
            fmt_helper::append_string_view(":", dest);
            fmt_helper::append_string_view(value, dest);
            if (it != last_element) {
                fmt_helper::append_string_view(" ", dest);
            }
        }
    }
};
#endif

// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
class full_formatter final : public flag_formatter {
public:
    explicit full_formatter(padding_info padinfo)
        : flag_formatter(padinfo) {}