mirror of
https://github.com/gabime/spdlog.git
synced 2025-03-29 17:32:41 +08:00
Fix encoding issue in qt_sinks (#2862)
Added support for utf8 in qt_color_sink
This commit is contained in:
parent
3aceda041b
commit
8014d6c31a
@ -66,14 +66,15 @@ private:
|
|||||||
// Color location is determined by the sink log pattern like in the rest of spdlog sinks.
|
// 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).
|
// 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.
|
// 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>
|
template<typename Mutex>
|
||||||
class qt_color_sink : public base_sink<Mutex>
|
class qt_color_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
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)
|
: qt_text_edit_(qt_text_edit)
|
||||||
, max_lines_(max_lines)
|
, max_lines_(max_lines)
|
||||||
|
, is_utf8_(is_utf8)
|
||||||
{
|
{
|
||||||
if (!qt_text_edit_)
|
if (!qt_text_edit_)
|
||||||
{
|
{
|
||||||
@ -162,15 +163,28 @@ protected:
|
|||||||
|
|
||||||
const string_view_t str = string_view_t(formatted.data(), formatted.size());
|
const string_view_t str = string_view_t(formatted.data(), formatted.size());
|
||||||
// apply the color to the color range in the formatted message.
|
// 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
|
invoke_params params{max_lines_, // max lines
|
||||||
qt_text_edit_, // text edit to append to
|
qt_text_edit_, // text edit to append to
|
||||||
std::move(payload), // text to append
|
std::move(payload), // text to append
|
||||||
default_color_, // default color
|
default_color_, // default color
|
||||||
colors_.at(msg.level), // color to apply
|
colors_.at(msg.level), // color to apply
|
||||||
static_cast<int>(msg.color_range_start), // color range start
|
color_range_start, // color range start
|
||||||
static_cast<int>(msg.color_range_end)}; // color range end
|
color_range_end}; // color range end
|
||||||
|
|
||||||
QMetaObject::invokeMethod(
|
QMetaObject::invokeMethod(
|
||||||
qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection);
|
qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection);
|
||||||
@ -219,6 +233,7 @@ protected:
|
|||||||
|
|
||||||
QTextEdit *qt_text_edit_;
|
QTextEdit *qt_text_edit_;
|
||||||
int max_lines_;
|
int max_lines_;
|
||||||
|
bool is_utf8_;
|
||||||
QTextCharFormat default_color_;
|
QTextCharFormat default_color_;
|
||||||
std::array<QTextCharFormat, level::n_levels> colors_;
|
std::array<QTextCharFormat, level::n_levels> colors_;
|
||||||
};
|
};
|
||||||
@ -278,15 +293,15 @@ inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QObj
|
|||||||
|
|
||||||
// log to QTextEdit with colorize output
|
// log to QTextEdit with colorize output
|
||||||
template<typename Factory = spdlog::synchronous_factory>
|
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>
|
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
|
} // namespace spdlog
|
||||||
|
Loading…
Reference in New Issue
Block a user