Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encoding issue in qt_sinks #2862

Merged
merged 6 commits into from
Sep 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions include/spdlog/sinks/qt_sinks.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ class qt_sink : public base_sink<Mutex>
// 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<typename Mutex>
class qt_color_sink : public base_sink<Mutex>
{
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_)
{
Expand Down Expand Up @@ -162,15 +163,28 @@ class qt_color_sink : public base_sink<Mutex>

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<int>(str.size()));
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
static_cast<int>(msg.color_range_start), // color range start
static_cast<int>(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);
Expand Down Expand Up @@ -219,6 +233,7 @@ class qt_color_sink : public base_sink<Mutex>

QTextEdit *qt_text_edit_;
int max_lines_;
bool is_utf8_;
QTextCharFormat default_color_;
std::array<QTextCharFormat, level::n_levels> colors_;
};
Expand Down Expand Up @@ -278,15 +293,15 @@ inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QObj

// log to QTextEdit with colorize output
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_color_logger_mt(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines)
inline std::shared_ptr<logger> qt_color_logger_mt(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines, bool is_utf8 = false)
{
return Factory::template create<sinks::qt_color_sink_mt>(logger_name, qt_text_edit, max_lines);
return Factory::template create<sinks::qt_color_sink_mt>(logger_name, qt_text_edit, max_lines, false, is_utf8);
}

template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qt_color_logger_st(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines)
inline std::shared_ptr<logger> qt_color_logger_st(const std::string &logger_name, QTextEdit *qt_text_edit, int max_lines, bool is_utf8 = false)
{
return Factory::template create<sinks::qt_color_sink_st>(logger_name, qt_text_edit, max_lines);
return Factory::template create<sinks::qt_color_sink_st>(logger_name, qt_text_edit, max_lines, false, is_utf8);
}

} // namespace spdlog