mirror of
https://github.com/gabime/spdlog.git
synced 2025-04-01 02:42:41 +08:00
Formatted qt_sinks.h code
This commit is contained in:
parent
72a7ec3eb9
commit
bed324e414
@ -30,13 +30,14 @@ template<typename Mutex>
|
|||||||
class qt_sink : public base_sink<Mutex>
|
class qt_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
qt_sink(QObject *qt_object, std::string meta_method): qt_object_(qt_object), meta_method_(std::move(meta_method))
|
qt_sink(QObject *qt_object, std::string meta_method)
|
||||||
|
: qt_object_(qt_object)
|
||||||
|
, meta_method_(std::move(meta_method))
|
||||||
{
|
{
|
||||||
if (!qt_object_)
|
if (!qt_object_)
|
||||||
{
|
{
|
||||||
throw_spdlog_ex("qt_sink: qt_object is null");
|
throw_spdlog_ex("qt_sink: qt_object is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~qt_sink()
|
~qt_sink()
|
||||||
@ -66,12 +67,13 @@ private:
|
|||||||
// 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.
|
// Note: Only ascii (latin1) is supported by this sink.
|
||||||
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)
|
qt_color_sink(QTextEdit *qt_text_edit, int max_lines)
|
||||||
: qt_text_edit_(qt_text_edit), max_lines_(max_lines)
|
: qt_text_edit_(qt_text_edit)
|
||||||
|
, max_lines_(max_lines)
|
||||||
{
|
{
|
||||||
if (!qt_text_edit_)
|
if (!qt_text_edit_)
|
||||||
{
|
{
|
||||||
@ -131,20 +133,19 @@ private:
|
|||||||
return default_color_;
|
return default_color_;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
struct invoke_params
|
struct invoke_params
|
||||||
{
|
{
|
||||||
invoke_params(int max_lines, QTextEdit *q_text_edit, QString payload, QTextCharFormat default_color,
|
invoke_params(int max_lines, QTextEdit *q_text_edit, QString payload, QTextCharFormat default_color, QTextCharFormat level_color,
|
||||||
QTextCharFormat level_color, int color_range_start, int color_range_end)
|
int color_range_start, int color_range_end)
|
||||||
: max_lines(max_lines),
|
: max_lines(max_lines)
|
||||||
q_text_edit(q_text_edit),
|
, q_text_edit(q_text_edit)
|
||||||
payload(std::move(payload)),
|
, payload(std::move(payload))
|
||||||
default_color(default_color),
|
, default_color(default_color)
|
||||||
level_color(level_color),
|
, level_color(level_color)
|
||||||
color_range_start(color_range_start),
|
, color_range_start(color_range_start)
|
||||||
color_range_end(color_range_end)
|
, color_range_end(color_range_end)
|
||||||
{
|
{}
|
||||||
}
|
|
||||||
int max_lines;
|
int max_lines;
|
||||||
QTextEdit *q_text_edit;
|
QTextEdit *q_text_edit;
|
||||||
QString payload;
|
QString payload;
|
||||||
@ -163,8 +164,7 @@ private:
|
|||||||
// 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()));
|
auto payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
|
||||||
|
|
||||||
invoke_params params {
|
invoke_params params{max_lines_, // max lines
|
||||||
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
|
||||||
@ -173,10 +173,7 @@ private:
|
|||||||
static_cast<int>(msg.color_range_end)}; // color range end
|
static_cast<int>(msg.color_range_end)}; // color range end
|
||||||
|
|
||||||
QMetaObject::invokeMethod(
|
QMetaObject::invokeMethod(
|
||||||
qt_text_edit_,
|
qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection);
|
||||||
[params]() {invoke_method_(params);},
|
|
||||||
Qt::AutoConnection);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush_() override {}
|
void flush_() override {}
|
||||||
@ -191,7 +188,7 @@ private:
|
|||||||
QTextCursor cursor(document);
|
QTextCursor cursor(document);
|
||||||
|
|
||||||
// remove first blocks if number of blocks exceeds max_lines
|
// remove first blocks if number of blocks exceeds max_lines
|
||||||
while(document->blockCount() > params.max_lines)
|
while (document->blockCount() > params.max_lines)
|
||||||
{
|
{
|
||||||
cursor.select(QTextCursor::BlockUnderCursor);
|
cursor.select(QTextCursor::BlockUnderCursor);
|
||||||
cursor.removeSelectedText();
|
cursor.removeSelectedText();
|
||||||
@ -202,7 +199,7 @@ private:
|
|||||||
cursor.setCharFormat(params.default_color);
|
cursor.setCharFormat(params.default_color);
|
||||||
|
|
||||||
// if color range not specified or not not valid, just append the text with default color
|
// if color range not specified or not not valid, just append the text with default color
|
||||||
if(params.color_range_end <= params.color_range_start)
|
if (params.color_range_end <= params.color_range_start)
|
||||||
{
|
{
|
||||||
cursor.insertText(params.payload);
|
cursor.insertText(params.payload);
|
||||||
return;
|
return;
|
||||||
@ -224,7 +221,7 @@ private:
|
|||||||
int max_lines_;
|
int max_lines_;
|
||||||
QTextCharFormat default_color_;
|
QTextCharFormat default_color_;
|
||||||
std::array<QTextCharFormat, level::n_levels> colors_;
|
std::array<QTextCharFormat, level::n_levels> colors_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include "spdlog/details/null_mutex.h"
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@ -283,13 +280,13 @@ inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QObj
|
|||||||
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)
|
||||||
{
|
{
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
Loading…
Reference in New Issue
Block a user