00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CConsoleRedirector_H
00029 #define CConsoleRedirector_H
00030
00031 #include <streambuf>
00032 #include <iostream>
00033
00034
00035 namespace mrpt
00036 {
00037 namespace utils
00038 {
00039
00040
00041
00042 class CConsoleRedirector : public std::streambuf
00043 {
00044 protected:
00045 std::ofstream m_of;
00046 std::streambuf *sbOld;
00047 std::streambuf *sbOld_cerr;
00048 bool m_also_to_console;
00049 mrpt::synch::CCriticalSection m_cs;
00050
00051 public:
00052
00053
00054
00055
00056
00057
00058
00059
00060 CConsoleRedirector(
00061 const std::string &out_file,
00062 bool also_to_console=true,
00063 bool also_cerr = true,
00064 bool append_file = false,
00065 int bufferSize = 1000 ) : m_of(), sbOld(NULL),sbOld_cerr(NULL),m_also_to_console(also_to_console), m_cs()
00066 {
00067
00068 std::ios_base::openmode openMode = std::ios_base::binary | std::ios_base::out;
00069 if ( append_file ) openMode |= std::ios_base::app;
00070 m_of.open(out_file.c_str(), openMode );
00071 if (!m_of.is_open()) THROW_EXCEPTION_CUSTOM_MSG1("Error opening file: %s",out_file.c_str())
00072
00073 if (bufferSize)
00074 {
00075 char *ptr = new char[bufferSize];
00076 setp(ptr, ptr + bufferSize);
00077 }
00078 else
00079 setp(0, 0);
00080
00081
00082 sbOld = std::cout.rdbuf();
00083 std::cout.rdbuf( this );
00084
00085 if (also_cerr)
00086 {
00087 sbOld_cerr = std::cerr.rdbuf();
00088 std::cerr.rdbuf( this );
00089 }
00090 }
00091
00092 virtual ~CConsoleRedirector()
00093 {
00094 sync();
00095
00096 std::cout.rdbuf(sbOld);
00097 if (sbOld_cerr!=NULL) std::cerr.rdbuf( sbOld_cerr );
00098 delete[] pbase();
00099 }
00100
00101 void flush()
00102 {
00103 sync();
00104 }
00105
00106 virtual void writeString(const std::string &str)
00107 {
00108 if (m_also_to_console) sbOld->sputn(str.c_str(),str.size());
00109 m_of << str;
00110 }
00111
00112 private:
00113 int overflow(int c)
00114 {
00115 sync();
00116
00117 m_cs.enter();
00118 if (c != EOF)
00119 {
00120 if (pbase() == epptr())
00121 {
00122 std::string temp;
00123 temp += char(c);
00124 writeString(temp);
00125 }
00126 else
00127 sputc(c);
00128 }
00129
00130 m_cs.leave();
00131 return 0;
00132 }
00133
00134 int sync()
00135 {
00136 m_cs.enter();
00137 if (pbase() != pptr())
00138 {
00139 int len = int(pptr() - pbase());
00140 std::string temp(pbase(), len);
00141 writeString(temp);
00142 setp(pbase(), epptr());
00143 }
00144 m_cs.leave();
00145 return 0;
00146 }
00147 };
00148
00149 }
00150 }
00151
00152 #endif