base/StdoutFileOStream.hpp
Go to the documentation of this file.
00001 /*
00002 
00003   This file is part of OpenFLUID software
00004   Copyright(c) 2007, INRA - Montpellier SupAgro
00005 
00006 
00007  == GNU General Public License Usage ==
00008 
00009   OpenFLUID is free software: you can redistribute it and/or modify
00010   it under the terms of the GNU General Public License as published by
00011   the Free Software Foundation, either version 3 of the License, or
00012   (at your option) any later version.
00013 
00014   OpenFLUID is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017   GNU General Public License for more details.
00018 
00019   You should have received a copy of the GNU General Public License
00020   along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
00021 
00022 
00023  == Other Usage ==
00024 
00025   Other Usage means a use of OpenFLUID that is inconsistent with the GPL
00026   license, and requires a written agreement between You and INRA.
00027   Licensees for Other Usage of OpenFLUID may use this file in accordance
00028   with the terms contained in the written agreement between You and INRA.
00029   
00030 */
00031 
00032 
00033 /**
00034   \file StdoutFileOStream.hpp
00035   \brief Header of ...
00036 
00037   \author Jean-Christophe FABRE <fabrejc@supagro.inra.fr>
00038  */
00039 
00040 
00041 #ifndef __STDOUTFILEOSTREAM_HPP__
00042 #define __STDOUTFILEOSTREAM_HPP__
00043 
00044 #include <iostream>
00045 #include <fstream>
00046 #include <boost/iostreams/stream.hpp>
00047 #include <boost/iostreams/device/null.hpp>
00048 #include <boost/filesystem/path.hpp>
00049 #include <boost/iostreams/tee.hpp>
00050 #include <boost/iostreams/device/file.hpp>
00051 #include <boost/iostreams/filtering_stream.hpp>
00052 
00053 
00054 #include <openfluid/dllexport.hpp>
00055 
00056 
00057 namespace openfluid { namespace base {
00058 
00059 
00060 class DLLEXPORT StdoutAndFileOutputStream
00061 {
00062   private:
00063 
00064     typedef boost::iostreams::tee_device<std::ostream,std::ofstream> TeeDevice;
00065 
00066 
00067   public:
00068 
00069     typedef boost::iostreams::stream<TeeDevice> TeeStream;
00070 
00071 
00072   private:
00073 
00074     TeeDevice* mp_LoggerDevice;
00075 
00076     std::ofstream m_FileLogger;
00077 
00078     TeeStream m_Logger;
00079 
00080     void tieStreams();
00081 
00082 
00083   public:
00084 
00085     StdoutAndFileOutputStream();
00086 
00087     StdoutAndFileOutputStream(std::string LogFilePath);
00088 
00089     ~StdoutAndFileOutputStream();
00090 
00091     inline std::ofstream& getFile() { return m_FileLogger; };
00092 
00093     inline std::ostream& getStdout() { return std::cout; };
00094 
00095     inline TeeStream& get() { return m_Logger; };
00096 
00097     bool open(std::string LogFilePath);
00098 
00099     void close();
00100 
00101     void flush();
00102 
00103 #if defined WIN32
00104     inline bool isOpened() { return m_FileLogger.is_open(); };
00105 #elif defined __unix__ || defined __APPLE__
00106     inline bool isOpened() const { return m_FileLogger.is_open(); };
00107 #endif
00108 
00109 };
00110 
00111 
00112 
00113 
00114 } }  // namespaces
00115 
00116 
00117 #endif /* __STDOUTFILEOSTREAM_HPP__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines