log.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* docks/log.cpp - global log
  2. * Copyright (C) 2018 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <iostream>
  18. #include <sstream>
  19. #include <QThread>
  20. #include <QTimer>
  21. #include <util/strings.h>
  22. #include "log.h"
  23. #include "ui_log.h"
  24. namespace rainynite::studio {
  25. class LogDock::RedirectStream : public std::streambuf {
  26. public:
  27. void set_ui_output(QTextEdit* ui_output_) {
  28. ui_output.reset(ui_output_);
  29. }
  30. void set_console_output(observer_ptr<std::streambuf> console_output_) {
  31. console_output = console_output_;
  32. }
  33. protected:
  34. int_type overflow(int_type ch) override {
  35. if (ch == std::char_traits<char>::eof())
  36. return ch;
  37. if (console_output) {
  38. console_output->sputc(ch);
  39. }
  40. char c = ch;
  41. write_to_ui(string(&c, 1));
  42. return ch;
  43. }
  44. std::streamsize xsputn(char const* line, std::streamsize n) override {
  45. if (console_output)
  46. console_output->sputn(line, n);
  47. write_to_ui(string(line, n));
  48. return n;
  49. }
  50. public:
  51. void flush() {
  52. auto s = buff.str();
  53. buff = std::ostringstream();
  54. if (s.size() > 0) {
  55. ui_output->append(util::str(std::move(s)));
  56. }
  57. }
  58. private:
  59. void write_to_ui(string&& s) {
  60. if (QThread::currentThread() == ui_output->thread()) {
  61. flush();
  62. ui_output->append(util::str(s));
  63. } else {
  64. buff << s;
  65. }
  66. }
  67. observer_ptr<QTextEdit> ui_output;
  68. observer_ptr<std::streambuf> console_output;
  69. std::ostringstream buff;
  70. };
  71. observer_ptr<LogDock::RedirectStream> global_buff;
  72. void handler(QtMsgType, QMessageLogContext const&, QString const& s) {
  73. if (!global_buff)
  74. return;
  75. string cs = util::str(s)+"\n";
  76. global_buff->sputn(cs.c_str(), cs.size());
  77. }
  78. LogDock::LogDock(QWidget* parent) :
  79. DockWidget(parent),
  80. ui(make_unique<Ui::LogDock>()),
  81. buff(make_unique<RedirectStream>()),
  82. timer(make_unique<QTimer>())
  83. {
  84. ui->setupUi(this);
  85. global_buff.reset(buff.get());
  86. old_cout.reset(std::cout.rdbuf(buff.get()));
  87. old_cerr.reset(std::cerr.rdbuf(buff.get()));
  88. qInstallMessageHandler(&handler);
  89. buff->set_ui_output(ui->text);
  90. buff->set_console_output(old_cout);
  91. connect(timer.get(), &QTimer::timeout, [this]() {
  92. buff->flush();
  93. });
  94. timer->start(1000); // force log update each second
  95. }
  96. LogDock::~LogDock() {
  97. std::cout.rdbuf(old_cout.get());
  98. std::cerr.rdbuf(old_cerr.get());
  99. qInstallMessageHandler(0);
  100. global_buff.reset();
  101. }
  102. } // namespace rainynite::studio