9 #ifndef CMyRedirector_H 10 #define CMyRedirector_H 12 #include <wx/string.h> 13 #include <wx/textctrl.h> 15 #include <wx/thread.h> 50 bool yieldApplication =
false,
51 int bufferSize = 3000,
52 bool also_cerr =
false,
53 bool threadSafe =
false,
54 bool also_to_cout_cerr =
false ) : m_txt(obj), m_yieldApplication(yieldApplication), m_also_cerr(also_cerr),m_threadSafe(threadSafe), m_also_to_cout_cerr(also_to_cout_cerr)
58 char *ptr =
new char[bufferSize];
59 setp(ptr, ptr + bufferSize);
65 sbOld = std::cout.rdbuf();
66 std::cout.rdbuf(
this );
70 sbOldErr = std::cerr.rdbuf();
71 std::cerr.rdbuf(
this );
79 std::cout.rdbuf(sbOld);
82 std::cerr.rdbuf(sbOldErr);
98 s = wxString(str.c_str(), wxConvUTF8);
103 #if wxCHECK_VERSION(3,0,0) 104 m_txt->GetEventHandler()->CallAfter(&wxTextCtrl::WriteText, s);
113 if (m_also_to_cout_cerr) ::printf(
"%s",str.c_str());
114 if (m_yieldApplication && wxThread::IsMain()) wxTheApp->Yield(
true);
122 wxCriticalSectionLocker lock(m_cs);
124 if (!m_strbuf.empty())
126 if (m_also_to_cout_cerr) ::printf(
"%s",m_strbuf.c_str());
128 *m_txt << wxString( m_strbuf.c_str(), wxConvUTF8 );
130 *m_txt <<
_U( m_strbuf.c_str() );
143 wxCriticalSectionLocker lock(m_cs);
144 if (pbase() == epptr())
159 wxCriticalSectionLocker lock(m_cs);
161 if (pbase() != pptr())
163 int len = int(pptr() - pbase());
164 std::string temp(pbase(), len);
166 setp(pbase(), epptr());
const bool m_also_to_cout_cerr
This auxiliary class redirects the output sent to a streambuf to a wxTextCtrl object.
const bool m_yieldApplication
std::streambuf * sbOldErr
virtual void writeString(const std::string &str)
CMyRedirector(wxTextCtrl *obj, bool yieldApplication=false, int bufferSize=3000, bool also_cerr=false, bool threadSafe=false, bool also_to_cout_cerr=false)
void dumpNow()
Writes all the stored strings to the text control (only for threadSafe mode).