Console.h

00001 
00002 //    Copyright 2004, SenseGraphics AB
00003 //
00004 //    This file is part of H3D API.
00005 //
00006 //    H3D API is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    H3D API is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with H3D API; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 //    A commercial license is also available. Please contact us at 
00021 //    www.sensegraphics.com for more information.
00022 //
00023 //
00027 //  This node provides a static stream that all error and warning messages
00028 //  are sent to. The stream can be redirected to any arbitrary stream, and
00029 //  defaults to cerr. The stream can be controlled to set a minimum 
00030 //  error level to show (setting 0 will show all messages). The default level
00031 //  is 3.
00032 //
00033 //  Example usage:
00034 //
00035 //  Console.setOutputStream( cout );
00036 //  Console.setOutputLevel( 2 );
00037 //  Console(0) << "Warning:" << endl;
00038 //  Console    << " This is a warning" << endl;
00039 //  Console    << " This is still the same warning" << endl;
00040 //
00041 //  Console(1) << "Level 1" << endl;
00042 //  Console(2) << "Level 2" << endl;
00043 //  Console.setShowTime( true );
00044 //  Console(3) << "Level 3, with time" << endl;
00045 //
00047 #ifndef __CONSOLE_H__
00048 #define __CONSOLE_H__
00049 
00050 #include <ostream>
00051 #include <sstream>
00052 #include <string>
00053 #include <iostream>
00054 #include <iomanip>
00055 #include "H3DApi.h"
00056 #include "TimeStamp.h"
00057 
00058 
00059 
00060 namespace H3D {
00061 
00062   template <class CharT, class TraitsT = std::char_traits<CharT> >
00063   class basic_debugbuf : public std::basic_stringbuf<CharT, TraitsT>  {
00064     int outputlevel, level;
00065     ostream *outputstream;
00066     TimeStamp starttime;
00067     bool showtime;
00068     bool showlevel;
00069   public:
00070     basic_debugbuf(  ) : 
00071       outputstream( &cerr ),
00072       outputlevel( 3 ),
00073       level( 0 ),
00074       showtime(false),
00075       showlevel( true ) {
00076     }
00077 
00078     virtual ~basic_debugbuf() {
00079       outputlevel=-1;
00080       sync();
00081     }
00082 
00083     void setShowTime( bool show ) { showtime = show; }
00084 
00085     void setShowLevel( bool show ) { showlevel = show; }
00086 
00087     void setOutputStream( ostream &s ) { outputstream = &s; }
00088 
00089     void setOutputLevel( int _outputlevel ) { outputlevel = _outputlevel; }
00090 
00091     void setLevel( int _level ) { level = _level; }
00092   
00093   protected:
00094 
00095     int sync() {
00096       TimeStamp time;
00097       
00098       if ( outputlevel >= 0  &&  level >= outputlevel ) {
00099         if ( showlevel || showtime ){
00100           *outputstream << "[";
00101         }
00102         
00103         if ( showlevel ) {
00104           if ( level <= 2 ) {
00105             *outputstream << "I"; }
00106           else {
00107             *outputstream << "W"; }
00108         }
00109         
00110         if ( showlevel && showtime ) {
00111           *outputstream << " ";
00112         }
00113         
00114         if ( showtime ) {
00115           *outputstream << std::setfill('0')
00116                         << std::setprecision(2)
00117                         << std::setiosflags(std::ios::fixed)
00118                         << std::setw(6)
00119                         << (time-starttime)
00120             // Reset to default
00121                         << std::setfill(' ')
00122                         << std::setprecision(6)
00123                         << std::resetiosflags(std::ios::floatfield);
00124         }
00125         if ( showlevel || showtime ) {
00126           *outputstream << "] ";
00127         }
00128         
00129         *outputstream << std::basic_stringbuf<CharT, TraitsT>::str();
00130       }
00131       
00132       str( std::basic_string<CharT>() ); // Clear the string buffer
00133       
00134       return 0;
00135     }
00136     
00137   };
00138 
00139 
00140   template<class CharT, class TraitsT = std::char_traits<CharT> >
00141   class basic_dostream : public std::basic_ostream<CharT, TraitsT> {
00142   public:
00143   
00144     basic_dostream() : 
00145       std::basic_ostream<CharT, TraitsT>(new basic_debugbuf<CharT, TraitsT>()) {
00146     }
00147 
00148     ~basic_dostream() {
00149       delete std::ios::rdbuf(); 
00150     }
00151 
00152     void setShowTime( bool show ) { 
00153       static_cast< basic_debugbuf<CharT, TraitsT>* >(std::ios::rdbuf())->
00154         setShowTime( show );  
00155     }
00156 
00157     void setShowLevel( bool show ) { 
00158       static_cast< basic_debugbuf<CharT, TraitsT>* >(std::ios::rdbuf())->
00159         setShowLevel( show );  
00160     }
00161 
00162     void setOutputStream( ostream &s ) { 
00163       static_cast< basic_debugbuf<CharT, TraitsT>* >(std::ios::rdbuf())->
00164         setOutputStream( s );  
00165     }
00166 
00167     void setOutputLevel( int _outputlevel ) {
00168       static_cast<basic_debugbuf<CharT, TraitsT>* >( std::ios::rdbuf() )->
00169         setOutputLevel( _outputlevel ); 
00170     }
00171 
00172     void setLevel( int _level ) { 
00173       static_cast< basic_debugbuf<CharT, TraitsT>* >(std::ios::rdbuf())->
00174         setLevel( _level );  
00175     }
00176 
00177     basic_dostream & operator ()( int l ) {
00178       setLevel( l );
00179       return *this;
00180     }
00181 
00182   };
00183 
00184   typedef basic_dostream<char>    ConsoleStream;
00185 
00186   extern H3DAPI_API ConsoleStream Console;
00187 
00188 }
00189 #endif

Generated on Thu Aug 24 12:38:32 2006 for H3D API by  doxygen 1.4.5