Repository: gabime/spdlog
File: include/spdlog/sinks/qt_sinks.h (C/C++)

Code before:
            , color_range_end(color_range_end)
        {}
        int max_lines;
        QTextEdit *q_text_edit;
        QString payload;
        QTextCharFormat default_color;
        QTextCharFormat level_color;
        int color_range_start;
        int color_range_end;
    };

    void sink_it_(const details::log_msg &msg) override
    {
        memory_buf_t formatted;
        base_sink<Mutex>::formatter_->format(msg, formatted);

        const string_view_t str = string_view_t(formatted.data(), formatted.size());
        // apply the color to the color range in the formatted message.
        auto payload = QString::fromUtf8(str.data(), static_cast<int>(str.size()));
        // convert color ranges from byte index to character index.
        int color_range_start, color_range_end;
        color_range_start = static_cast<int>(msg.color_range_start);
        color_range_end = static_cast<int>(msg.color_range_end);
        if (msg.color_range_start < msg.color_range_end) {
            color_range_start = QString::fromUtf8(str.data(), msg.color_range_start).size();
            color_range_end = QString::fromUtf8(str.data(), msg.color_range_end).size();
        }

        invoke_params params{max_lines_,             // max lines
            qt_text_edit_,                           // text edit to append to
            std::move(payload),                      // text to append
            default_color_,                          // default color
            colors_.at(msg.level),                   // color to apply
            color_range_start,                       // color range start
            color_range_end};                        // color range end

        QMetaObject::invokeMethod(
            qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection);
    }

    void flush_() override {}

    // Add colored text to the text edit widget. This method is invoked in the GUI thread.
    // It is a static method to ensure that it is handled correctly even if the sink is destroyed prematurely
    // before it is invoked.

    static void invoke_method_(invoke_params params)
    {
        auto *document = params.q_text_edit->document();
        QTextCursor cursor(document);


Review comment (bug):
Why use `QString`?
I think you can use `QStringRef` here.

See: https://stackoverflow.com/questions/62910966/how-to-use-qstringview-or-qstringref-in-qt-widgets

Code after:
            , color_range_start(color_range_start)
            , color_range_end(color_range_end)
        {}
        int max_lines;
        QTextEdit *q_text_edit;
        QString payload;
        QTextCharFormat default_color;
        QTextCharFormat level_color;
        int color_range_start;
        int color_range_end;
    };

    void sink_it_(const details::log_msg &msg) override
    {
        memory_buf_t formatted;
        base_sink<Mutex>::formatter_->format(msg, formatted);

        const string_view_t str = string_view_t(formatted.data(), formatted.size());
        // apply the color to the color range in the formatted message.
        QString payload;
        int color_range_start, color_range_end;
        color_range_start = static_cast<int>(msg.color_range_start);
        color_range_end = static_cast<int>(msg.color_range_end);
        if (is_utf8_) {
            payload = QString::fromUtf8(str.data(), static_cast<int>(str.size()));
            // convert color ranges from byte index to character index.
            if (msg.color_range_start < msg.color_range_end) {
                color_range_start = QString::fromUtf8(str.data(), msg.color_range_start).size();
                color_range_end = QString::fromUtf8(str.data(), msg.color_range_end).size();
            }
        } else {
            payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
        }

        invoke_params params{max_lines_,             // max lines
            qt_text_edit_,                           // text edit to append to
            std::move(payload),                      // text to append
            default_color_,                          // default color
            colors_.at(msg.level),                   // color to apply
            color_range_start,                       // color range start
            color_range_end};                        // color range end

        QMetaObject::invokeMethod(
            qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection);
    }

    void flush_() override {}

    // Add colored text to the text edit widget. This method is invoked in the GUI thread.
    // It is a static method to ensure that it is handled correctly even if the sink is destroyed prematurely
    // before it is invoked.