Store MetaMethod object in qt_sink for better performance
This commit is contained in:
parent
230cad163d
commit
5384512f25
@ -17,6 +17,7 @@
|
|||||||
#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 <QMetaMethod>
|
||||||
#include <QTextEdit>
|
#include <QTextEdit>
|
||||||
#include <QPlainTextEdit>
|
#include <QPlainTextEdit>
|
||||||
|
|
||||||
@ -29,10 +30,17 @@ template<typename Mutex>
|
|||||||
class qt_sink : public base_sink<Mutex>
|
class qt_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
qt_sink(QObject *qt_object, const std::string &meta_method)
|
qt_sink(QObject *qt_object, const std::string &meta_method_name)
|
||||||
{
|
{
|
||||||
|
// store the meta method object for later usage
|
||||||
qt_object_ = qt_object;
|
qt_object_ = qt_object;
|
||||||
meta_method_ = meta_method;
|
const QMetaObject *metaobject = qt_object_->metaObject();
|
||||||
|
qt_object_->dumpObjectInfo();
|
||||||
|
int methodIndex = metaobject->indexOfMethod(meta_method_name.c_str());
|
||||||
|
if (methodIndex == -1) {
|
||||||
|
throw_spdlog_ex("qt_sink: qt_object does not have meta_method " + meta_method_name);
|
||||||
|
}
|
||||||
|
meta_method_ = metaobject->method(methodIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
~qt_sink()
|
~qt_sink()
|
||||||
@ -46,15 +54,15 @@ protected:
|
|||||||
memory_buf_t formatted;
|
memory_buf_t formatted;
|
||||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||||
string_view_t str = string_view_t(formatted.data(), formatted.size());
|
string_view_t str = string_view_t(formatted.data(), formatted.size());
|
||||||
QMetaObject::invokeMethod(qt_object_, meta_method_.c_str(), Qt::AutoConnection,
|
auto payload = QString::fromUtf8(str.data(), static_cast<int>(str.size())).trimmed();
|
||||||
Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size())).trimmed()));
|
meta_method_.invoke(qt_object_, Qt::AutoConnection, Q_ARG(QString, payload));
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush_() override {}
|
void flush_() override {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QObject *qt_object_ = nullptr;
|
QObject *qt_object_ = nullptr;
|
||||||
std::string meta_method_;
|
QMetaMethod meta_method_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include "spdlog/details/null_mutex.h"
|
||||||
@ -67,27 +75,27 @@ using qt_sink_st = qt_sink<spdlog::details::null_mutex>;
|
|||||||
// Factory functions
|
// Factory functions
|
||||||
//
|
//
|
||||||
template<typename Factory = spdlog::synchronous_factory>
|
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")
|
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);
|
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Factory = spdlog::synchronous_factory>
|
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")
|
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);
|
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Factory = spdlog::synchronous_factory>
|
template<typename Factory = spdlog::synchronous_factory>
|
||||||
inline std::shared_ptr<logger> qt_logger_mt(
|
inline std::shared_ptr<logger> qt_logger_mt(
|
||||||
const std::string &logger_name, QPlainTextEdit *qt_object, const std::string &meta_method = "appendPlainText")
|
const std::string &logger_name, QPlainTextEdit *qt_object, const std::string &meta_method = "appendPlainText(QString)")
|
||||||
{
|
{
|
||||||
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Factory = spdlog::synchronous_factory>
|
template<typename Factory = spdlog::synchronous_factory>
|
||||||
inline std::shared_ptr<logger> qt_logger_st(
|
inline std::shared_ptr<logger> qt_logger_st(
|
||||||
const std::string &logger_name, QPlainTextEdit *qt_object, const std::string &meta_method = "appendPlainText")
|
const std::string &logger_name, QPlainTextEdit *qt_object, const std::string &meta_method = "appendPlainText(QString)")
|
||||||
{
|
{
|
||||||
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user