Simplified Qt sinks

Removed private class that derived from QObject
This commit is contained in:
Muhammed Galib Uludag 2021-07-27 23:05:24 +03:00 committed by GitHub
parent fb47935a7b
commit c07b3aeef9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,200 +1,129 @@
// Copyright(c) 2015-present, Gabi Melman, mguludag and spdlog contributors. // Copyright(c) 2015-present, Gabi Melman, mguludag and spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once #pragma once
// //
// Custom sink for QPlainTextEdit or QTextEdit and its children(QTextBrowser... // Custom sink for QPlainTextEdit or QTextEdit and its childs(QTextBrowser... etc)
// etc) Building and using requires Qt library. // Building and using requires Qt library.
// //
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/details/log_msg.h" #include "spdlog/details/log_msg.h"
#include "spdlog/details/synchronous_factory.h" #include "spdlog/details/synchronous_factory.h"
#include "spdlog/sinks/base_sink.h" #include "spdlog/sinks/base_sink.h"
#include <QObject> #include <QTextEdit>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QTextEdit>
//
namespace _spdlog_p { // qtextedit_sink class
namespace _sinks_p { //
// namespace spdlog {
// Private class for QTextEdit and its derivatives namespace sinks {
// template <typename Mutex>
class qtextedit_sink_p : public QObject class qtextedit_sink : public base_sink<Mutex> {
{ public:
Q_OBJECT qtextedit_sink(QTextEdit *textedit = nullptr) {
public: if (textedit != nullptr) {
qtextedit_sink_p(QTextEdit *textedit = nullptr) qtextedit = textedit;
{ } else {
if (textedit != nullptr) throw spdlog_ex("Error opening QTextEdit");
{ }
textedit_ = textedit; }
connect(this, &qtextedit_sink_p::append_text, textedit_, &QTextEdit::append);
} ~qtextedit_sink() { flush_(); }
}
protected:
~qtextedit_sink_p() {} void sink_it_(const details::log_msg &msg) override {
if(qtextedit == nullptr)
void append(const spdlog::string_view_t &str) return;
{ memory_buf_t formatted;
emit append_text(QString::fromUtf8(str.data(), static_cast<int>(str.size() - 2))); base_sink<Mutex>::formatter_->format(msg, formatted);
} string_view_t str = string_view_t(formatted.data(), formatted.size());
QMetaObject::invokeMethod(qtextedit,"append", Qt::AutoConnection, Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size() - 2))));
signals: }
void append_text(const QString &);
void flush_() override {}
private:
QTextEdit *textedit_ = nullptr; private:
}; QTextEdit* qtextedit = nullptr;
};
//
// Private class for QPlainTextEdit //
// // qplaintextedit_sink class
class qplaintextedit_sink_p : public QObject //
{ template <typename Mutex>
Q_OBJECT class qplaintextedit_sink : public base_sink<Mutex> {
public: public:
qplaintextedit_sink_p(QPlainTextEdit *textedit = nullptr) qplaintextedit_sink(QPlainTextEdit *textedit = nullptr) {
{ if (textedit != nullptr) {
if (textedit != nullptr) qplaintextedit = textedit;
{ } else {
textedit_ = textedit; throw spdlog_ex("Error opening QPlainTextEdit");
connect(this, &qplaintextedit_sink_p::append_text, textedit_, &QPlainTextEdit::appendPlainText); }
} }
}
~qplaintextedit_sink() { flush_(); }
~qplaintextedit_sink_p() {}
protected:
void append(const spdlog::string_view_t &str) void sink_it_(const details::log_msg &msg) override {
{ if(qplaintextedit == nullptr)
emit append_text(QString::fromUtf8(str.data(), static_cast<int>(str.size() - 2))); return;
} memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
signals: string_view_t str = string_view_t(formatted.data(), formatted.size());
void append_text(const QString &); QMetaObject::invokeMethod(qplaintextedit, "appendPlainText", Qt::AutoConnection, Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size() - 2))));
}
private:
QPlainTextEdit *textedit_ = nullptr; void flush_() override {}
};
} // namespace _sinks_p private:
} // namespace _spdlog_p QPlainTextEdit* qplaintextedit = nullptr;
};
//
// qtextedit_sink class #include "spdlog/details/null_mutex.h"
// #include <mutex>
namespace spdlog { using qtextedit_sink_mt = qtextedit_sink<std::mutex>;
namespace sinks { using qtextedit_sink_st = qtextedit_sink<spdlog::details::null_mutex>;
template<typename Mutex>
class qtextedit_sink : public base_sink<Mutex> using qplaintextedit_sink_mt = qplaintextedit_sink<std::mutex>;
{ using qplaintextedit_sink_st = qplaintextedit_sink<spdlog::details::null_mutex>;
public:
qtextedit_sink(QTextEdit *textedit = nullptr) } // namespace sinks
{
if (textedit != nullptr) //
{ // Factory functions
textedit_p = std::make_shared<_spdlog_p::_sinks_p::qtextedit_sink_p>(textedit); //
} template <typename Factory = spdlog::synchronous_factory>
else inline std::shared_ptr<logger>
{ qtextedit_logger_mt(const std::string &logger_name,
throw spdlog_ex("Error opening QTextEdit"); QTextEdit *qtextedit = nullptr) {
} return Factory::template create<sinks::qtextedit_sink_mt>(logger_name,
} qtextedit);
}
~qtextedit_sink()
{ template <typename Factory = spdlog::synchronous_factory>
flush_(); inline std::shared_ptr<logger>
} qtextedit_logger_st(const std::string &logger_name,
QTextEdit *qtextedit = nullptr) {
protected: return Factory::template create<sinks::qtextedit_sink_st>(logger_name,
void sink_it_(const details::log_msg &msg) override qtextedit);
{ }
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted); template <typename Factory = spdlog::synchronous_factory>
string_view_t str_v = string_view_t(formatted.data(), formatted.size()); inline std::shared_ptr<logger>
textedit_p->append(str_v); qplaintextedit_logger_mt(const std::string &logger_name,
} QPlainTextEdit *qplaintextedit = nullptr) {
return Factory::template create<sinks::qplaintextedit_sink_mt>(logger_name,
void flush_() override {} qplaintextedit);
}
private:
std::shared_ptr<_spdlog_p::_sinks_p::qtextedit_sink_p> textedit_p = nullptr; template <typename Factory = spdlog::synchronous_factory>
}; inline std::shared_ptr<logger>
qplaintextedit_logger_st(const std::string &logger_name,
// QPlainTextEdit *qplaintextedit = nullptr) {
// qplaintextedit_sink class return Factory::template create<sinks::qplaintextedit_sink_st>(logger_name,
// qplaintextedit);
template<typename Mutex> }
class qplaintextedit_sink : public base_sink<Mutex> } // namespace spdlog
{
public:
qplaintextedit_sink(QPlainTextEdit *textedit = nullptr)
{
if (textedit != nullptr)
{
textedit_p = std::make_shared<_spdlog_p::_sinks_p::qplaintextedit_sink_p>(textedit);
}
else
{
throw spdlog_ex("Error opening QPlainTextEdit");
}
}
~qplaintextedit_sink()
{
flush_();
}
protected:
void sink_it_(const details::log_msg &msg) override
{
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
string_view_t str_v = string_view_t(formatted.data(), formatted.size());
textedit_p->append(str_v);
}
void flush_() override {}
private:
std::shared_ptr<_spdlog_p::_sinks_p::qplaintextedit_sink_p> textedit_p = nullptr;
};
#include "spdlog/details/null_mutex.h"
#include <mutex>
using qtextedit_sink_mt = qtextedit_sink<std::mutex>;
using qtextedit_sink_st = qtextedit_sink<spdlog::details::null_mutex>;
using qplaintextedit_sink_mt = qplaintextedit_sink<std::mutex>;
using qplaintextedit_sink_st = qplaintextedit_sink<spdlog::details::null_mutex>;
} // namespace sinks
//
// Factory functions
//
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qtextedit_logger_mt(const std::string &logger_name, QTextEdit *qtextedit = nullptr)
{
return Factory::template create<sinks::qtextedit_sink_mt>(logger_name, qtextedit);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qtextedit_logger_st(const std::string &logger_name, QTextEdit *qtextedit = nullptr)
{
return Factory::template create<sinks::qtextedit_sink_st>(logger_name, qtextedit);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qplaintextedit_logger_mt(const std::string &logger_name, QPlainTextEdit *qplaintextedit = nullptr)
{
return Factory::template create<sinks::qplaintextedit_sink_mt>(logger_name, qplaintextedit);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> qplaintextedit_logger_st(const std::string &logger_name, QPlainTextEdit *qplaintextedit = nullptr)
{
return Factory::template create<sinks::qplaintextedit_sink_st>(logger_name, qplaintextedit);
}
} // namespace spdlog