/* * QDebugStream.hpp * * Created on: Jun 19, 2014 * Author: heber */ #ifndef QDEBUGSTREAM_HPP_ #define QDEBUGSTREAM_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include /** * Taken from http://www.qtforum.de/forum/viewtopic.php?t=7970 */ class QDebugStream : public QObject, std::basic_streambuf{ Q_OBJECT signals: void sendString(QString text); public: QDebugStream(std::ostream &stream, QPlainTextEdit* text_edit) : m_stream(stream){ log_window = text_edit; m_old_buf = stream.rdbuf(); stream.rdbuf(this); connect(this, SIGNAL(sendString(QString)), text_edit, SLOT(appendPlainText (QString))); } ~QDebugStream(){ // output anything that is left if (!m_string.empty()) emit sendString(m_string.c_str()); //log_window->appendPlainText(m_string.c_str()); m_stream.rdbuf(m_old_buf); } protected: virtual int_type overflow(int_type v){ mutex.lock(); if (v == '\n'){ emit sendString(m_string.c_str()); //log_window->appendPlainText(m_string.c_str()); m_string.erase(m_string.begin(), m_string.end()); } else m_string += v; mutex.unlock(); return v; } virtual std::streamsize xsputn(const char *p, std::streamsize n){ mutex.lock(); m_string.append(p, p + n); size_t pos = 0; while (pos != std::string::npos){ pos = m_string.find('\n'); if (pos != std::string::npos){ std::string tmp(m_string.begin(), m_string.begin() + pos); emit sendString(tmp.c_str()); //log_window->appendPlainText(tmp.c_str()); m_string.erase(m_string.begin(), m_string.begin() + pos + 1); } } mutex.unlock(); return n; } private: std::ostream &m_stream; std::streambuf *m_old_buf; std::string m_string; QPlainTextEdit* log_window; QMutex mutex; }; #endif // QDEBUGSTREAM_HPP