diff --git a/include/spdlog/sinks/qt_sinks.h b/include/spdlog/sinks/qt_sinks.h index 565a292c7..b128d6565 100644 --- a/include/spdlog/sinks/qt_sinks.h +++ b/include/spdlog/sinks/qt_sinks.h @@ -66,14 +66,15 @@ class qt_sink : public base_sink // Color location is determined by the sink log pattern like in the rest of spdlog sinks. // Colors can be modified if needed using sink->set_color(level, qtTextCharFormat). // max_lines is the maximum number of lines that the sink will hold before removing the oldest lines. -// Note: Only ascii (latin1) is supported by this sink. +// By default, only ascii (latin1) is supported by this sink. Set is_utf8 to true if utf8 support is needed. template class qt_color_sink : public base_sink { public: - qt_color_sink(QTextEdit *qt_text_edit, int max_lines, bool dark_colors=false) + qt_color_sink(QTextEdit *qt_text_edit, int max_lines, bool dark_colors=false, bool is_utf8=false) : qt_text_edit_(qt_text_edit) , max_lines_(max_lines) + , is_utf8_(is_utf8) { if (!qt_text_edit_) { @@ -162,15 +163,28 @@ class qt_color_sink : public base_sink 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::fromLatin1(str.data(), static_cast(str.size())); + QString payload; + int color_range_start, color_range_end; + color_range_start = static_cast(msg.color_range_start); + color_range_end = static_cast(msg.color_range_end); + if (is_utf8_) { + payload = QString::fromUtf8(str.data(), static_cast(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(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 - static_cast(msg.color_range_start), // color range start - static_cast(msg.color_range_end)}; // color range end + color_range_start, // color range start + color_range_end}; // color range end QMetaObject::invokeMethod( qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection); @@ -219,6 +233,7 @@ class qt_color_sink : public base_sink QTextEdit *qt_text_edit_; int max_lines_; + bool is_utf8_; QTextCharFormat default_color_; std::array colors_; }; @@ -278,15 +293,15 @@ inline std::shared_ptr qt_logger_st(const std::string &logger_name, QObj // log to QTextEdit with colorize output template -inline std::shared_ptr qt_color_logger_mt(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines) +inline std::shared_ptr qt_color_logger_mt(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines, bool is_utf8 = false) { - return Factory::template create(logger_name, qt_text_edit, max_lines); + return Factory::template create(logger_name, qt_text_edit, max_lines, false, is_utf8); } template -inline std::shared_ptr qt_color_logger_st(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines) +inline std::shared_ptr qt_color_logger_st(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines, bool is_utf8 = false) { - return Factory::template create(logger_name, qt_text_edit, max_lines); + return Factory::template create(logger_name, qt_text_edit, max_lines, false, is_utf8); } } // namespace spdlog