00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
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
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>() );
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