Main MRPT website > C++ reference
MRPT logo

CConsoleRedirector.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
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                 /** By creating an object of this class, all the output to std::cout (and std::cerr) will be redirected to a text file, and optionally also shown on the console.
00040                   *  Based on code from http://www.devmaster.net/forums/showthread.php?t=7037
00041                   */
00042                 class CConsoleRedirector : public std::streambuf
00043                 {
00044                 protected:
00045                         std::ofstream   m_of;           //!< The text output file stream.
00046                         std::streambuf  *sbOld;         //!< The "old" std::cout
00047                         std::streambuf  *sbOld_cerr;            //!< The "old" std::cout
00048                         bool                    m_also_to_console;
00049                         mrpt::synch::CCriticalSection   m_cs;
00050 
00051                 public:
00052                         /** Constructor
00053                           * \param out_file The file to create / append
00054                           * \param also_to_console Whether to redirect data to file *and* also dump data to the console as usual.
00055                           * \param append_file If set to false the file will be truncated on open
00056                           * \param bufferSize It's recommended to buffer the data instead of writing characters one by one.
00057                           * \param also_cerr Whether to redirect the output to std::cerr in addition to std::cout.
00058                           * \exception std::exception If the file cannot be opened.
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                                 // Open the file:
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                                 // Redirect:
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                                 // Restore normal output:
00096                                 std::cout.rdbuf(sbOld);
00097                                 if (sbOld_cerr!=NULL) std::cerr.rdbuf( sbOld_cerr );
00098                                 if (pbase()) 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         } // end namespace
00150 } // end namespace
00151 
00152 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011