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://www.mrpt.org/                                |
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                  * \ingroup mrpt_base_grp
00042                   */
00043                 class CConsoleRedirector : public std::streambuf
00044                 {
00045                 protected:
00046                         std::ofstream   m_of;           //!< The text output file stream.
00047                         std::streambuf  *sbOld;         //!< The "old" std::cout
00048                         std::streambuf  *sbOld_cerr;            //!< The "old" std::cout
00049                         bool                    m_also_to_console;
00050                         mrpt::synch::CCriticalSection   m_cs;
00051 
00052                 public:
00053                         /** Constructor
00054                           * \param out_file The file to create / append
00055                           * \param also_to_console Whether to redirect data to file *and* also dump data to the console as usual.
00056                           * \param append_file If set to false the file will be truncated on open
00057                           * \param bufferSize It's recommended to buffer the data instead of writing characters one by one.
00058                           * \param also_cerr Whether to redirect the output to std::cerr in addition to std::cout.
00059                           * \exception std::exception If the file cannot be opened.
00060                           */
00061                         CConsoleRedirector(
00062                                 const std::string &out_file,
00063                                 bool also_to_console=true,
00064                                 bool also_cerr = true,
00065                                 bool append_file = false,
00066                                 int bufferSize = 1000 ) : m_of(), sbOld(NULL),sbOld_cerr(NULL),m_also_to_console(also_to_console), m_cs()
00067                         {
00068                                 // Open the file:
00069                                 std::ios_base::openmode  openMode = std::ios_base::binary | std::ios_base::out;
00070                                 if ( append_file ) openMode |= std::ios_base::app;
00071                                 m_of.open(out_file.c_str(),  openMode );
00072                                 if (!m_of.is_open()) THROW_EXCEPTION_CUSTOM_MSG1("Error opening file: %s",out_file.c_str())
00073 
00074                                 if (bufferSize)
00075                                 {
00076                                         char *ptr = new char[bufferSize];
00077                                         setp(ptr, ptr + bufferSize);
00078                                 }
00079                                 else
00080                                         setp(0, 0);
00081 
00082                                 // Redirect:
00083                                 sbOld = std::cout.rdbuf();
00084                                 std::cout.rdbuf( this );
00085 
00086                                 if (also_cerr)
00087                                 {
00088                                         sbOld_cerr = std::cerr.rdbuf();
00089                                         std::cerr.rdbuf( this );
00090                                 }
00091                         }
00092 
00093                         virtual ~CConsoleRedirector()
00094                         {
00095                                 sync();
00096                                 // Restore normal output:
00097                                 std::cout.rdbuf(sbOld);
00098                                 if (sbOld_cerr!=NULL) std::cerr.rdbuf( sbOld_cerr );
00099                                 if (pbase()) delete[] pbase();
00100                         }
00101 
00102                         void flush()
00103                         {
00104                                 sync();
00105                         }
00106 
00107                         virtual void writeString(const std::string &str)
00108                         {
00109                                 if (m_also_to_console) sbOld->sputn(str.c_str(),str.size());
00110                                 m_of << str;
00111                         }
00112 
00113                 private:
00114                         int     overflow(int c)
00115                         {
00116                                 sync();
00117 
00118                                 m_cs.enter();
00119                                 if (c != EOF)
00120                                 {
00121                                         if (pbase() == epptr())
00122                                         {
00123                                                 std::string temp;
00124                                                 temp += char(c);
00125                                                 writeString(temp);
00126                                         }
00127                                         else
00128                                                 sputc(c);
00129                                 }
00130 
00131                                 m_cs.leave();
00132                                 return 0;
00133                         }
00134 
00135                         int     sync()
00136                         {
00137                                 m_cs.enter();
00138                                 if (pbase() != pptr())
00139                                 {
00140                                         int len = int(pptr() - pbase());
00141                                         std::string temp(pbase(), len);
00142                                         writeString(temp);
00143                                         setp(pbase(), epptr());
00144                                 }
00145                                 m_cs.leave();
00146                                 return 0;
00147                         }
00148                 };
00149 
00150         } // end namespace
00151 } // end namespace
00152 
00153 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011