qt_sinks.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright(c) 2015-present, Gabi Melman, mguludag and spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. //
  5. // Custom sink for QPlainTextEdit or QTextEdit and its childs(QTextBrowser...
  6. // etc) Building and using requires Qt library.
  7. //
  8. #include "spdlog/common.h"
  9. #include "spdlog/details/log_msg.h"
  10. #include "spdlog/details/synchronous_factory.h"
  11. #include "spdlog/sinks/base_sink.h"
  12. #include <QTextEdit>
  13. #include <QPlainTextEdit>
  14. //
  15. // qt_sink class
  16. //
  17. namespace spdlog {
  18. namespace sinks {
  19. template<typename Mutex>
  20. class qt_sink : public base_sink<Mutex>
  21. {
  22. public:
  23. qt_sink(QObject *qt_object, const std::string &meta_method)
  24. {
  25. qt_object_ = qt_object;
  26. meta_method_ = meta_method;
  27. }
  28. ~qt_sink()
  29. {
  30. flush_();
  31. }
  32. protected:
  33. void sink_it_(const details::log_msg &msg) override
  34. {
  35. memory_buf_t formatted;
  36. base_sink<Mutex>::formatter_->format(msg, formatted);
  37. string_view_t str = string_view_t(formatted.data(), formatted.size());
  38. QMetaObject::invokeMethod(qt_object_, meta_method_.c_str(), Qt::AutoConnection,
  39. Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size())).trimmed()));
  40. }
  41. void flush_() override {}
  42. private:
  43. QObject *qt_object_ = nullptr;
  44. std::string meta_method_;
  45. };
  46. #include "spdlog/details/null_mutex.h"
  47. #include <mutex>
  48. using qt_sink_mt = qt_sink<std::mutex>;
  49. using qt_sink_st = qt_sink<spdlog::details::null_mutex>;
  50. } // namespace sinks
  51. //
  52. // Factory functions
  53. //
  54. template<typename Factory = spdlog::synchronous_factory>
  55. inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append")
  56. {
  57. return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
  58. }
  59. template<typename Factory = spdlog::synchronous_factory>
  60. inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append")
  61. {
  62. return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
  63. }
  64. template<typename Factory = spdlog::synchronous_factory>
  65. inline std::shared_ptr<logger> qt_logger_mt(
  66. const std::string &logger_name, QPlainTextEdit *qt_object, const std::string &meta_method = "appendPlainText")
  67. {
  68. return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
  69. }
  70. template<typename Factory = spdlog::synchronous_factory>
  71. inline std::shared_ptr<logger> qt_logger_st(
  72. const std::string &logger_name, QPlainTextEdit *qt_object, const std::string &meta_method = "appendPlainText")
  73. {
  74. return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
  75. }
  76. template<typename Factory = spdlog::synchronous_factory>
  77. inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name, QObject *qt_object, const std::string &meta_method)
  78. {
  79. return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
  80. }
  81. template<typename Factory = spdlog::synchronous_factory>
  82. inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QObject *qt_object, const std::string &meta_method)
  83. {
  84. return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
  85. }
  86. } // namespace spdlog