cmtkConsole.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
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, //<! Line width including margin. Default: determine based on terminal width, or default to 80 if that fails.
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     // for now, skip the output entirely if this is not the root process.
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 } // namespace cmtk
00145 
00146 #endif // #ifndef __cmtkConsole_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines