ARC Graphical Clients: /home/jonas/Development/arc-gui-clients/src/arcstorage-ui/qdebugstream.h Source File

- Main Page
- Classes
- Files

- File List

# /home/jonas/Development/arc-gui-clients/src/arcstorage-ui/qdebugstream.h

    00001 #ifndef QDEBUGSTREAM_H
    00002 #define QDEBUGSTREAM_H
    00003
    00004 //################
    00005 //# qdebugstream.h  #
    00006 //################
    00007
    00008 #include <iostream>
    00009 #include <streambuf>
    00010 #include <string>
    00011 #include <QMutex>
    00012
    00013 #include "qtextedit.h"
    00014
    00015 class QDebugStream : public std::basic_streambuf<char>
    00016 {
    00017 public:
    00018     QDebugStream(std::ostream &stream, QTextEdit* text_edit) : m_stream(stream)
    00019     {
    00020         log_window = text_edit;
    00021         m_old_buf = stream.rdbuf();
    00022         stream.rdbuf(this);
    00023     }
    00024     ~QDebugStream()
    00025     {
    00026         // output anything that is left
    00027         if (!m_string.empty())
    00028             log_window->append(m_string.c_str());
    00029
    00030         m_stream.rdbuf(m_old_buf);
    00031     }
    00032
    00033 protected:
    00034     virtual int_type overflow(int_type v)
    00035     {
    00036         m_accessMutex.lock();
    00037
    00038         if (v == '\n')
    00039         {
    00040
    00041             log_window->append(m_string.c_str());
    00042
    00043             // Scroll to bottom of window
    00044
    00045             log_window->moveCursor(QTextCursor::End);
    00046             log_window->moveCursor(QTextCursor::StartOfLine);
    00047
    00048             m_string.erase(m_string.begin(), m_string.end());
    00049         }
    00050         else
    00051             m_string += v;
    00052
    00053         m_accessMutex.unlock();
    00054         return v;
    00055     }
    00056
    00057     virtual std::streamsize xsputn(const char *p, std::streamsize n)
    00058     {
    00059         m_accessMutex.lock();
    00060         m_string.append(p, p + n);
    00061
    00062         std::string::size_type pos = 0;
    00063         while (pos != std::string::npos)
    00064         {
    00065             pos = m_string.find('\n');
    00066             if (pos != std::string::npos)
    00067             {
    00068                 std::string tmp(m_string.begin(), m_string.begin() + pos);
    00069                 log_window->append(tmp.c_str());
    00070
    00071                 // Scroll to bottom of window
    00072
    00073                 log_window->moveCursor(QTextCursor::End);
    00074                 log_window->moveCursor(QTextCursor::StartOfLine);
    00075
    00076                 m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
    00077             }
    00078         }
    00079
    00080         m_accessMutex.unlock();
    00081         return n;
    00082     }
    00083
    00084 private:
    00085     std::ostream &m_stream;
    00086     std::streambuf *m_old_buf;
    00087     std::string m_string;
    00088     QTextEdit* log_window;
    00089     QMutex m_accessMutex;
    00090 };
    00091
    00092 #endif // QDEBUGSTREAM_H

All Classes Functions Variables