Go to the documentation of this file.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 
00029 
00030 
00031 
00032 
00033 #ifndef __cmtkConsole_h_included_
00034 #define __cmtkConsole_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <System/cmtkCannotBeCopied.h>
00039 
00040 #include <iostream>
00041 #include <string>
00042 
00043 #ifdef CMTK_BUILD_MPI
00044 #  include <mpi.h>
00045 #endif
00046 
00047 #include <System/cmtkMutexLock.h>
00048 #include <System/cmtkLockingPtr.h>
00049 
00050 namespace
00051 cmtk
00052 {
00053 
00056 
00058 class Console :
00060   private CannotBeCopied
00061 {
00062 public:
00064   Console( std::ostream& stream ) 
00065     : m_Stream( stream )
00066   { 
00067     this->IndentLevel = 0; 
00068     this->DebugLevel = 0; 
00069 #ifdef CMTK_BUILD_MPI
00070     this->m_RankMPI = -1;
00071 #endif
00072   }
00073 
00075   size_t GetLineWidth() const;
00076 
00078   Console& FormatText( const std::string& text, 
00079                        const size_t margin = 0, 
00080                        const size_t width = 0, 
00081                        const int firstLine = 0 
00082     );
00083 
00085   void printf ( const char* format, ... );
00086 
00088   void flush() 
00089   { 
00090     LockingPtr<std::ostream> pStream( this->m_Stream, this->m_MutexLock );
00091     pStream->flush(); 
00092   }
00093 
00095   template<class T> 
00096   Console& operator << ( const T data ) 
00097   { 
00098 #ifdef CMTK_BUILD_MPI
00099     
00100     if ( this->m_RankMPI < 0 ) this->m_RankMPI = MPI::COMM_WORLD.Get_rank();
00101     if ( this->m_RankMPI ) return *this;
00102 #endif
00103     LockingPtr<std::ostream> pStream( this->m_Stream, this->m_MutexLock );
00104     this->m_Stream << data; 
00105     return *this; 
00106   }
00107 
00109   void indent() { IndentLevel += 2; }
00110 
00112   void unindent() { IndentLevel -= 2; }
00113 
00114 private:
00116   std::ostream& m_Stream;
00117   
00119   size_t IndentLevel;
00120 
00122   void Indent( size_t level = 0 );
00123   
00125   int DebugLevel;
00126 
00128   MutexLock m_MutexLock;
00129 
00130 #ifdef CMTK_BUILD_MPI
00131 
00132   int m_RankMPI;
00133 #endif
00134 };
00135 
00137 extern Console StdErr;
00138 
00140 extern Console StdOut;
00141 
00143 
00144 } 
00145 
00146 #endif // #ifndef __cmtkConsole_h_included_