Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:34

0001 #pragma once
0002 /**
0003 sdirect.h : Stream redirection to silence noisy code unless VERBOSE is defined
0004 ================================================================================
0005 
0006 Refs:
0007 
0008 * http://stackoverflow.com/questions/5419356/redirect-stdout-stderr-to-a-string
0009 * http://wordaligned.org/articles/cpp-streambufs
0010 
0011 
0012 Usage::
0013 
0014     std::stringstream coutbuf;
0015     std::stringstream cerrbuf;
0016     {
0017         sdirect::cout_(coutbuf.rdbuf());
0018         sdirect::cerr_(cerrbuf.rdbuf());
0019    
0020         m_dc = new DetectorConstruction ;  // noisy code
0021    }
0022    std::string out = coutbuf.str();
0023    std::string err = cerrbuf.str();
0024    bool verbose = getenv("VERBOSE") != nullptr ;  
0025    std::cout << sdirect::OutputMessage("DetectorConstruction", out, err, verbose );
0026 
0027 
0028 **/
0029 
0030 #include <iostream>
0031 #include <iomanip>
0032 #include <streambuf>
0033 #include <sstream>
0034 #include <string>
0035 
0036 namespace sdirect
0037 {
0038     struct cout_ 
0039     {
0040         std::streambuf* old;
0041 
0042         cout_( std::streambuf* newbuf ) 
0043            : 
0044            old( std::cout.rdbuf(newbuf) ) 
0045         {} 
0046 
0047         ~cout_() 
0048         { 
0049             std::cout.rdbuf( old );
0050         }   
0051 
0052     };
0053 
0054     struct cerr_ 
0055     {
0056         std::streambuf* old;
0057 
0058         cerr_( std::streambuf* newbuf ) 
0059            : 
0060            old( std::cerr.rdbuf(newbuf) ) 
0061         {} 
0062 
0063         ~cerr_() 
0064         { 
0065             std::cerr.rdbuf( old );
0066         }   
0067     };
0068 
0069     // allows code expecting to write to file to 
0070     // be tricked into writing to a string 
0071     struct ostream_
0072     {
0073         std::ostream& src;
0074         std::streambuf * const sbuf;
0075 
0076         ostream_(std::ostream& dst, std::ostream& src)
0077             :   
0078             src(src), 
0079             sbuf(src.rdbuf(dst.rdbuf())) 
0080         {   
0081         }   
0082 
0083         ~ostream_() 
0084         { 
0085             src.rdbuf(sbuf); 
0086         }
0087     };
0088 
0089 
0090     inline std::string OutputMessage(const char* msg, const std::string& out, const std::string& err, bool verbose )  // static
0091     {
0092         std::stringstream ss ;
0093 
0094         ss << std::left << std::setw(30) << msg << std::right
0095            << " yielded chars : "
0096            << " cout " << std::setw(6) << out.size()
0097            << " cerr " << std::setw(6) << err.size()
0098            << " : set VERBOSE to see them "
0099            << std::endl
0100            ;   
0101 
0102         if(verbose)
0103         {   
0104             ss << "cout[" << std::endl << out << "]" << std::endl  ;
0105             ss << "cerr[" << std::endl << err << "]" << std::endl  ;
0106         }   
0107         std::string s = ss.str();
0108         return s ; 
0109     }
0110 }
0111 
0112