Agora  1.2.0
Agora project
qt_sinks.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman, mguludag and spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
6 //
7 // Custom sink for QPlainTextEdit or QTextEdit and its childs(QTextBrowser...
8 // etc) Building and using requires Qt library.
9 //
10 
11 #include "spdlog/common.h"
12 #include "spdlog/details/log_msg.h"
14 #include "spdlog/sinks/base_sink.h"
15 
16 #include <QTextEdit>
17 #include <QPlainTextEdit>
18 
19 //
20 // qt_sink class
21 //
22 namespace spdlog {
23 namespace sinks {
24 template<typename Mutex>
25 class qt_sink : public base_sink<Mutex>
26 {
27 public:
28  qt_sink(QObject *qt_object, const std::string &meta_method)
29  {
30  qt_object_ = qt_object;
31  meta_method_ = meta_method;
32  }
33 
35  {
36  flush_();
37  }
38 
39 protected:
40  void sink_it_(const details::log_msg &msg) override
41  {
42  memory_buf_t formatted;
43  base_sink<Mutex>::formatter_->format(msg, formatted);
44  string_view_t str = string_view_t(formatted.data(), formatted.size());
45  QMetaObject::invokeMethod(qt_object_, meta_method_.c_str(), Qt::AutoConnection,
46  Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size())).trimmed()));
47  }
48 
49  void flush_() override {}
50 
51 private:
52  QObject *qt_object_ = nullptr;
53  std::string meta_method_;
54 };
55 
57 #include <mutex>
60 } // namespace sinks
61 
62 //
63 // Factory functions
64 //
65 template<typename Factory = spdlog::synchronous_factory>
66 inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append")
67 {
68  return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
69 }
70 
71 template<typename Factory = spdlog::synchronous_factory>
72 inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method = "append")
73 {
74  return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
75 }
76 
77 template<typename Factory = spdlog::synchronous_factory>
78 inline std::shared_ptr<logger> qt_logger_mt(
79  const std::string &logger_name, QPlainTextEdit *qt_object, const std::string &meta_method = "appendPlainText")
80 {
81  return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
82 }
83 
84 template<typename Factory = spdlog::synchronous_factory>
85 inline std::shared_ptr<logger> qt_logger_st(
86  const std::string &logger_name, QPlainTextEdit *qt_object, const std::string &meta_method = "appendPlainText")
87 {
88  return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
89 }
90 
91 template<typename Factory = spdlog::synchronous_factory>
92 inline std::shared_ptr<logger> qt_logger_mt(const std::string &logger_name, QObject *qt_object, const std::string &meta_method)
93 {
94  return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
95 }
96 
97 template<typename Factory = spdlog::synchronous_factory>
98 inline std::shared_ptr<logger> qt_logger_st(const std::string &logger_name, QObject *qt_object, const std::string &meta_method)
99 {
100  return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
101 }
102 } // namespace spdlog
spdlog::sinks::qt_sink::meta_method_
std::string meta_method_
Definition: qt_sinks.h:53
spdlog::sinks::qt_sink::sink_it_
void sink_it_(const details::log_msg &msg) override
Definition: qt_sinks.h:40
base_sink.h
spdlog::string_view_t
fmt::basic_string_view< char > string_view_t
Definition: common.h:154
fmt::v8::detail::buffer::size
constexpr auto size() const -> size_t
Definition: core.h:820
spdlog::sinks::qt_sink::~qt_sink
~qt_sink()
Definition: qt_sinks.h:34
synchronous_factory.h
fmt::v8::basic_string_view
Definition: core.h:448
fmt::v8::basic_string_view::size
constexpr auto size() const -> size_t
Definition: core.h:495
null_mutex.h
spdlog::sinks::qt_sink
Definition: qt_sinks.h:25
fmt::v8::basic_memory_buffer
Definition: format.h:677
spdlog::sinks::qt_sink::qt_sink
qt_sink(QObject *qt_object, const std::string &meta_method)
Definition: qt_sinks.h:28
spdlog
Definition: async.h:25
spdlog::sinks::base_sink
Definition: base_sink.h:19
fmt::v8::detail::buffer::data
auto data() -> T *
Definition: core.h:826
fmt::v8::basic_string_view::data
constexpr auto data() const -> const Char *
Definition: core.h:492
spdlog::qt_logger_st
std::shared_ptr< logger > qt_logger_st(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method="append")
Definition: qt_sinks.h:72
common.h
spdlog::details::log_msg
Definition: log_msg.h:11
spdlog::sinks::qt_sink::flush_
void flush_() override
Definition: qt_sinks.h:49
log_msg.h
spdlog::qt_logger_mt
std::shared_ptr< logger > qt_logger_mt(const std::string &logger_name, QTextEdit *qt_object, const std::string &meta_method="append")
Definition: qt_sinks.h:66
spdlog::sinks::qt_sink::qt_object_
QObject * qt_object_
Definition: qt_sinks.h:52