diff --git a/include/spdlog/sinks/qt_sinks.h b/include/spdlog/sinks/qt_sinks.h index d61c6bec..e4d03365 100644 --- a/include/spdlog/sinks/qt_sinks.h +++ b/include/spdlog/sinks/qt_sinks.h @@ -17,6 +17,7 @@ #include "spdlog/details/synchronous_factory.h" #include "spdlog/sinks/base_sink.h" +#include #include #include @@ -29,10 +30,17 @@ template class qt_sink : public base_sink { 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; - 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() @@ -46,15 +54,15 @@ protected: memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); string_view_t str = string_view_t(formatted.data(), formatted.size()); - QMetaObject::invokeMethod(qt_object_, meta_method_.c_str(), Qt::AutoConnection, - Q_ARG(QString, QString::fromUtf8(str.data(), static_cast(str.size())).trimmed())); + auto payload = QString::fromUtf8(str.data(), static_cast(str.size())).trimmed(); + meta_method_.invoke(qt_object_, Qt::AutoConnection, Q_ARG(QString, payload)); } void flush_() override {} private: QObject *qt_object_ = nullptr; - std::string meta_method_; + QMetaMethod meta_method_; }; #include "spdlog/details/null_mutex.h" @@ -67,27 +75,27 @@ using qt_sink_st = qt_sink; // Factory functions // template -inline std::shared_ptr qt_logger_mt(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append") +inline std::shared_ptr qt_logger_mt(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)") { return Factory::template create(logger_name, qt_object, meta_method); } template -inline std::shared_ptr qt_logger_st(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append") +inline std::shared_ptr qt_logger_st(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append(QString)") { return Factory::template create(logger_name, qt_object, meta_method); } template inline std::shared_ptr 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(logger_name, qt_object, meta_method); } template inline std::shared_ptr 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(logger_name, qt_object, meta_method); }