|
|
|
@ -11,7 +11,7 @@
|
|
|
|
|
// auto edit = new QTextEdit();
|
|
|
|
|
// setCentralWidget(edit);
|
|
|
|
|
// auto logger = spdlog::qt_color_logger_mt("my_logger", my_edit_widget);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Warning: those sinks won't be notified if the target widget is destroyed.
|
|
|
|
|
// If the widget's lifetime can be shorter than the logger's one, you should provide some permanent QObject,
|
|
|
|
|
// and then use a standard signal/slot.
|
|
|
|
@ -72,7 +72,6 @@ private:
|
|
|
|
|
QMetaMethod meta_method_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QT color sink to QTextEdit.
|
|
|
|
|
// 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).
|
|
|
|
@ -81,104 +80,104 @@ template<typename Mutex>
|
|
|
|
|
class qt_color_sink : public base_sink<Mutex>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
qt_color_sink(QTextEdit *qt_text_edit) : qt_text_edit_(qt_text_edit)
|
|
|
|
|
qt_color_sink(QTextEdit *qt_text_edit)
|
|
|
|
|
: qt_text_edit_(qt_text_edit)
|
|
|
|
|
{
|
|
|
|
|
if (!qt_text_edit_)
|
|
|
|
|
{
|
|
|
|
|
if(!qt_text_edit_)
|
|
|
|
|
{
|
|
|
|
|
throw_spdlog_ex("qt_color_text_sink: text_edit is null");
|
|
|
|
|
}
|
|
|
|
|
default_color_ = qt_text_edit_->currentCharFormat();
|
|
|
|
|
// set colors
|
|
|
|
|
QTextCharFormat format;
|
|
|
|
|
// trace
|
|
|
|
|
format.setForeground(Qt::gray);
|
|
|
|
|
colors_.at(level::trace) = format;
|
|
|
|
|
// debug
|
|
|
|
|
format.setForeground(Qt::cyan);
|
|
|
|
|
colors_.at(level::debug) = format;
|
|
|
|
|
// info
|
|
|
|
|
format.setForeground(Qt::green);
|
|
|
|
|
colors_.at(level::info) = format;
|
|
|
|
|
// warn
|
|
|
|
|
format.setForeground(Qt::yellow);
|
|
|
|
|
colors_.at(level::warn) = format;
|
|
|
|
|
// err
|
|
|
|
|
format.setForeground(Qt::red);
|
|
|
|
|
colors_.at(level::err) = format;
|
|
|
|
|
// critical
|
|
|
|
|
format.setForeground(Qt::white);
|
|
|
|
|
format.setBackground(Qt::red);
|
|
|
|
|
colors_.at(level::critical) = format;
|
|
|
|
|
throw_spdlog_ex("qt_color_text_sink: text_edit is null");
|
|
|
|
|
}
|
|
|
|
|
default_color_ = qt_text_edit_->currentCharFormat();
|
|
|
|
|
// set colors
|
|
|
|
|
QTextCharFormat format;
|
|
|
|
|
// trace
|
|
|
|
|
format.setForeground(Qt::gray);
|
|
|
|
|
colors_.at(level::trace) = format;
|
|
|
|
|
// debug
|
|
|
|
|
format.setForeground(Qt::cyan);
|
|
|
|
|
colors_.at(level::debug) = format;
|
|
|
|
|
// info
|
|
|
|
|
format.setForeground(Qt::green);
|
|
|
|
|
colors_.at(level::info) = format;
|
|
|
|
|
// warn
|
|
|
|
|
format.setForeground(Qt::yellow);
|
|
|
|
|
colors_.at(level::warn) = format;
|
|
|
|
|
// err
|
|
|
|
|
format.setForeground(Qt::red);
|
|
|
|
|
colors_.at(level::err) = format;
|
|
|
|
|
// critical
|
|
|
|
|
format.setForeground(Qt::white);
|
|
|
|
|
format.setBackground(Qt::red);
|
|
|
|
|
colors_.at(level::critical) = format;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~qt_color_sink()
|
|
|
|
|
{
|
|
|
|
|
flush_();
|
|
|
|
|
}
|
|
|
|
|
~qt_color_sink()
|
|
|
|
|
{
|
|
|
|
|
flush_();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_default_color(QTextCharFormat format)
|
|
|
|
|
{
|
|
|
|
|
//std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
|
|
|
|
default_color_ = format;
|
|
|
|
|
}
|
|
|
|
|
void set_default_color(QTextCharFormat format)
|
|
|
|
|
{
|
|
|
|
|
// std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
|
|
|
|
default_color_ = format;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set_level_color(level::level_enum color_level, QTextCharFormat format)
|
|
|
|
|
{
|
|
|
|
|
//std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
|
|
|
|
colors_.at(static_cast<size_t>(color_level)) = format;
|
|
|
|
|
}
|
|
|
|
|
void set_level_color(level::level_enum color_level, QTextCharFormat format)
|
|
|
|
|
{
|
|
|
|
|
// std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
|
|
|
|
colors_.at(static_cast<size_t>(color_level)) = format;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextCharFormat& get_level_color(level::level_enum color_level)
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
|
|
|
|
return colors_.at(static_cast<size_t>(color_level));
|
|
|
|
|
}
|
|
|
|
|
QTextCharFormat &get_level_color(level::level_enum color_level)
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
|
|
|
|
return colors_.at(static_cast<size_t>(color_level));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextCharFormat& get_default_color()
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
|
|
|
|
return default_color_;
|
|
|
|
|
}
|
|
|
|
|
QTextCharFormat &get_default_color()
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
|
|
|
|
return default_color_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void sink_it_(const details::log_msg &msg) override
|
|
|
|
|
void sink_it_(const details::log_msg &msg) override
|
|
|
|
|
{
|
|
|
|
|
memory_buf_t formatted;
|
|
|
|
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
|
|
|
|
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()));
|
|
|
|
|
if (msg.color_range_end > msg.color_range_start)
|
|
|
|
|
{
|
|
|
|
|
memory_buf_t formatted;
|
|
|
|
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
|
|
|
|
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()));
|
|
|
|
|
if(msg.color_range_end > msg.color_range_start)
|
|
|
|
|
{
|
|
|
|
|
QTextCursor cursor(qt_text_edit_->document());
|
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
|
|
|
|
|
|
// insert the text before the color range
|
|
|
|
|
cursor.setCharFormat(default_color_);
|
|
|
|
|
cursor.insertText(payload.left(msg.color_range_start));
|
|
|
|
|
|
|
|
|
|
// insert the colorized text
|
|
|
|
|
auto color = colors_.at(static_cast<size_t>(msg.level));
|
|
|
|
|
cursor.setCharFormat(color);
|
|
|
|
|
cursor.insertText(payload.mid(msg.color_range_start, msg.color_range_end - msg.color_range_start));
|
|
|
|
|
|
|
|
|
|
// insert the text after the color range with default format
|
|
|
|
|
cursor.setCharFormat(default_color_);
|
|
|
|
|
cursor.insertText(payload.mid(msg.color_range_end));
|
|
|
|
|
}
|
|
|
|
|
else // no color range
|
|
|
|
|
{
|
|
|
|
|
qt_text_edit_->append(payload.trimmed());
|
|
|
|
|
}
|
|
|
|
|
QTextCursor cursor(qt_text_edit_->document());
|
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
|
|
|
|
|
|
// insert the text before the color range
|
|
|
|
|
cursor.setCharFormat(default_color_);
|
|
|
|
|
cursor.insertText(payload.left(msg.color_range_start));
|
|
|
|
|
|
|
|
|
|
// insert the colorized text
|
|
|
|
|
auto color = colors_.at(static_cast<size_t>(msg.level));
|
|
|
|
|
cursor.setCharFormat(color);
|
|
|
|
|
cursor.insertText(payload.mid(msg.color_range_start, msg.color_range_end - msg.color_range_start));
|
|
|
|
|
|
|
|
|
|
// insert the text after the color range with default format
|
|
|
|
|
cursor.setCharFormat(default_color_);
|
|
|
|
|
cursor.insertText(payload.mid(msg.color_range_end));
|
|
|
|
|
}
|
|
|
|
|
else // no color range
|
|
|
|
|
{
|
|
|
|
|
qt_text_edit_->append(payload.trimmed());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void flush_() override {}
|
|
|
|
|
QTextEdit *qt_text_edit_;
|
|
|
|
|
void flush_() override {}
|
|
|
|
|
QTextEdit *qt_text_edit_;
|
|
|
|
|
QTextCharFormat default_color_;
|
|
|
|
|
std::array<QTextCharFormat, level::n_levels> colors_;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
std::array<QTextCharFormat, level::n_levels> colors_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#include "spdlog/details/null_mutex.h"
|
|
|
|
|
#include <mutex>
|
|
|
|
@ -194,13 +193,15 @@ using qt_color_sink_st = qt_color_sink<spdlog::details::null_mutex>;
|
|
|
|
|
|
|
|
|
|
// create logger using QTextEdit object
|
|
|
|
|
template<typename Factory = spdlog::synchronous_factory>
|
|
|
|
|
inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
|
|
|
|
|
inline std::shared_ptr<logger> qt_logger_mt(
|
|
|
|
|
const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
|
|
|
|
|
{
|
|
|
|
|
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Factory = spdlog::synchronous_factory>
|
|
|
|
|
inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
|
|
|
|
|
inline std::shared_ptr<logger> qt_logger_st(
|
|
|
|
|
const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)")
|
|
|
|
|
{
|
|
|
|
|
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
|
|
|
|
}
|
|
|
|
|