TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/common/utils/FileGlobal.hpp
Go to the documentation of this file.
00001 #ifndef _FileGlobal_HPP
00002 #define _FileGlobal_HPP
00003 
00004 #include <tuttle/common/patterns/Singleton.hpp>
00005 #include <tuttle/common/system/system.hpp>
00006 
00007 #include <iostream>
00008 #include <fstream>
00009 
00010 /**
00011  * @brief Output datas in the same file from anywhere.
00012  **/
00013 class FileGlobal : public Singleton<FileGlobal>
00014 {
00015 public:
00016         friend class Singleton<FileGlobal>;
00017 
00018 private:
00019         FileGlobal() : _flux( NULL ) {}
00020         ~FileGlobal() {}
00021 
00022 public:
00023         std::ofstream _flux;
00024 
00025 public:
00026         /// @param[in] fileName : file to create
00027         /// @brief File creation
00028         void openFile( std::string fileName )
00029         {
00030                 #ifndef __WINDOWS__
00031                 _flux.open( fileName.c_str() );
00032                 #else
00033                 _flux.open( fileName.c_str(), std::ios::binary );
00034                 #endif
00035                 if( !_flux )
00036                 {
00037                         std::cerr << "impossible de creer le fichier global : " << fileName << std::endl;
00038                 }
00039         }
00040 
00041         /// @brief File close
00042         void closeFile() { _flux.close(); }
00043 };
00044 
00045 //______________________________________________________________________________
00046 // Macros to output in global file
00047 
00048 /**
00049  * @param[in] FILENAME : file name
00050  * @brief Change the filename for the output
00051  **/
00052 #define FILEGLOBAL_FILENAME( FILENAME ) \
00053     if( FileGlobal::instance()._flux ) { FileGlobal::instance().closeFile(); } \
00054     FileGlobal::instance().openFile( FILENAME );
00055 
00056 /**
00057  * @param[in] ... : all parameters with an operator << defined
00058  * @brief Output into the file define by FILEGLOBAL_FILENAME (if not specified, by default "out.log")
00059  **/
00060 #define FILEGLOBAL(... ) \
00061     if( !FileGlobal::instance()._flux ) { FileGlobal::instance().openFile( "out.log" ); } \
00062     FileGlobal::instance()._flux << __VA_ARGS__ << std::endl;
00063 
00064 #define FILEGLOBAL_VAR( a ) \
00065     if( !FileGlobal::instance()._flux ) { FileGlobal::instance().openFile( "out.log" ); } \
00066     FileGlobal::instance()._flux << # a << ": " << a << std::endl;
00067 
00068 #define FILEGLOBAL_VAR2( a, b ) \
00069     if( !FileGlobal::instance()._flux ) { FileGlobal::instance().openFile( "out.log" ); } \
00070     FileGlobal::instance()._flux << # a << ": " << a << ", " << # b << ": " << b << std::endl;
00071 
00072 #define FILEGLOBAL_VAR3( a, b, c ) \
00073     if( !FileGlobal::instance()._flux ) { FileGlobal::instance().openFile( "out.log" ); } \
00074     FileGlobal::instance()._flux << # a << ": " << a << ", " << # b << ": " << b << ", " << # c << ": " << c << std::endl;
00075 
00076 #define FILEGLOBAL_VAR4( a, b, c, d ) \
00077     if( !FileGlobal::instance()._flux ) { FileGlobal::instance().openFile( "out.log" ); } \
00078     FileGlobal::instance()._flux << # a << ": " << a << ", " << # b << ": " << b << ", " << # c << ": " << c << ", " << # d << ": " << d << std::endl;
00079 
00080 /**
00081  * @param[in] ... : all parameters with an operator << defined
00082  * @brief Output into the file define by FILEGLOBAL_FILENAME (if not specified, by default "out.log")
00083  **/
00084 #define FILEGLOBAL_INFO(... ) \
00085     if( !FileGlobal::instance()._flux ) { FileGlobal::instance().openFile( "out.log" ); } \
00086     FileGlobal::instance()._flux << INFO << \
00087     "\t" << __VA_ARGS__ << std::endl;
00088 
00089 #define FILEGLOBAL_FUNCTION \
00090     if( !FileGlobal::instance()._flux ) { FileGlobal::instance().openFile( "out.log" ); } \
00091     FileGlobal::instance()._flux << INFO << std::endl;
00092 
00093 #endif